模拟保皇游戏开始的发牌过程(Java)

模拟保皇游戏开始的发牌过程

问题描述:规则:4副扑克,5个玩家。1)有一个大王标记为皇上。每次发牌时,所发牌中有该大王的玩家是皇上。2)皇帝选择侍卫(也叫保儿、腿子,游戏过程中与皇帝一伙):作为皇上的玩家从自己的牌中选择一张拥有相同三张(点数和花色都相同)的一张牌(不能是2、3、大小王),其他四个玩家中有这张牌的就是侍卫。例如,皇上有三个红桃5,其他四个玩家中有红桃5的玩家就是侍卫。特殊情况是:1)皇上有一套四张牌相同的点数的牌,皇帝可以自己做侍卫;2)皇帝没有满足要求的牌,无法获得侍卫。程序要求:程序启动后生成5个玩家,并自动给他们发牌。然后输出:1)皇帝和侍卫的名字及其手里的牌(每张牌输出为“花色”+“点数”,如红桃5,牌之间用“,”分割),并按照大王、小王、2、A、K、Q、J、10、9、8、7、6、5、4、3的顺序排列,相同点数但不同花色的牌要把相同花色的牌放在一起;2)那张作为侍卫所特有的牌(“花色”+“点数”)。如果无法得到侍卫,则程序输出相应的提示。例如,程序运行后输出如下的结果:

皇帝是:玩家1

皇帝的牌是:[皇上, 小王, 小王, 小王, 小王, 方片2, 黑桃2, 黑桃A, 黑桃A, 红桃A, 方片K, 梅花K, 黑桃K, 红桃K, 梅花Q, 梅花Q, 黑桃Q, 方片J, 方片J, 方片J, 红桃J, 梅花9, 黑桃9, 黑桃9, 方片8, 梅花8, 红桃8, 梅花7, 黑桃7, 黑桃7, 红桃7, 梅花6, 梅花6, 黑桃6, 黑桃6, 方片5, 梅花5, 黑桃5, 黑桃5, 梅花4, 梅花4, 梅花4, 方片3, 红桃3]

侍卫对应的牌是:方片J

侍卫是:玩家2

侍卫的牌是:[方片2, 黑桃2, 红桃2, 方片A, 方片K, 梅花K, 梅花K, 黑桃K, 红桃K, 红桃K, 黑桃Q, 红桃Q, 方片J, 方片10, 黑桃10, 红桃10, 红桃10, 红桃10, 方片9, 红桃9, 方片8, 梅花8, 梅花8, 黑桃8, 黑桃8, 黑桃8, 红桃8, 红桃8, 方片7, 黑桃7, 黑桃7, 方片6, 黑桃6, 黑桃5, 梅花4, 黑桃4, 红桃4, 红桃4, 方片3, 梅花3, 黑桃3, 红桃3, 红桃3]

参考答案:

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

public class Main {

    public static void main(String[] args) {

        //选择pokers容器
        ArrayList<String> pokers = new ArrayList<>();

        //初始化
        initPokers(pokers);

        //洗牌
        Collections.shuffle(pokers);

        //发牌
        ArrayList<Poker>[] pokersOfGamers = new ArrayList[5];
        for (int i = 0; i < 5; i++) {
            pokersOfGamers[i] = new ArrayList<>();
        }
        boolean judgeEnd = true;
        int count = 0;
        while (judgeEnd){
            for (int i = 0; i < 5; i++) {
                pokersOfGamers[i].add(new Poker(pokers.get(count++)));
                if(count == 216){
                    judgeEnd = false;
                    break;
                }
            }
        }

        //排序
        for (int i = 0; i < 5; i++) {
            Collections.sort(pokersOfGamers[i], new PokerColorCompatator());
            Collections.sort(pokersOfGamers[i], new PokerNumComparator());
        }


        //拿牌
        Gamer[] gamers = new Gamer[5];          //玩家1~5
        for (int i = 0; i < 5; i++) {
            gamers[i] = new Gamer(pokersOfGamers[i]);
        }

        //输出皇帝
        int emperorNo = searchPrintEmperor(gamers);

        //输出侍卫
        Poker[] thePoker = searchTriPoker(gamers[emperorNo].getPokers());
        if(thePoker[1].getNo() != "NULL"){
            System.out.println("皇帝可以自己做侍卫。");
        }
        else if(thePoker[0].getNo() != "NULL"){
            System.out.print("侍卫对应的牌是:");
            System.out.println(thePoker[0].getNo());
            System.out.print("侍卫是:玩家");
            int guardNo = searchGuard(gamers, emperorNo, thePoker[0]);
            System.out.println(guardNo);
            System.out.print("侍卫的牌是:");
            System.out.println(gamers[guardNo].toString());
        }
        else{
            System.out.println("无法得到侍卫");
        }
    }

    static public void initPokers(ArrayList<String> pokers){
        for (int j = 0; j < 4; j++) {
            for(int i = 2; i < 11; ++i){
                pokers.add("红桃" + i);
                pokers.add("黑桃" + i);
                pokers.add("方块" + i);
                pokers.add("梅花" + i);
            }

            addspecial("A", pokers);
            addspecial("J", pokers);
            addspecial("Q", pokers);
            addspecial("K", pokers);
        }

        for (int i = 0; i < 4; i++) {
            pokers.add("大王");
            pokers.add("小王");
        }

        //做皇上标记
        pokers.set(pokers.indexOf("大王"), "皇上");

    }

    static public void addspecial(String tmp, ArrayList<String> pokers){
        pokers.add("红桃" + tmp);
        pokers.add("黑桃" + tmp);
        pokers.add("方块" + tmp);
        pokers.add("梅花" + tmp);
    }

