扑克牌洗牌发牌

模拟扑克牌

花色:♠ ♥ ♦ ♣

* 牌号:A 2 3 4 5 6 7 8 9 10 J Q K

* 大王、小王

* 1.生成一副牌

* 2.然后发牌

定义PockCard类

/**
 * 一张扑克牌类(一张牌就是一个对象)
 * 花色:♠  ♥   ♦   ♣
 *    牌号:A 2 3 4 5 6 7 8 9 10 J Q K
 *    大王、小王
 */
public class PokerCard {
    private String color; //花色
    private String cardNum; //牌号
    public PokerCard() {
    }
    public PokerCard(String color, String cardNum) {
        this.color = color;
        this.cardNum = cardNum;
    }

    @Override
    public String toString() {
        return  color +""+  cardNum ;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getCardNum() {
        return cardNum;
    }

    public void setCardNum(String cardNum) {
        this.cardNum = cardNum;
    }
}

定义Pocker类

public class Poker {
    //一副牌
    private List<PokerCard> pokerList;
        public List<PokerCard> getPokerList() {
        return pokerList;
    }

    public void setPokerList(List<PokerCard> pokerList) {
        this.pokerList = pokerList;
    }
}

初始化牌

//无参构造器,初始化一副牌
    public Poker() {
        String[]colors={"♠","♥","♣","♦"};
        pokerList = new ArrayList<>();
        //接下来生成牌
        //A牌的四种花色
        for (int i = 0; i < colors.length; i++) {//四种花色
            PokerCard pc = new PokerCard(colors[i], "A");
            pokerList.add(pc);
        }
        //2-10的牌
        for (int i = 2; i <= 10; i++) {
            //2-10牌的四种花色
            for (int j = 0; j < colors.length; j++) {//四种花色
                PokerCard pc = new PokerCard(colors[j], i+"");
                pokerList.add(pc);
            }
        }
        //JQK牌的四种花色
        for (int i = 0; i < colors.length; i++) {//四种花色
            PokerCard pc = new PokerCard(colors[i], "J");
            pokerList.add(pc);
        }
        for (int i = 0; i < colors.length; i++) {//四种花色
            PokerCard pc = new PokerCard(colors[i], "Q");
            pokerList.add(pc);
        }
        for (int i = 0; i < colors.length; i++) {//四种花色
            PokerCard pc = new PokerCard(colors[i], "K");
            pokerList.add(pc);
        }
        //大小王
        PokerCard pc1 = new PokerCard(null, "大王");
        pokerList.add(pc1);
        PokerCard pc2 = new PokerCard(null, "小王");
        pokerList.add(pc2);
    }

洗牌

    //洗牌
    public void Shuffle(){
        //获取牌
        List<PokerCard> pkcList = this.getPokerList();
        Object[] total = pkcList.toArray();
        //洗牌是随机的
        Random r = new Random();
        for (int j = 0; j < total.length; j++) {
            int sj = r.nextInt(54);
            System.out.print(total[sj]+" ");
        }
        System.out.println();
    }

发牌

    //发牌
    public void faPai(){
        //获取牌
        List<PokerCard> pkcList = this.getPokerList();
        Object[] total = pkcList.toArray();
        //存储三个玩家的牌
        Object[][] plays = new Object[3][17];
        //存储当前剩余牌的数量
        int leftNum = 54;
        //发牌是随机的
        Random r = new Random();
        //分别把牌分给3个人,留三张底牌,也就是说一个人能分到17张牌
        for (int i = 0; i < 17; i++) {
            //为每个人发牌
            for (int j = 0; j < plays.length; j++) {
                //System.out.println(j);
                int ranNumber = r.nextInt(leftNum);
                //发牌
                plays[j][i] = total[ranNumber];
                //移动发的牌
                total[ranNumber] = total[leftNum - 1];

                leftNum--;
            }

        }

        for (int i = 0; i < plays.length; i++) {
            System.out.print("玩家:"+i+"的牌 ");
            for (int j = 0; j < plays[i].length; j++) {
                System.out.print(plays[i][j] + " ");
            }
            System.out.println();
        }
        System.out.print("底牌:");
        for(int i = 0;i < 3;i++){
            System.out.print(total[i]);
        }

        System.out.println();

    }

测试

运行截图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值