关于纸牌的代码

纸牌

public class PlayingCards {

    private static final String[] CARD_VALUE = new String[]{"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
    private static final String[] CARD_COLOR = new String[]{"红桃", "黑桃", "方片", "梅花"};

    public static void main(String[] args) {
        try {
            System.out.println("============= 请输入玩家多少人?=============");
            // 玩家数量
            Scanner scanner = new Scanner(System.in);
            int player = scanner.nextInt();

            System.out.println("============= 请输入纸牌多少套?=============");
            // 纸牌套数
            int count = scanner.nextInt();

            List<Player> result = PlayingCards.licensing(player, count);

            result.forEach(System.out::println);
        } catch (Exception e) {
            System.out.println("控制台输入数据非法");
        }
    }

    // 发牌
    private static List<Player> licensing(int number, int count) {
        // 发牌结果集
        List<Player> result = new ArrayList<>();
        // 入场
        for (int i = 1; i <= number; i++) {
            String name = "玩家".concat(String.valueOf(i));
            result.add(new Player(IdentityGenerator.getUuid(), name));
        }

        // 洗牌
        List<Card> cards = PlayingCards.shuffle(count);

        // 发牌
        PlayingCards.assign(cards, result);

        // 理牌
        for (Player player : result) {
            List<Card> list = player.getCards();
            list.sort(new Comparator<Card>() {
                @Override
                public int compare(Card o1, Card o2) {
                    return o1.index - o2.index;
                }
            });
        }
        return result;
    }

    // 洗牌
    private static List<Card> shuffle(int count) {
        // 返回集
        List<Card> result = new ArrayList<>();

        for (int i = 0; i < count; i++) {
            Arrays.stream(CARD_COLOR).forEach(color -> {
                for (int index = 1; index <= CARD_VALUE.length; index++) {
                    result.add(new Card(IdentityGenerator.getUuid(), index, color, CARD_VALUE[index - 1], 0));
                }
            });
            result.add(new Card(IdentityGenerator.getUuid(), 14, "", "小王", 1));
            result.add(new Card(IdentityGenerator.getUuid(), 15, "", "大王", 2));
        }

        Collections.shuffle(result);

        return result;
    }

    // 分牌
    private static void assign(List<Card> cards, List<Player> players) {
        // 总牌数
        for (int i = 0; i < cards.size(); i++) {
            int index = i % players.size();
            Player player = players.get(index);
            player.getCards().add(cards.get(i));
        }
    }

    static class Player {
        private String uuid;
        private String name;
        private List<Card> cards = new ArrayList<>();

        public Player(String uuid, String name) {
            this.uuid = uuid;
            this.name = name;
        }

        public String getUuid() {
            return uuid;
        }

        public void setUuid(String uuid) {
            this.uuid = uuid;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<Card> getCards() {
            return cards;
        }

        public void setCards(List<Card> cards) {
            this.cards = cards;
        }

        @Override
        public String toString() {
            return "Player{" + name + ", cards=" + cards.toString() + "}";
        }
    }

    static class Card {
        private String uuid;
        private String color;
        private int index;
        private String value;
        private int type = 0; // 0:普通牌 1:小王 2:大王

        public Card(String uuid, int index, String color, String value, int type) {
            this.uuid = uuid;
            this.color = color;
            this.index = index;
            this.value = value;
            this.type = type;
        }

        public String getUuid() {
            return uuid;
        }

        public void setUuid(String uuid) {
            this.uuid = uuid;
        }

        public String getColor() {
            return color;
        }

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

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        @Override
        public String toString() {
            return type == 0 ? (color + value) : value;
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值