模拟斗地主发扑克的编程

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

效果展示:

相关推荐
ldj20202 分钟前
SpringBoot为什么使用new RuntimeException() 来获取调用栈?
java·spring boot·后端
超龄超能程序猿3 分钟前
Spring 应用中 Swagger 2.0 迁移 OpenAPI 3.0 详解:配置、注解与实践
java·spring boot·后端·spring·spring cloud
风象南15 分钟前
SpringBoot配置属性热更新的轻量级实现
java·spring boot·后端
洛阳泰山16 分钟前
Spring Boot 整合 Nacos 实战教程:服务注册发现与配置中心详解
java·spring boot·后端·nacos
Y40900116 分钟前
C语言转Java语言,相同与相异之处
java·c语言·开发语言·笔记
YuTaoShao17 分钟前
【LeetCode 热题 100】994. 腐烂的橘子——BFS
java·linux·算法·leetcode·宽度优先
布朗克16817 分钟前
java常见的jvm内存分析工具
java·jvm·数据库
都叫我大帅哥1 小时前
深入浅出 Resilience4j:Java 微服务的“免疫系统”实战指南
java·spring cloud
Cao_Shixin攻城狮3 小时前
Flutter运行Android项目时显示java版本不兼容(Unsupported class file major version 65)的处理
android·java·flutter
Dcs6 小时前
还在用 Arrays.hashCode?Java 自己也能写出更快的版本!
java