    static public int searchPrintEmperor(Gamer[] gamers){
        int emperor = 0;
        for(int i = 0; i < 5; ++i){
            if(gamers[i].getRole() == "皇帝"){
                emperor = i;
                break;
            }
        }
        System.out.println("皇帝是:玩家" + (emperor + 1));
        System.out.print("皇帝的牌是:");
        System.out.println(gamers[emperor].toString());
        return emperor;
    }

    static public Poker[] searchTriPoker(ArrayList<Poker> pokers){
        Poker thePoker[] = new Poker[2];
        thePoker[0] = new Poker("NULL");
        thePoker[1] = new Poker("NULL");
        Poker thePokert = pokers.get(0);
        int count = 1;
        int flag = 0;
        for (int i = 1; i < pokers.size(); i++) {
            if(count == 3){
                flag = 1;
            }
            if(thePokert.equals(pokers.get(i))){
                count++;
                flag = 0;
            }
            else{
                if(flag == 1){
                    thePoker[0] = thePokert;
                    flag = 0;
                }
                thePokert = pokers.get(i);
                count = 1;
            }
            if(count == 4){
                thePoker[1] = thePokert;
            }

        }
        return thePoker;
    }

    static public int searchGuard(Gamer[] gamers, int emp, Poker thePoker){
        for (int i = 0; i < 5; i++) {
            if(i == emp)
                continue;
            for (int j = 0; j < gamers[i].getPokers().size(); j++) {
                if(thePoker.equals(gamers[i].getPokers().get(j))){
                    gamers[i].setRole("侍卫");
                    return i;
                }
            }
        }
        return -1;
    }
}

class Gamer{
    private String role;
    private ArrayList<Poker> pokers;

    public Gamer(ArrayList<Poker> pokers) {
        this.pokers = pokers;
        if(pokers.contains(new Poker("皇上")))
             this.role = "皇帝";
        else
            this.role = "平民";
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public ArrayList<Poker> getPokers() {
        return pokers;
    }

    @Override
    public String toString() {
        return pokers.toString();
    }

}

class Poker {
    private String no;

    public Poker(String no) {
        this.no = no;
    }

    public String getNo() {
        return no;
    }

    @Override
    public String toString() {
        return no;
    }

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

}

class PokerNumComparator implements Comparator{
    @Override
    public int compare(Object o1, Object o2) {
        Poker poker = (Poker) o1;
        Poker poker1 = (Poker) o2;
        if(poker1.getNo() == "皇上")
            return 1;
        if(poker.getNo() == "皇上")
            return -1;
        if(poker1.getNo().charAt(0) == '大' && poker.getNo().charAt(0) == '大'){
            return 0;
        }
        if(poker1.getNo().charAt(0) == '小' && poker.getNo().charAt(0) == '小'){
            return 0;
        }
        if(poker1.getNo().charAt(0) == '2' && poker.getNo().charAt(0) == '2'){
            return 0;
        }
        if(poker1.getNo().charAt(0) == 'A' && poker.getNo().charAt(0) == 'A'){
            return 0;
        }
        if(poker1.getNo().charAt(0) == 'K' && poker.getNo().charAt(0) == 'K'){
            return 0;
        }
        if(poker1.getNo().charAt(0) == 'Q' && poker.getNo().charAt(0) == 'Q'){
            return 0;
        }
        if(poker1.getNo().charAt(0) == 'J' && poker.getNo().charAt(0) == 'J'){
            return 0;
        }
        if(poker1.getNo().charAt(0) == '1' && poker.getNo().charAt(0) == '1'){
            return 0;
        }
        if(poker1.getNo().charAt(0) == '大')
            return 1;
        else if(poker.getNo().charAt(0) == '大'){
            return -1;
        }
        else if(poker1.getNo().charAt(0) == '小'){
            return 1;
        }
        else if(poker.getNo().charAt(0) == '小'){
            return -1;
        }
        else if(poker1.getNo().charAt(2) == '2'){
            return 1;
        }
        else if(poker.getNo().charAt(2) == '2'){
            return -1;
        }
        else if(poker1.getNo().charAt(2) == 'A'){
            return 1;
        }
        else if(poker.getNo().charAt(2) == 'A'){
            return -1;
        }
        else if(poker1.getNo().charAt(2) == 'K'){
            return 1;
        }
        else if(poker.getNo().charAt(2) == 'K'){
            return -1;
        }
        else if(poker1.getNo().charAt(2) == 'Q'){
            return 1;
        }
        else if(poker.getNo().charAt(2) == 'Q'){
            return -1;
        }
        else if(poker1.getNo().charAt(2) == 'J'){
            return 1;
        }
        else if(poker.getNo().charAt(2) == 'J'){
            return -1;
        }
        else if(poker1.getNo().charAt(2) == '1'){
            return 1;
        }
        else if(poker.getNo().charAt(2) == '1'){
            return -1;
        }
        else if(poker1.getNo().charAt(2) > poker.getNo().charAt(2)){
                return 1;
        }
        if(poker1.getNo().charAt(2) == poker.getNo().charAt(2))
            return 0;
        else
            return -1;
    }
}

class PokerColorCompatator implements Comparator<Poker>{
    @Override
    public int compare(Poker o1, Poker o2) {
        if (o1.getNo().charAt(0) == o2.getNo().charAt(0))
            return 0;
        return o1.getNo().charAt(0) > o2.getNo().charAt(0) ? 1 : -1;
    }
}

你可能感兴趣的:(java)