java实现扑克牌

1、 定义card类

属性 rank代表牌面值0-13
suit 代表花色

class Card{
    public int rank;//牌面值
    public String suit;

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

    @Override
    public String toString() {
        return String.format("[%s %d]", suit, rank);
    }
}

定义四种花色 “♥”, “♠”, “♦”, “♣”

public static final String[] SUITS = {"♥", "♠", "♦", "♣"};

2、生成52张牌

//生成牌 52
        public static List<Card> buyDeck() {
            List<Card> deck = new ArrayList<>();
            for (int i = 0; i < 4; i++){
                for (int j = 1; j <= 13; j++){
                    //int rank = i;
                    //String suit = SUITS[j];
                    Card card = new Card(j, SUITS[i]);
                    deck.add(card);
                }
            }
            return deck;
        }

3、打乱牌面顺序
这里采用从后向前的顺序进行交换,每次把最后一张牌与前面随机的一张牌进行交换使用,使用rand函数生成随机值

	private  static void swap(List<Card> deck,int index, int i){
            //tmp = [index]
            //[index] = [i]
            //[i] = tmp;
            Card tmp = deck.get(index);
            deck.set(index, deck.get(i));
            deck.set(i, tmp);
        }

        //洗牌
        public static void shuffle(List<Card> deck){
            for (int i = deck.size() - 1; i >0; i--){
                Random random = new Random();
                int index = random.nextInt(i);//[0,i)
                swap(deck, index, i);
            }
        }

3、三个人每人轮流起五张牌
这里采用嵌套列表的方式实现,定义一个总的列表用来存储下面的每个人存牌的列表。每次从最上面抽取一张牌,由总列表控制每人每轮取到一张牌,直到每人都取到五张牌为止。在这里插入图片描述

public static void main(String[] args) {
        List<Card> deck = buyDeck();
        System.out.println(deck);
        System.out.println("==========洗牌==========");
        shuffle(deck);
        System.out.println(deck);

        //三个人每人揭牌五张
        List<List<Card>> hand = new ArrayList<>();
        List<Card> hands1 = new ArrayList<>();
        List<Card> hands2 = new ArrayList<>();
        List<Card> hands3 = new ArrayList<>();

        hand.add(hands1);
        hand.add(hands2);
        hand.add(hands3);

        for (int i = 0; i < 5; i++){
            for (int j = 0; j < 3; j++){
                Card card = deck.remove(0);
                hand.get(j).add(card);
                // List<Card> lsit = hand.get(j);
                // list.add(card);
            }
        }
        System.out.println("第一个人的牌:");
        System.out.println(hands1);
        System.out.println("第二个人的牌:");
        System.out.println(hands2);
        System.out.println("第三个人的牌:");
        System.out.println(hands3);
        System.out.println("剩下的牌:");
        System.out.println(deck);
    }

完整代码

class Card{
    public int rank;//牌面值
    public String suit;

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

    @Override
    public String toString() {
        return String.format("[%s %d]", suit, rank);
    }
}
public class CardDemo {
        public static final String[] SUITS = {"♥", "♠", "♦", "♣"};

        //买牌 52
        public static List<Card> buyDeck() {
            List<Card> deck = new ArrayList<>();
            for (int i = 0; i < 4; i++){
                for (int j = 1; j <= 13; j++){
                    //int rank = i;
                    //String suit = SUITS[j];
                    Card card = new Card(j, SUITS[i]);
                    deck.add(card);
                }
            }
            return deck;
        }

        private  static void swap(List<Card> deck,int index, int i){
            //tmp = [index]
            //[index] = [i]
            //[i] = tmp;
            Card tmp = deck.get(index);
            deck.set(index, deck.get(i));
            deck.set(i, tmp);
        }

        //洗牌
        public static void shuffle(List<Card> deck){
            for (int i = deck.size() - 1; i >0; i--){
                Random random = new Random();
                int index = random.nextInt(i);//[0,i)
                swap(deck, index, i);
            }
        }

    public static void main(String[] args) {
        List<Card> deck = buyDeck();
        System.out.println(deck);
        System.out.println("洗牌:");
        shuffle(deck);
        System.out.println(deck);

        //三个人每人揭牌五张
        List<List<Card>> hand = new ArrayList<>();
        List<Card> hands1 = new ArrayList<>();
        List<Card> hands2 = new ArrayList<>();
        List<Card> hands3 = new ArrayList<>();

        hand.add(hands1);
        hand.add(hands2);
        hand.add(hands3);

        for (int i = 0; i < 5; i++){
            for (int j = 0; j < 3; j++){
                Card card = deck.remove(0);
                hand.get(j).add(card);
                // List<Card> lsit = hand.get(j);
                // list.add(card);
            }
        }
        System.out.println("第一个人的牌:");
        System.out.println(hands1);
        System.out.println("第二个人的牌:");
        System.out.println(hands2);
        System.out.println("第三个人的牌:");
        System.out.println(hands3);
        System.out.println("剩下的牌:");
        System.out.println(deck);
    }
}

运行结果
在这里插入图片描述

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值