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

简易扑克牌游戏(两位玩家)

标签:
Java
public class Poker{
    private String point;
    private String color;
    private int colorpoint;

    public Poker() {}
    public Poker(String point, String color) {
        super();
        this.point = point;
        this.color = color;
    }

    public int getColorpoint() {
        return colorpoint;
    }
    public void setColorpoint(String color) {
        if (color.equals("黑桃")) {
            this.colorpoint = 1;
        }else if (color.equals("红桃")) {
            this.colorpoint = 2;
        }else if (color.equals("梅花")) {
            this.colorpoint = 3;
        }else{
            this.colorpoint = 4;
        }
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getPoint() {
        return point;
    }
    public void setPoint(String point) {
        this.point = point;
    }

    @Override
    public String toString() {
        return color+point;
    }
}
public class Player {
    private int id = -1 ;
    private String name;
    private ArrayList<Poker> pokers = new ArrayList<>();

    public Player() {}

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

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public ArrayList<Poker> getPokers() {
        return pokers;
    }

    public void setPokers(ArrayList<Poker> pokers) {
        this.pokers = pokers;
    }

}   
public class createPoker {
    ArrayList<Poker> pokerList = new ArrayList<>();

    public createPoker(){
        Poker poker = null;
        String[] colors = {"黑桃" , "红桃" , "梅花" , "方片" };
        String[] points = {"1","2","3","4","5","6","7","8","9","J","P","K","A"};
        for(int i = 0 ; i < 4 ; i++){
            for (int j = 0; j < points.length; j++) {
                poker = new Poker(points[j] , colors[i]);
                pokerList.add(poker);
            }
        }
    }

    public ArrayList<Poker> getPokerList() {
        return pokerList;
    }

}
public class createPlayer {
    int id = -1 ;
    String name;
    public createPlayer(){
        while (true) {
            try {
                Scanner console = new Scanner(System.in);
                System.out.println("请输入玩家id:");
                id = console.nextInt();
                System.out.println("请输入玩家名称:");
                name = console.next();
                break;
            } catch (Exception e) {
                if ( id == -1) {
                    System.out.println("输入字符不符合要求,请重新输入整数:");
                }
            }
        }

    }

}
public class pokerComparator implements Comparator<Poker>{
    @Override
    public int compare(Poker poker1, Poker poker2) {
        if (poker1.getPoint().compareTo(poker2.getPoint()) != 0) {
            return poker1.getPoint().compareTo(poker2.getPoint());
        }else {
            int colorpoint1 = poker1.getColorpoint();
            int  colorpoint2 = poker2.getColorpoint();

            if (colorpoint1 == colorpoint2) {
                return 0;
            }else if (colorpoint1 > colorpoint2) {
                return 1;
            }else {
                return -1;
            }
        }
    }
}
public class playGame {
    public static void main(String[] args) {    
            //创建一副新的扑克牌
        createPoker createPoker = new createPoker();
        System.out.println("创建一副新的扑克牌为:");
        for (Poker p : createPoker.getPokerList()) {
            System.out.print("   [" + p + "] ");
        }

            //洗牌,打乱扑克牌的顺序
        Collections.shuffle(createPoker.getPokerList());
        System.out.println();
        /*
            for (Poker p : createPoker.getPokerList()) {
                System.out.print("   [" + p + "] ");
            }
        */
        System.out.println("洗扑克牌成功");

            //创建两名玩家
        createPlayer createPlayer1 = new createPlayer();
        Player player1 = new Player(createPlayer1.id,createPlayer1.name);
        createPlayer createPlayer2 = new createPlayer();
        Player player2 = new Player(createPlayer2.id,createPlayer2.name);

            //发牌
        ArrayList<Poker> pokers1 = new ArrayList<>();
        pokers1.add(createPoker.pokerList.get(0));
        pokers1.add(createPoker.pokerList.get(2));

        ArrayList<Poker> pokers2 = new ArrayList<>();
        pokers2.add(createPoker.pokerList.get(1));
        pokers2.add(createPoker.pokerList.get(3));

        player1.setPokers(pokers1);
        player2.setPokers(pokers2);

        Collections.sort(pokers1,new pokerComparator());
        Poker player1max = pokers1.get(1);
        System.out.println("第一位玩家" + player1.getName() + "手中较大的牌微:" + player1max);

        Collections.sort(pokers2,new pokerComparator());
        Poker player2max = pokers2.get(1);
        System.out.println("第二位玩家" + player2.getName() + "手中较大的牌微:" + player2max);

        if (player1max.getPoint() == player2max.getPoint()){
            if (player1max.getColorpoint() == player2max.getColorpoint()) {
                System.out.println("==============两位玩家打成平手============");
            }else if (player1max.getColorpoint() > player2max.getColorpoint()) {
                System.out.println("============玩家" + player1.getName() + "赢了===========");
            }else {
                System.out.println("============玩家" + player2.getName() + "赢了===========");
            }
        }else if (  (player1max.getPoint()).compareTo(  (player2max.getPoint())) > 0){
                System.out.println("============玩家" + player1.getName() + "赢了===========");
        }else {
            System.out.println("=============玩家" + player2.getName() + "赢了============");
        }

        System.out.println("玩家" + player1.getName() + "手中的牌为:" );
        for (Poker poker : pokers1) {
            System.out.print("   " + poker);
        }

        System.out.println("玩家" + player2.getName() + "手中的牌为:" );
        for (Poker poker : pokers2) {
            System.out.print("   " + poker);
        }
    }
}
点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消