使用ArrayList进行斗地主的发牌

GameDemo_fapai类

public class GameDemo_fapai {
    /**
     * 分析业务需求
     * 总共有54张牌
     * 点数:"3""4""5""6""7""8""9""10"以""Q""K""A""2
     * 花色:"♣", "♦", "♥", "♠"
     * 大小王:"大🤡“ ”小🤡“
     * 斗地主:发出51张牌,剩下3张做为底牌。
     */
    public static void main(String[] args) {
        Room room = new Room();
        room.start();

    }
}

Room类

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Room {
    //必须有一副牌
    private List<Card> allCard = new ArrayList<>();
    public Room(){
        //1.54张牌
        //a.点数
        String[] Number = new String[]{"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        //b.花色
        String[] color = new String[]{"♣", "♦", "♥", "♠"};
        //c.遍历点数,再遍历花色
        int size=0;
        for (String num : Number) {
            size++;
            for (String col : color) {
                allCard.add(new Card(num,col,size));
            }
        }
        Card card1 = new Card("","大🤡",++size);
        Card card2 = new Card("","小🤡",++size);
        Collections.addAll(allCard,card1,card2);
        System.out.println("新牌"+allCard);
    }

    public void start() {
        ArrayList<Card> zhangsan = new ArrayList<>();
        ArrayList<Card> lisi = new ArrayList<>();
        ArrayList<Card> wangwu= new ArrayList<>();
        //洗牌
        Collections.shuffle(allCard);
        System.out.println("洗牌后"+allCard);
        //发牌
        for (int i = 0; i < allCard.size()-3; i++) {
            Card card = allCard.get(i);
            if(i%3==0){
                zhangsan.add(card);
            }else if(i%3==1){
               lisi.add(card);
            } else if(i%3==2){
                wangwu.add(card);
            }
        }
        //理牌
        Collections.sort(zhangsan);
        Collections.sort(lisi);
        Collections.sort(wangwu);

        //看牌
        System.out.println("张三的牌"+zhangsan);
        System.out.println("李四的牌"+lisi);
        System.out.println("王五的牌"+wangwu);
        List<Card> dizhu = allCard.subList(allCard.size() - 3, allCard.size());
        System.out.println(dizhu);
        lisi.addAll(dizhu);
        Collections.sort(lisi);
        System.out.println("李四抢完地主"+lisi);
    }
}

Card类

public class Card implements  Comparable{
    private String number;
    private String color;
    private int size;

    public Card() {
    }

    public Card(String number, String color, int size) {
        this.number = number;
        this.color = color;
        this.size = size;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getColor() {
        return color;
    }

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

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

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

    @Override
    public int compareTo(Object o) {
        Card a = (Card) o;
        return (this.size-a.size);
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值