模拟斗地主发扑克的编程

java 复制代码
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        int numPlayers = 3; // 固定为3名玩家
        int cardsPerPlayer = 17; // 每位玩家获得17张牌
        int bottomCards = 3; // 底牌数量

        Deck deck = new Deck();
        List<List<Card>> playersHands = new ArrayList<>();

        // 发给每位玩家17张牌
        for (int i = 0; i < numPlayers; i++) {
            List<Card> hand = deck.deal(cardsPerPlayer);
            playersHands.add(hand);
            System.out.println("玩家" + (i + 1) + "的手牌: " + hand);
        }

        // 底牌
        List<Card> bottomCardsList = deck.deal(bottomCards);
        System.out.println("底牌: " + bottomCardsList);
    }
}

class Card {
    private String suit; // 花色
    private String rank; // 点数

    public Card(String suit, String rank) {
        this.suit = suit;
        this.rank = rank;
    }

    @Override
    public String toString() {
        return  suit + rank;
    }
}

class Deck {
    private List<Card> cards;

    public Deck() {
        this.cards = new ArrayList<>();
        String[] suits = {"♥", "♠", "♦", "♣"};
        String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};

        // 添加普通牌
        for (String suit : suits) {
            for (String rank : ranks) {
                cards.add(new Card(suit, rank));
            }
        }
        // 添加大小王
        cards.add(new Card("大", "王"));  // 大王
        cards.add(new Card("小", "王")); // 小王

        Collections.shuffle(cards); // 洗牌
    }

    public List<Card> deal(int numCards) {
        List<Card> hand = new ArrayList<>();
        for (int i = 0; i < numCards && !cards.isEmpty(); i++) {
            hand.add(cards.remove(0)); // 从顶部发牌
        }
        return hand;
    }
}

效果展示:

相关推荐
Lyyaoo.5 小时前
【JAVA基础面经】JAVA的面向对象特性
java·开发语言·windows
浮游本尊5 小时前
Java学习第37天 - 领域驱动设计(DDD)与 CQRS 实战
java
米糕闯编程5 小时前
xshell使用CentOS10 root用户登录,权限问题
java·linux
sxhcwgcy5 小时前
Python中的简单爬虫
java
xiaoliuliu123455 小时前
Android Studio 2025 安装教程:详细步骤+自定义安装路径+SDK配置(附桌面快捷方式创建)
java·前端·数据库
老前端的功夫5 小时前
【Java从入门到入土】21:List三剑客:ArrayList、LinkedList、Vector的爱恨情仇
java·javascript·网络·python·list
SAP小崔说事儿5 小时前
SAP B1 批量应用用户界面配置模板
java·前端·ui·sap·b1·无锡sap
电商API&Tina6 小时前
唯品会数据采集API接口||电商API数据采集
java·javascript·数据库·python·sql·json
人机与认知实验室6 小时前
Maven与以色列福音系统有何区别?
java·maven
wuqingshun3141596 小时前
spring如何解决循环依赖问题的?
java