为了账号安全,请及时绑定邮箱和手机立即绑定

交作业咯~


package com.imooc.test;

import java.util.*;

public class Player {
    public int id;
    public String name;
    public static Player player=new Player();

    public List<String> playerPoker = new ArrayList<>();
    public List<String> ending = new ArrayList<>();
    public List<Integer> endingInteger = new ArrayList<>();
    public List<String> index = new ArrayList<>();
    public List<Integer> indexInteger = new ArrayList<>();

    public static List<String> pk = new ArrayList<>();
    public List<String> pkIndex = new ArrayList<>();
    public List<String> pkEnding = new ArrayList<>();
    public List<Integer> pkIndexInteger = new ArrayList<>();
    public List<Integer> pkEndingInteger = new ArrayList<>();


//    public List<Integer> pkInteger = new ArrayList<>();

    public Player(){

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

    public void lookPlayerPoker() {

        System.out.println("玩家各自的手牌为:");
        for (Player player : Start.players) {
            System.out.println(player.name + ":" + Start.lookPoker(player.playerPoker));
        }
    }

    public void comparePoker() {

        for (Player p : Start.players) {
            for (String pp : p.playerPoker) {
                String pokerString = pp;
                if (pokerString.length() == 3) {
                    p.ending.add(pokerString.substring(pokerString.length() - 1));
                } else if (pokerString.length() == 4) {
                    p.ending.add(pokerString.substring(pokerString.length() - 2));
                }
                p.index.add(pokerString.substring(0, 2));
            }
        }

        for (Player p : Start.players) {
            for (String s : index) {
                switch (s) {
                    case "方片":
                        indexInteger.add(1);
                        break;
                    case "梅花":
                        indexInteger.add(2);
                        break;
                    case "红桃":
                        indexInteger.add(3);
                        break;
                    case "黑桃":
                        indexInteger.add(4);
                        break;
                }
            }
        }

        for (Player p : Start.players) {
            for (String s : p.ending) {
                switch (s) {
                    case "2":
                    case "3":
                    case "4":
                    case "5":
                    case "6":
                    case "7":
                    case "8":
                    case "9":
                    case "10":
                        p.endingInteger.add(Integer.valueOf(s));
                        break;
                    case "J":
                        p.endingInteger.add(11);
                        break;
                    case "Q":
                        p.endingInteger.add(12);
                        break;
                    case "K":
                        p.endingInteger.add(13);
                        break;
                    case "A":
                        p.endingInteger.add(14);
                        break;
                }
            }
        }

        System.out.println("---------开始游戏-------");
        for (Player p : Start.players) {
            if (p.endingInteger.get(0) > p.endingInteger.get(1)) {
                System.out.println(p.name + "最大手牌:" + p.playerPoker.get(0));
                Player.pk.add(p.playerPoker.get(0));
            } else if (p.endingInteger.get(0) < p.endingInteger.get(1)) {
                System.out.println(p.name + "最大手牌:" + p.playerPoker.get(1));
                Player.pk.add(p.playerPoker.get(1));
            } else if (p.indexInteger.get(0) > p.indexInteger.get(1)) {
                System.out.println(p.name + "最大手牌:" + p.playerPoker.get(0));
                Player.pk.add(p.playerPoker.get(0));
            } else if (p.indexInteger.get(0) < p.indexInteger.get(1)) {
                System.out.println(p.name + "最大手牌:" + p.playerPoker.get(1));
                Player.pk.add(p.playerPoker.get(1));
            }
        }
    }

    public void winner() {

        for (Player p : Start.players) {
            for (String s : p.pk) {
                String pokerString = s;
                if (pokerString.length() == 3) {
                    p.pkEnding.add(pokerString.substring(pokerString.length() - 1));
                } else if (pokerString.length() == 4) {
                    p.pkEnding.add(pokerString.substring(pokerString.length() - 2));
                }
                p.pkIndex.add(pokerString.substring(0, 2));
            }
        }

            for (String s : pkEnding) {
                switch (s){
                    case "2":
                    case "3":
                    case "4":
                    case "5":
                    case "6":
                    case "7":
                    case "8":
                    case "9":
                    case "10":
                        pkEndingInteger.add(Integer.valueOf(s));
                        break;
                    case "J":
                        pkEndingInteger.add(11);
                        break;
                    case "Q":
                        pkEndingInteger.add(12);
                        break;
                    case "K":
                        pkEndingInteger.add(13);
                        break;
                    case "A":
                        pkEndingInteger.add(14);
                        break;
                }
        }

        for (Player p : Start.players) {
            for (String s : index) {
                switch (s) {
                    case "方片":
                        pkIndexInteger.add(1);
                        break;
                    case "梅花":
                        pkIndexInteger.add(2);
                        break;
                    case "红桃":
                        pkIndexInteger.add(3);
                        break;
                    case "黑桃":
                        pkIndexInteger.add(4);
                        break;
                }
            }
        }

            if (pkEndingInteger.get(0) > pkEndingInteger.get(1)) {
                System.out.println("玩家:"+Start.players.get(0).name+"获胜");
            } else if (pkEndingInteger.get(0) < pkEndingInteger.get(1)) {
                System.out.println("玩家:"+Start.players.get(1).name+"获胜");
            } else if (pkIndexInteger.get(0) > pkIndexInteger.get(1)) {
                System.out.println("玩家:"+Start.players.get(0).name+"获胜");
            } else if (pkIndexInteger.get(0) < pkIndexInteger.get(1)) {
                System.out.println("----------------玩家:"+Start.players.get(1).name+"获胜-------------");
            }
    }

}




package com.imooc.test;

import java.util.*;

public class Start {

