模拟斗地主发扑克的编程

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

效果展示:

相关推荐
HerayChen3 小时前
HbuilderX 内存溢出报错
java·大数据·linux
程序员小白条4 小时前
0经验如何找实习?
java·开发语言·数据结构·数据库·链表
小马爱打代码4 小时前
Spring AI:搭建自定义 MCP Server:获取 QQ 信息
java·人工智能·spring
郭涤生4 小时前
QT 架构笔记
java·数据库·系统架构
daidaidaiyu4 小时前
基于LangGraph开发复杂智能体学习一则
java·ai
小小8程序员5 小时前
STL 库(C++ Standard Template Library)全面介绍
java·开发语言·c++
a努力。5 小时前
Redis Java 开发系列#2 数据结构
java·数据结构·redis
a努力。6 小时前
腾讯Java面试被问:String、StringBuffer、StringBuilder区别
java·开发语言·后端·面试·职场和发展·架构
Vic101017 小时前
解决 Spring Security 在异步线程中用户信息丢失的问题
java·前端·spring
QD_IT伟7 小时前
SpringBoot项目整合Tlog 数据链路的规范加强
java·spring boot·后端