java 控制台斗地主 demo代码

扑克牌实体类
/**
 * 描述:
 * 实体类 扑克牌
 * @author 闲走天涯
 * @create 2021/3/17 17:09
 */
public class Pai  implements Comparable<Pai>{
   
    private String name ;
    private String num ;
    private int id ;

    public Pai(String name, String num, int id) {
   
        this.name = name;
        this.num = num;
        this.id = id;
    }

    public Pai() {
   
    }

    public String getName() {
   
        return name;
    }

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

    public String getNum() {
   
        return num;
    }

    public void setNum(String num) {
   
        this.num = num;
    }

    public int getId() {
   
        return id;
    }

    public void setId(int id) {
   
        this.id = id;
    }

    @Override
    public String toString() {
   
        return name+num;
    }


    @Override
    public int compareTo(Pai o) {
   
        return this.getId()-o.getId();
    }
}

逻辑代码

/**
 * 描述:
 * 控制台斗地主代码
 * @author 闲走天涯
 * @create 2021/3/17 17:05
 */
public class Doudizhu {
   
    public static void main(String[] args) {
   
        Scanner sc = new Scanner(System.in);
        String[] huashe1 = {
   "♥", "♠", "♦", "♣"};
        int[] daxiao1 = {
   3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
        String[] pai1 = {
   "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        ArrayList<Pai> list = new ArrayList<>();
        for (String s : huashe1) {
   
            for (int i = 0; i < daxiao1.length; i++) {
   
                list.add(new Pai(s, pai1[i], daxiao1[i]));
            }
        }
        list.add(new Pai("", "大王", 50));
        list.add(new Pai("", "小王", 20));
        Collections.shuffle((List<?>) list);
        ArrayList<Pai> player1 = new ArrayList<>();
        ArrayList<Pai> player2 = new ArrayList<>();
        ArrayList<Pai> player3 = new ArrayList<>();
        ArrayList<Pai> under = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
   
            if (i >= list.size() - 3) {
   
                under.add(list.get(i));
            } else {
   
                if (i % 3 == 0) {
   
                    player1.add(list.get(i));
                } else if (i % 3 == 1) {
   
                    player2.add(list.get(i));
                } else {
   
                    player3.add(list.get(i));
                }
            }
        }
        System.out.println(under);
        int r = new Random().nextInt(51);
        if (r % 3 == 0) {
   
            player1.addAll(under);
        } else if (r % 3 == 1) {
   
            player2.addAll(under);
        } else {
   
            player3.addAll(under);
        }
        Collections.sort(player1);
        Collections.sort(player2);
        Collections.sort(player3);
        System.out.println(player1);
        System.out.println(player2);
        System.out.println(player3);
        ArrayList<Pai> paikuplayer1 = new ArrayList<>();
        ArrayList<Pai> paikuplayer2 = new ArrayList<>();
        ArrayList<Pai> paikuplayer3 = new ArrayList<>();
        ArrayList<Pai> paikuunderp1 = new ArrayList<>();
        ArrayList<Pai> paikuunderp2 = new ArrayList<>();
        ArrayList<Pai> paikuunderp3 = new ArrayList<>();
        ArrayList<Pai> paikuunder = new ArrayList<>();
        String p1;
        String p2;
        String p3;
        if (player1.size() == 20) {
   
            p1 = "地主";
            p2 = "玩家一";
            p3 = "玩家二";
        } else if (player2.size() == 20) {
   
            p1 = "玩家二";
            p2 = "地主";
            p3 = "玩家一";
        } else {
   
            p1 = "玩家一";
            p2 = "玩家二";
            p3 = "地主";
        }
        if (player1.size() != 20) {
   
        } else {
   
            System.out.println("==================");
            System.out.println(p1 + "的牌为" + player1);
            System.out.println("请" + p1 + "出牌");
            System.out.println("请输入想要出的牌,输入-1停止出牌");
            while (true) {
   
                if (paikuunderp2.size() == 0 && paikuunderp3.size() == 0) {
   
                    paikuunderp1.clear();
                } else {
   
                    paikuunderp1.clear();
                    System.out.println("请选择,1.出牌 2.要不起 3.过");
                    if (sc.nextLine().equals("2")) {
   
                        System.out.println("要不起");
                        break;
                    } else if (sc.nextLine().equals("3")) {
   
                        System.out.println("过");
                        break;
                    }
                }
                while (true) {
   
                    System.out.println("请输入要出的牌");
                    String a = sc.nextLine();
                    if (a.equals("-1")) {
   
                        break;
                    } else {
   
                        if (!isTrue(player1, a)) {
   
                            System.out.println("您没有这张牌");
                        } else {
   
                            Pai pai = throwcard(player1, a);
                            paikuplayer1.add(pai);
                            player1.remove(pai);
                        }
                    }
                }
                System.out.println("出的牌为" + paikuplayer1);
                if (chuPai(paikuplayer1)) {
   
                    System.out.println("可以出牌");
                    if (daNi(paikuplayer1, paikuunderp2) && daNi(paikuplayer1, paikuunderp3)) {
   
                        paikuunderp1.addAll(paikuplayer1);
                        paikuunder.addAll(paikuplayer1);
                        paikuplayer1.clear();
                        System.out.println(p1 + "出的牌为" + paikuunderp1);
                        paikuunderp3.clear();
                        break;
                    }
                }
                System.out.println("不符合规则,请重新出牌");
                player1.addAll(paikuplayer1);
                Collections.sort(player1);
                System.out.println("当前牌库为" + player1);
                paikuplayer1.clear();
            }
            System.out.println("底库中牌为" + paikuunder);
        }
        if (player2.size() != 20 && paikuunderp1.size() == 0) {
   
        } else {
   
            System.out.println("==================");
            System.out.println(p2 + "的牌为" 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值