模拟斗地主发牌

public class Card {
    private String number;
    private String color;
    //每张牌的大小
    private int size;

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

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

    public Card() {
    }

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

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

public class Room {
    //必须有一副牌
    List<Card> allCards = new ArrayList<>();
    public Room(){
        //做出24张牌,存入allCards
        String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","1","2"};
        String[] colors = {"♠","♥","♣","♦"};
        int size = 0;//表示每张牌的大小
        //遍历点数,在遍历花色
        for (String number : numbers) {
            size++;
            for (String color : colors) {
                Card c = new Card(number,color,size);//存入牌
                allCards.add(c);
            }
        }
        allCards.add(new Card("","小王",size++));
        allCards.add(new Card("","大王",size++));
        System.out.println("新牌:"+allCards);
    }

    public void start() {
        //洗牌
        Collections.shuffle(allCards);
        System.out.println("洗牌后:"+allCards);

        //发牌,定义三个玩家
        List<Card> people11 = new ArrayList<>();
        List<Card> people22 = new ArrayList<>();
        List<Card> people33 = new ArrayList<>();

        for (int i = 0; i < allCards.size()-3; i++) {
            Card c= allCards.get(i);
            if(i%3==0){
                people11.add(c);
            }else if(i%3==1){
                people22.add(c);
            }else{
                people33.add(c);
            }
        }

        //对三个玩家的牌进行排序
        sortCards(people11);
        sortCards(people22);
        sortCards(people33);

        //看牌
        System.out.println("第一个玩家:"+people11);
        System.out.println("第二个玩家:"+people22);
        System.out.println("第三个玩家:"+people33);
        System.out.println("三张底牌:"+allCards.subList(allCards.size()-3,allCards.size()));
        List<Card> lastThreeCards = allCards.subList(allCards.size()-3,allCards.size());
        //一号玩家抢到了地主
        people11.addAll(lastThreeCards);
        sortCards(people11);
        System.out.println("第一个玩家抢到地主后:"+people11);

    }
//进行排序
    private void sortCards(List<Card> cards) {
        Collections.sort(cards, new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                return o1.getSize()-o2.getSize();
            }
        });

    }
}

public class GameDemo {
    public static void main(String[] args) {
        Room room = new Room();
        //启动游戏
        room.start();



    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值