模拟斗地主发扑克的编程

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

效果展示:

相关推荐
打工的小王1 分钟前
java并发编程(三)CAS
java·开发语言
尤老师FPGA28 分钟前
使用ZYNQ芯片和LVGL框架实现用户高刷新UI设计系列教程(第四十五讲)
android·java·ui
星火开发设计1 小时前
C++ 函数定义与调用:程序模块化的第一步
java·开发语言·c++·学习·函数·知识
cypking1 小时前
二、前端Java后端对比指南
java·开发语言·前端
未若君雅裁1 小时前
SpringAI基础入门
java·spring boot·ai
CC.GG1 小时前
【C++】用哈希表封装myunordered_map和 myunordered_set
java·c++·散列表
a努力。2 小时前
字节Java面试被问:TCP的BBR拥塞控制算法原理
java·开发语言·python·tcp/ip·elasticsearch·面试·职场和发展
jiaguangqingpanda2 小时前
Day24-20260120
java·开发语言·数据结构
一个龙的传说2 小时前
xshell下载
java
C雨后彩虹3 小时前
羊、狼、农夫过河
java·数据结构·算法·华为·面试