0
点赞
收藏
分享

微信扫一扫

Collection 斗地主游戏

他说Python 2022-04-29 阅读 63
package day8_doudizhu;

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

//1.做牌
//2.洗牌
//3.定义三个玩家
//4.发牌
//5.排序
//6.看牌
public class GameDemo {
    //1.定义一个静态的集合存储54张牌
    public static List<Card> allCards = new ArrayList<>();

    //2.做牌,定义静态代码块初始化牌数据
    static {
        //3.定义点数:个数确定,类型确定,使用数组
        String[] sizes = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        //4.定义花色:个数确定,类型确定,用数组
        String[] colors = {"♣", "♥", "♦", "♠"};
        //5.组合点数和花色
        int index = 0;//记录牌的大小
        for (String size : sizes) {
            index++;
            for (String color : colors) {
                //6.封装成一个牌对象
                Card c = new Card(size, color,index);
                //7.存入到集合容器中去
                allCards.add(c);
            }
        }
        //8.大小王存入到集合容器中去
        Card c1 = new Card("", "🃏",++index);
        Card c2 = new Card("", "🃏",++index+1);
        Collections.addAll(allCards, c1, c2);

        System.out.println("新牌" + allCards);
    }

    public static void main(String[] args) {
        //9.洗牌
        Collections.shuffle(allCards);
        System.out.println("洗牌后:" + allCards);
        //10发牌,定义三个玩家,每个玩家的牌也是一个集合容器
        List<Card> linhuchong = new ArrayList<>();
        List<Card> jiumozhi = new ArrayList<>();
        List<Card> renyingying = new ArrayList<>();
        //allCards=[♠J, ♣10, ♥7, ♠10, ♠K, 🃏, ♣5, ♥K, ♥A, ♦9, ♠3, ♣3, ♠Q
        //    i      0    1   2                                      %    3
        //11.开始发牌(发 51张,剩余三张做底牌)
        for (int i = 0; i < allCards.size() - 3; i++) {
            Card c = allCards.get(i);
            if (i % 3 == 0) {
                linhuchong.add(c);
            } else if (i % 3 == 1) {
                jiumozhi.add(c);
            } else if (i % 3 == 2) {
                renyingying.add(c);
            }

        }
        //12.拿到三张底牌(把最后三张截取成一个子集)
        List<Card> lastThreeCards = allCards.subList(allCards.size()-3,allCards.size());
        //13.给玩家的牌排序(从大到小)
        sortCards(linhuchong);
        sortCards(jiumozhi);
        sortCards(renyingying);
        //14.输出玩家的牌
        System.out.println("阿冲:"+linhuchong);
        System.out.println("阿纠:"+jiumozhi);
        System.out.println("盈盈:"+renyingying);
        System.out.println("底牌:"+lastThreeCards);

    }

    private static void sortCards(List<Card> cards) {
        Collections.sort(cards, new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                return o2.getIndex()- o1.getIndex();
            }
        });
    }
}

 

package day8_doudizhu;

public class Card {
    private String size;
    private String color;
    private int index;//牌的大小
    public Card() {
    }

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

    public String getSize() {
        return size;
    }

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

    public String getColor() {
        return color;
    }

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

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

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

 

举报

相关推荐

0 条评论