练习扑克牌

练习扑克牌

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Carddemo {

    public static void main(String[] args) {
        DeckCard as = new DeckCard();
        List<Card> listcards = as.buyCard();
        as.washCard(listcards);
        System.out.println(listcards);
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入玩牌的人数");
        int people = sc.nextInt();
        List<List<Card>> ass = as.sendCard(listcards, people);
        for (int i = 0; i < people; i++) {
            System.out.println(ass.get(i));
        }
    }
}

//纸牌
class Card {
    public String suit;
    public int rank;

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

    @Override
    public String toString() {
        return "[" + suit + "," + rank + "]";
    }
}

//扑克牌
class DeckCard {
    public static final String[] suits = {"♥", "♠", "♦", "♣"};

    //获得新的扑克牌
    public List<Card> buyCard() {
        List<Card> cardList = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13; j++) {
                String suit1 = suits[i];
                int rank1 = j;
                Card card = new Card(suit1, rank1);
                cardList.add(card);
            }
        }
        return cardList;
    }

    //洗牌
    public void washCard(List<Card> listcard) {
        for (int i = listcard.size() - 1; i > 0; i--) {
            Random random = new Random();
            int j = random.nextInt(i);
            swap(listcard, i, j);
        }
    }


    public void swap(List<Card> cardList, int i, int j) {
        Card temp = cardList.get(i);
        cardList.set(i, cardList.get(j));
        cardList.set(j, temp);
    }

    //发牌
    public List<List<Card>> sendCard(List<Card> listCard, int people) {
        List<List<Card>> peoples = new ArrayList<>();
        for (int i = 0; i < people; i++) {
            peoples.add(new ArrayList<>());

        }
        while (!listCard.isEmpty()) {
            for (int i = 0; i < people; i++) {
                if (listCard.isEmpty()) {
                    break;
                }
                Card card = listCard.remove(0);
                peoples.get(i).add(card);
            }

        }
        return peoples;
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值