24点游戏


一、游戏内容

程序设计要求:
24点游戏是经典的纸牌益智游戏。
常见游戏规则:
从扑克中每次取出4张牌。使用加减乘除,第一个能得出24者为赢。(其中,J代表11,Q代表12,K代表13,A代表1),按照要求编程解决24点游戏。
基本要求: 随机生成4个代表扑克牌牌面的数字字母,程序自动列出所有可能算出24的表达式,用擅长的语言(C/C++/Java或其他均可)实现程序解决问题。
1.程序风格良好(使用自定义注释模板)
2.列出表达式无重复。

二、源代码

import java.util.*;
public class game {
    static int number[] = new int[4];
    static int m[]=new int [4];
    static String n[] = new String[4];
    static boolean flag = false;
    static char[] operator = { '+', '-', '*', '/' };
    private static Object key;
    public static void main(String[] args){
        Random rand = new Random();
        System.out.println("下列给出四个数字,使用+,-,*,/进行计算使最后计算结果为24");
        for(int i=0;i<4;i++){
            number[i]=rand.nextInt(13)+1;
            if(number[i]==1){
                System.out.println("A");
            }
            else if(number[i]==11){
                System.out.println("J");
            }
            else if(number[i]==12){
                System.out.println("Q");
            }
            else if(number[i]==13){
                System.out.println("K");
            }
            else
                System.out.println(number[i]);
        }
        System.out.println("可能的结果有:");
        calculate();

    }
    public static int calcute(int count1, int count2, char operator) {
        if (operator == '+') {
            return count1 + count2;
        }
        else if (operator == '-') {
            return count1 - count2;
        }
        else if (operator == '*') {
            return count1 * count2;
        }
        else if ((operator == '/' )&& (count2 != 0) && (count1%count2==0)) {
            return count1 / count2;
        }
        else {
            return -1;
        }
    }
    public static void calculate(){
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < number.length; i++) {
            if(map.get(number[i]) == null){
                map.put(number[i], 1);
            }
            else {
                map.put(number[i], map.get(number[i]) + 1);
            }
        }
        if(map.size() == 1){
            calculation(number[0], number[1],number[2],number[3]);
        }
        else if(map.size()==2){
            int index = 0;
            int state = 0;
            for (Integer key : map.keySet()) {
                if(map.get(key) == 1){
                    number[3] = key;
                    state = 1;
                }
                else if(map.get(key)==2){
                    number[index++]=key;
                    number[index++]=key;
                }
                else{
                    number[index++]=key;
                }
            }
            if(state == 1){
                calculation(number[3],number[1],number[1],number[1]);
                calculation(number[1],number[3],number[1],number[1]);
                calculation(number[1],number[1],number[3],number[1]);
                calculation(number[1],number[1],number[1],number[3]);
            }
            if(state==0){
                calculation(number[1],number[1],number[3],number[3]);
                calculation(number[1],number[3],number[1],number[3]);
                calculation(number[1],number[3],number[3],number[1]);
                calculation(number[3],number[3],number[1],number[1]);
                calculation(number[3],number[1],number[3],number[1]);
                calculation(number[3],number[1],number[1],number[3]);
            }
        }
        else if(map.size()==3){
            int index = 0;
            for (Integer key : map.keySet()) {
                if(map.get(key) == 2){
                    number[2] = key;
                    number[3] = key;
                }
                else {
                    number[index++] = key;
                }
            }
            calculation(number[0],number[1],number[3],number[3]);
            calculation(number[0],number[3],number[1],number[3]);
            calculation(number[0],number[3],number[3],number[1]);
            calculation(number[1],number[0],number[3],number[3]);
            calculation(number[1],number[3],number[0],number[3]);
            calculation(number[1],number[3],number[3],number[0]);
            calculation(number[3],number[3],number[0],number[1]);
            calculation(number[3],number[3],number[1],number[0]);
            calculation(number[3],number[1],number[3],number[0]);
            calculation(number[3],number[0],number[3],number[1]);
            calculation(number[3],number[0],number[1],number[3]);
            calculation(number[3],number[1],number[0],number[3]);
        }
        else if(map.size() == 4){
            calculation(number[0],number[1],number[2],number[3]);
            calculation(number[0],number[1],number[3],number[2]);
            calculation(number[0],number[2],number[1],number[3]);
            calculation(number[0],number[2],number[3],number[1]);
            calculation(number[0],number[3],number[1],number[2]);
            calculation(number[0],number[3],number[2],number[1]);
            calculation(number[1],number[0],number[2],number[3]);
            calculation(number[1],number[0],number[3],number[2]);
            calculation(number[1],number[2],number[3],number[0]);
            calculation(number[1],number[2],number[0],number[3]);
            calculation(number[1],number[3],number[0],number[2]);
            calculation(number[1],number[3],number[2],number[0]);
            calculation(number[2],number[0],number[1],number[3]);
            calculation(number[2],number[0],number[3],number[1]);
            calculation(number[2],number[1],number[0],number[3]);
            calculation(number[2],number[1],number[3],number[0]);
            calculation(number[2],number[3],number[0],number[1]);
            calculation(number[2],number[3],number[1],number[0]);
            calculation(number[3],number[0],number[1],number[2]);
            calculation(number[3],number[0],number[2],number[1]);
            calculation(number[3],number[1],number[0],number[2]);
            calculation(number[3],number[1],number[2],number[0]);
            calculation(number[3],number[2],number[0],number[1]);
            calculation(number[3],number[2],number[1],number[0]);
        }
        if(flag==false)
            System.out.println("这四张牌面数字无法经过运算得到24!");
    }
    public static void calculation(int num1, int num2, int num3, int num4){
        for (int i = 0; i < 4; i++){
            char operator1 = operator[i];
            int firstResult = calcute(num1, num2, operator1);
            int midResult = calcute(num2, num3, operator1);
            int tailResult = calcute(num3,num4, operator1);
            for (int j = 0; j < 4; j++){
                char operator2 = operator[j];
                int firstMidResult = calcute(firstResult, num3, operator2);
                int firstTailResult = calcute(num3,num4,operator2);
                int midFirstResult = calcute(num1, midResult, operator2);
                int midTailResult= calcute(midResult,num4,operator2);
                int tailMidResult = calcute(num2, tailResult, operator2);
                for (int k = 0; k < 4; k++){
                    char operator3 = operator[k];
                    if(calcute(firstMidResult, num4, operator3) == 24){
                        m[0]=num1;
                        m[1]=num2;
                        m[2]=num3;
                        m[3]=num4;
                        for(int p=0;p<4;p++){
                            if(m[p]==1){
                                n[p]="A";}
                            if(m[p]==2){
                                n[p]="2";}
                            if(m[p]==3){
                                n[p]="3";}
                            if(m[p]==4){
                                n[p]="4";}
                            if(m[p]==5){
                                n[p]="5";}
                            if(m[p]==6){
                            n[p]="6";}
                            if(m[p]==7){
                                n[p]="7";}
                            if(m[p]==8){
                                n[p]="8";}
                            if(m[p]==9){
                                n[p]="9";}
                            if(m[p]==10){
                                n[p]="10";}
                            if(m[p]==11){
                                n[p]="J";}
                            if(m[p]==12){
                                n[p]="Q";}
                            if(m[p]==13){
                                n[p]="k";}
                        }
                        System.out.println("((" + n[0] + operator1 + n[1] + ")" + operator2 + n[2] + ")" + operator3 + n[3]);
                        flag = true;
                    }
                    if(calcute(firstResult, firstTailResult, operator3) == 24){
                        System.out.println("(" + n[0] + operator1 + n[1] + ")" + operator3 + "(" + n[2] + operator2 + n[3] + ")");
                        flag = true;
                    }
                    if(calcute(midFirstResult, num4, operator3) == 24){
                        m[0]=num1;
                        m[1]=num2;
                        m[2]=num3;
                        m[3]=num4;
                        for(int p=0;p<4;p++){
                            if(m[p]==1){
                                n[p]="A";}
                            if(m[p]==2){
                                n[p]="2";}
                            if(m[p]==3){
                                n[p]="3";}
                            if(m[p]==4){
                                n[p]="4";}
                            if(m[p]==5){
                                n[p]="5";}
                            if(m[p]==6){
                                n[p]="6";}
                            if(m[p]==7){
                                n[p]="7";}
                            if(m[p]==8){
                                n[p]="8";}
                            if(m[p]==9){
                                n[p]="9";}
                            if(m[p]==10){
                                n[p]="10";}
                            if(m[p]==11){
                                n[p]="J";}
                            if(m[p]==12){
                                n[p]="Q";}
                            if(m[p]==13){
                                n[p]="k";}
                        }
                        System.out.println("(" + n[0] + operator2 + "(" + n[1] + operator1 + n[2] + "))" + operator3 + n[3]);
                        flag = true;
                    }
                    if(calcute(num1,midTailResult, operator3) == 24){
                        m[0]=num1;
                        m[1]=num2;
                        m[2]=num3;
                        m[3]=num4;
                        for(int p=0;p<4;p++){
                            if(m[p]==1){
                                n[p]="A";}
                            if(m[p]==2){
                                n[p]="2";}
                            if(m[p]==3){
                                n[p]="3";}
                            if(m[p]==4){
                                n[p]="4";}
                            if(m[p]==5){
                                n[p]="5";}
                            if(m[p]==6){
                                n[p]="6";}
                            if(m[p]==7){
                                n[p]="7";}
                            if(m[p]==8){
                                n[p]="8";}
                            if(m[p]==9){
                                n[p]="9";}
                            if(m[p]==10){
                                n[p]="10";}
                            if(m[p]==11){
                                n[p]="J";}
                            if(m[p]==12){
                                n[p]="Q";}
                            if(m[p]==13){
                                n[p]="k";}
                        }
                        System.out.println(" " + n[0] + operator3 + "((" + n[1] + operator1 + n[2] + ")" + operator2 + n[3] + ")");
                        flag = true;
                    }
                    if(calcute(num1,tailMidResult,operator3) == 24){
                        m[0]=num1;
                        m[1]=num2;
                        m[2]=num3;
                        m[3]=num4;
                        for(int p=0;p<4;p++){
                            if(m[p]==1){
                                n[p]="A";}
                            if(m[p]==2){
                                n[p]="2";}
                            if(m[p]==3){
                                n[p]="3";}
                            if(m[p]==4){
                                n[p]="4";}
                            if(m[p]==5){
                                n[p]="5";}
                            if(m[p]==6){
                                n[p]="6";}
                            if(m[p]==7){
                                n[p]="7";}
                            if(m[p]==8){
                                n[p]="8";}
                            if(m[p]==9){
                                n[p]="9";}
                            if(m[p]==10){
                                n[p]="10";}
                            if(m[p]==11){
                                n[p]="J";}
                            if(m[p]==12){
                                n[p]="Q";}
                            if(m[p]==13){
                                n[p]="k";}
                        }
                        System.out.println(" " + n[0] + operator3 + "(" + n[1] + operator2 + "(" + n[2] + operator1 + n[3] + "))");
                        flag = true;
                    }
                }
            }
        }
    }
}

三、流程图

在这里插入图片描述


总结

在此次代码编写的过程中,遇到了一些问题,通过查阅网络解决了问题,而且在今后的Java代码编写过程中应该多创建类。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值