    public static List<String> pokers = new ArrayList<>();
    public static String string = "";
    public static String[] colors = {"方片", "梅花", "红桃", "黑桃"};
    public static List<Player> players = new ArrayList<>();


    public static String lookPoker(List<String> pokers) {
        Object[] pokerArrays = pokers.toArray();
        return Arrays.toString(pokerArrays);
    }

    public static List<String> shuffleCards() {
        System.out.println("------------开始洗牌--------------");
        Random random = new Random();
        List<String> pokersPack = new ArrayList<>();
        int index = 0;
        while (pokersPack.size() < pokers.size()) {
            index = random.nextInt(pokers.size());
            if (!pokersPack.contains(pokers.get(index))) {
                pokersPack.add(pokers.get(index));
            }
        }
        System.out.println("----------洗牌结束-----------");
        return pokersPack;
    }

    public static String createPoker() {

        System.out.println("---------------创建扑克牌----------------");
        for (String color : colors) {
            for (int i = 2; i <= 10; i++) {
                string = color + i;
                pokers.add(string);
            }
            char[] chars = {'J', 'Q', 'K', 'A'};
            for (char cha : chars) {
                string = color + cha;
                pokers.add(string);
            }
        }
        System.out.println("-------------------扑克牌创建成功------------------------");
        return lookPoker(pokers);
    }

    public static void inputPlayer() {
        System.out.println("--------------创建玩家------------");

        for (int i = 1; i <= 2; i++) {
            System.out.println("请输入第" + i + "位玩家的ID和姓名:");
            System.out.println("输入ID:");
            Scanner scanner = new Scanner(System.in);
            try {
                int id = new Scanner(System.in).nextInt();
                System.out.println("请输入姓名:");
                String name = scanner.next();
                Player player = new Player(id, name);
                players.add(player);
            } catch (InputMismatchException e) {
                System.out.println("请输入整数类型的ID!");
                inputPlayer();
                break;
            }
        }
    }

    public static void outPlayer() {
        for (Player player : players) {
            System.out.println("-----欢迎玩家:" + player.name);
        }
    }

    public static void takePoker() {
        Random random = new Random();
        boolean bool2 = false;
        System.out.println("------------开始发牌------------");
        for (int i = 0; i < players.size(); ) {
            for (Player player : players) {
                int index = random.nextInt(pokers.size());
                boolean bool = !player.playerPoker.contains(pokers.get(index));
                System.out.println("----------玩家:" + player.name + "拿牌");
                if (bool) {
                    bool2 = bool;
                    player.playerPoker.add(pokers.get(index));
                }
            }
            if (bool2)
                i++;
        }
        System.out.println("----------发牌结束---------");
    }


    public static void main(String[] args) {
        System.out.println(createPoker());
        pokers = shuffleCards();
        inputPlayer();
        outPlayer();
        takePoker();
        players.get(0).comparePoker();
        players.get(0).lookPlayerPoker();
        players.get(0).winner();
    }
}


正在回答

举报

0/150
提交
取消
Java入门第三季
  • 参与学习       409789    人
  • 解答问题       4340    个

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

进入课程

交作业咯~

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信