java扑克牌小游戏

开发软件:idea

1.主函数内容

public static void main(String[] args) {

        System.out.println("欢迎光临");
        Scanner s = new Scanner(System.in);
        System.out.println("请输入总数字:");
        userAMoney = s.nextInt();
        userBMoney = userAMoney;
        int currentMoney = 0;
        while (userAMoney > 0 && userBMoney > 0) {
            System.out.println("请输入本局数字:");
            currentMoney = s.nextInt();
            if (currentMoney <= 0 || currentMoney > userAMoney || currentMoney > userBMoney)
            {
                System.out.println("电脑或你的数字不足,请重新输入!");
                continue;
            }
            dispatchCards();
            printCards(twoHandsCards);
            if (compare() > 0) {
                userAMoney += currentMoney;
                userBMoney -= currentMoney;
                System.out.println("玩家获胜!");
            } else if (compare() < 0) {
                userAMoney -= currentMoney;
                userBMoney += currentMoney;
                System.out.println("电脑获胜!");
            } else {
                System.out.println("平局!");
            }
            System.out.println("玩家数字:" + userAMoney + ";电脑数字:" + userBMoney);
        }
        if (userAMoney <= 0) {
            System.out.println("你输光数字了");
        } else {
            System.out.println("电脑已经输光数字了");
        }

    }

2.下面是调牌的方法

    public static void dispatchCards() {
        int i = 0;
        while (i < twoHandsCards.length) {
            int h = (int) (Math.random() * 4) + 1;
            int d = (int) (Math.random() * 13) + 2;
            int j = 0;
            for (; j < i; j++) {
                if (twoHandsCards[j] == h * 100 + d) {
                    break;
                }
            }
            if (j < i) {
                continue;
            } else {
                twoHandsCards[i++] = h * 100 + d;
            }
        }
    }

3.下面的代码是处理扑克花色还有特殊扑克牌

public static void printCards(int[] oneHandCards) {
        for (int i = 0; i < oneHandCards.length; i++) {
            if (i == 0) {
                System.out.print("你的牌:");
            }
            if (i == 3) {
                System.out.print("\t电脑的牌:");
            }
            switch (oneHandCards[i] / 100) {
                case 1:
                    System.out.print("♠");
                    break;
                case 2:
                    System.out.print("♥");
                    break;
                case 3:
                    System.out.print("♣");
                    break;
                case 4:
                    System.out.print("♦");
                    break;
            }
            if (oneHandCards[i] % 100 <= 10) {
                System.out.print(oneHandCards[i] % 100);
            } else {
                switch (oneHandCards[i] % 100) {
                    case 11:
                        System.out.print("J");
                        break;
                    case 12:
                        System.out.print("Q");
                        break;
                    case 13:
                        System.out.print("K");
                        break;
                    case 14:
                        System.out.print("A");
                        break;
                }
            }
            System.out.print("  ");
        }
        System.out.println();
    }

4.点数数比较

 public static int compare() {
        int[] aCards = new int[3];
        int[] bCards = new int[3];
        for (int i = 0; i < 3; i++) {
            aCards[i] = twoHandsCards[i];
        }
        for (int i = 3; i < 6; i++) {
            bCards[i - 3] = twoHandsCards[i];
        }
        return fromCardsToNumber(aCards) - fromCardsToNumber(bCards);
    }

5.核心(可以按照自己的想法编写程序)

public static int fromCardsToNumber(int[] oneHandCards) {
        for (int i = 0; i < oneHandCards.length - 1; i++) {
            for (int j = i + 1; j < oneHandCards.length; j++) {
                if (oneHandCards[i] % 100 < oneHandCards[j] % 100) {
                    int temp = oneHandCards[i];
                    oneHandCards[i] = oneHandCards[j];
                    oneHandCards[j] = temp;
                }
            }
        }
        int num = 0;
        if (oneHandCards[0] % 100 == oneHandCards[1] % 100
                && oneHandCards[1] % 100 == oneHandCards[2] % 100) {
            num = 6 * 1000000 + oneHandCards[0] % 100 * 10000 + oneHandCards[1]
                    % 100 * 100 + oneHandCards[2] % 100;
        } else if (oneHandCards[0] / 100 == oneHandCards[1] / 100
                && oneHandCards[1] / 100 == oneHandCards[2] / 100
                && oneHandCards[0] % 100 == oneHandCards[1] % 100 + 1
                && oneHandCards[1] % 100 == oneHandCards[2] % 100 + 1) {
            num = 5 * 1000000 + oneHandCards[0] % 100 * 10000 + oneHandCards[1]
                    % 100 * 100 + oneHandCards[2] % 100;
        } else if (oneHandCards[0] / 100 == oneHandCards[1] / 100
                && oneHandCards[1] / 100 == oneHandCards[2] / 100) {
            num = 4 * 1000000 + oneHandCards[0] % 100 * 10000 + oneHandCards[1]
                    % 100 * 100 + oneHandCards[2] % 100;
        } else if (oneHandCards[0] % 100 == oneHandCards[1] % 100 + 1
                && oneHandCards[1] % 100 == oneHandCards[2] % 100 + 1) {
            num = 3 * 1000000 + oneHandCards[0] % 100 * 10000 + oneHandCards[1]
                    % 100 * 100 + oneHandCards[2] % 100;
        } else if (oneHandCards[0] % 100 == oneHandCards[1] % 100) {
            num = 2 * 1000000 + oneHandCards[0] % 100 * 10000 + oneHandCards[1]
                    % 100 * 100 + oneHandCards[2] % 100;
        } else if (oneHandCards[1] % 100 == oneHandCards[2] % 100) {
            num = 2 * 1000000 + oneHandCards[1] % 100 * 10000 + oneHandCards[2]
                    % 100 * 100 + oneHandCards[0] % 100;
        } else {
            num = 1 * 1000000 + oneHandCards[0] % 100 * 10000 + oneHandCards[1]
                    % 100 * 100 + oneHandCards[2] % 100;
        }           return num;
    }

整体代码参考请添加图片描述

效果截图

请添加图片描述

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值