java-study ArrayList实现发牌、洗牌、排序、随机地主

输出示例

java-study ArrayList实现发牌、洗牌、排序、随机地主_第1张图片

package com.TestCard;

import java.util.*;

public class Test1 {
    public static void main(String[] args) {
        //创建并拿到一副新牌
        List cards = new GenerateCards().getCards();
        //创建三个个玩家
        Player p1 = new Player("张三丰");
        Player p2 = new Player("杨过");
        Player p3 = new Player("东方不败");
        //创建对局
        Game game = new Game(cards,p1,p2,p3);
        System.out.println(p1);
        System.out.println(p2);
        System.out.println(p3);
        System.out.println("地主是:"+game.getLandlord().getNAME());
        System.out.println("底牌是:"+game.getFLOOR_CARDS());
    }
}
class Card {
    public final String TYPE;
    public final String NUM;
    public final int INDEX;//用来排序的
    public Card(String t, String n, int i) {
        TYPE = t;
        NUM = n;
        INDEX = i;
    }
    @Override
    public String toString() {
        return NUM + TYPE;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Card card = (Card) o;
        return Objects.equals(TYPE, card.TYPE) &&
                Objects.equals(NUM, card.NUM);
    }

    @Override
    public int hashCode() {
        return Objects.hash(TYPE, NUM);
    }
}
class GenerateCards {
    public static final List NUM = new ArrayList<>();
    public static final List TYPE = new ArrayList<>();
    public static final List KING = new ArrayList<>();
    public static final int TOTAL = 54;
    static {
        Collections.addAll(NUM,"3","4","5","6","7","8","9","10","J","Q","K","A","2");
        Collections.addAll(TYPE,"❤","♠","♦","♣");
        Collections.addAll(KING,"小王","大王");
    }
    public List getCards() {
        List res = new ArrayList<>();
        int index = 0;
        for (String n : NUM) {
            for (String t : TYPE) {
                res.add(new Card(t,n,index++));
            }
        }
        for (String s : KING) {
            res.add(new Card(s,"",index++));
        }
        return res;
    }
}
class Player {
    public final String NAME;
    public List cards = new ArrayList<>();
    public Player(String n) {
        NAME = n;
    }
    public List getCards() {
        return cards;
    }
    public void setCards(List cards) {
        this.cards = cards;
    }
    public String getNAME() {
        return NAME;
    }
    @Override
    public String toString() {
        return "玩家:"+NAME+"\n"+"剩余牌:"+cards;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Player player = (Player) o;
        return Objects.equals(NAME, player.NAME);
    }

    @Override
    public int hashCode() {
        return Objects.hash(NAME);
    }
}
class Game {
    private final List FLOOR_CARDS = new ArrayList<>();
    private final List CARDS;
    private final Player[] PLAYERS;
    private Player landlord;
    public Game(List cards, Player...players) {
        CARDS = cards;
        PLAYERS = players;
        //洗牌、发牌
        sendCard();
        //叫地主
        selectLandlord();
    }
    private void sendCard() {
        /**
         * 发牌
         * 底牌数量 = 玩家人数
         * 发牌数量 = 牌总数 - 底牌数
         */
        Collections.shuffle(CARDS);
        int cardsSize = CARDS.size();
        int playersLen = PLAYERS.length;
        for(int i = 0; i < cardsSize - playersLen; i++) {
            Player currPlayer = PLAYERS[i % playersLen];
            currPlayer.cards.add(CARDS.get(i));
        }
        FLOOR_CARDS.addAll(CARDS.subList(cardsSize - playersLen, cardsSize));
        //牌排序
        for (Player player : PLAYERS) {
            sortCards(player.getCards());
        }
    }
    private void sortCards(List card) {
        card.sort(new Comparator() {
            @Override
            public int compare(Card o1, Card o2) {
                return o2.INDEX - o1.INDEX;
            }
        });
    }
    private void selectLandlord() {
        int random = (int)Math.floor(Math.random() * PLAYERS.length);
        //实际还需要询问是否要地主,不要再询问下一个玩家
        PLAYERS[random].cards.addAll(FLOOR_CARDS);
        sortCards(PLAYERS[random].getCards());
        landlord = PLAYERS[random];
    }

    public List getFLOOR_CARDS() {
        return FLOOR_CARDS;
    }

    public List getCARDS() {
        return CARDS;
    }

    public Player[] getPLAYERS() {
        return PLAYERS;
    }

    public Player getLandlord() {
        return landlord;
    }

}

 

你可能感兴趣的:(java)