模拟斗地主发扑克的编程

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;
    }
}

效果展示:

相关推荐
fox_mt14 分钟前
AI Coding - ClaudeCode使用指南
java·ai编程
毕设源码-郭学长32 分钟前
【开题答辩全过程】以 基于SSM的高校运动会管理系统的设计与实现为例,包含答辩的问题和答案
java·eclipse
qq_54702617934 分钟前
Maven 使用指南
java·maven
xiaolyuh12342 分钟前
Arthas修改类(如加日志)的实现原理
java
栗子叶1 小时前
Java对象创建的过程
java·开发语言·jvm
有一个好名字1 小时前
力扣-从字符串中移除星号
java·算法·leetcode
zfj3211 小时前
CyclicBarrier、CountDownLatch、Semaphore 各自的作用和用法区别
java·开发语言·countdownlatch·semaphore·cyclicbarrier
2501_916766541 小时前
【JVM】类的加载机制
java·jvm
Sag_ever1 小时前
Java数组详解
java
张np1 小时前
java基础-ConcurrentHashMap
java·开发语言