24ポイントカードゲーム(Java実装)



24 Point Card Game



まず、トピックの要件

The 24 o'clock game is a classic card puzzle game. Common game rules: Take 4 cards from poker each time. Use addition, subtraction, multiplication and division, the first one can get 24 to win. (Where J stands for 11, Q stands for 12, K stands for 13, and A stands for 1), and the 24-point game is programmed as required. Basic Requirements: Randomly generate 4 alphanumeric characters representing the playing card face. The program automatically lists all expressions that may be calculated as 24.

第二に、アルゴリズムの設計

アルゴリズムのフローチャート:
画像
基本部分のアルゴリズム設計は、主に4つの数値の計算結果で設計されています。プログラムは、2つの数値の計算を通じて4つの数値の結果を計算するため、4つの数値が「+」、「-」、「*」、「/」によって満たされるかどうかが検証され、4つの算術演算で24の結果が得られます。

第三に、アルゴリズムが実装されています

import java.util.HashMap import java.util.Map import java.util.Random /** * Class Solution contains a variety of static variables and static methods to facilitate program writing, this class is used to solve the basic part of the 24 point game * * method public static void main(String[] args) is the main method * method public static int calcute(int count1, int count2, char operator) is a calculation method given two numbers and one operator * method public static void calculate() is a way to find a 24-point game * method public static void calculation(int num1, int num2, int num3, int num4) is the method of printing the result and finding the result of each other * * data: April 12, 2019 * * @author Bao Haijin * @version 1.0.0 * */ public class Solution { / / Define four numbers generated randomly static int data[] = new int[4] // converted num1, num2, num3, num4 static int reData[]=new int [4] static String dataString[] = new String[4] / / used to determine whether there is a solution static boolean flag = false / / store operator static char[] operator = { '+', '-', '*', '/' } private static Object key /** * program main method * @param args * */ public static void main(String[] args){ Random rand = new Random() System.out.println('The following four numbers are given, using +, -, *, / to calculate the final result as 24') for(int i=0i<4i++){ data[i]=rand.nextInt(13)+1/ / Randomly generate four int numbers if(data[i]==1){ System.out.println('A')/ / If the randomly generated number is 1, it is displayed as A in the poker face } else if(data[i]==11){ System.out.println('J')//If the randomly generated number is 11, it is displayed as J in the playing card face. } else if(data[i]==12){ System.out.println('Q')/ / If the randomly generated number is 12, it is displayed as Q in the poker face } else if(data[i]==13){ System.out.println('K')//If the randomly generated number is 13, it is displayed as K in the poker face. } else System.out.println(data[i]) } System.out.println('The possible outcomes are:') calculate() } /** * method public static int calcute(int count1, int count2, char operator) is a calculation method given two numbers and one operator * @param count1 operand one * @param count2 operand two * @param operator operator * @return returns the result of the calculation * */ 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 } } /** * method public static void calculate() is a way to find a 24-point game * */ public static void calculate(){ Map<Integer, Integer> map = new HashMap<Integer, Integer>() / / Store the number, used to determine the number of the input of the four of the repeated, and repeated cases for (int i = 0 i < data.length i++) { if(map.get(data[i]) == null){ map.put(data[i], 1) } else { map.put(data[i], map.get(data[i]) + 1) } } if(map.size() == 1){ //If there is only one type, there is only one sorting combination, such as 5, 5, 5, 5 calculation(data[0], data[1],data[2],data[3]) } else if(map.size()==2){ //If there are only 2 numbers, there are 2 cases, such as 1, 1, 2, 2 and 1, 1, 1, 2 int index = 0/ / for data processing int state = 0/ / Determine which is the case for (Integer key : map.keySet()) { if(map.get(key) == 1){ //If there is one number and the other three are different, turn data into data[0]=data[1]=data[2], / / Put the different one into data[3], easy to calculate data[3] = key state = 1 } else if(map.get(key)==2){ / / If the same two or two cases, the data becomes data[0] = data[1], data[2] = data[3] data[index++]=key data[index++]=key } else{ data[index++]=key } } / / List all the permutations and combinations of the two cases, and calculate separately if(state == 1){ calculation(data[3],data[1],data[1],data[1]) calculation(data[1],data[3],data[1],data[1]) calculation(data[1],data[1],data[3],data[1]) calculation(data[1],data[1],data[1],data[3]) } if(state==0){ calculation(data[1],data[1],data[3],data[3]) calculation(data[1],data[3],data[1],data[3]) calculation(data[1],data[3],data[3],data[1]) calculation(data[3],data[3],data[1],data[1]) calculation(data[3],data[1],data[3],data[1]) calculation(data[3],data[1],data[1],data[3]) } } else if(map.size()==3){ //There are 3 kinds of numbers int index = 0 for (Integer key : map.keySet()) { if(map.get(key) == 2){ / / Put the same 2 numbers into data[2]=data[3] data[2] = key data[3] = key } else { data[index++] = key } } / / Arrange the combination, all situations calculation(data[0],data[1],data[3],data[3]) calculation(data[0],data[3],data[1],data[3]) calculation(data[0],data[3],data[3],data[1]) calculation(data[1],data[0],data[3],data[3]) calculation(data[1],data[3],data[0],data[3]) calculation(data[1],data[3],data[3],data[0]) calculation(data[3],data[3],data[0],data[1]) calculation(data[3],data[3],data[1],data[0]) calculation(data[3],data[1],data[3],data[0]) calculation(data[3],data[0],data[3],data[1]) calculation(data[3],data[0],data[1],data[3]) calculation(data[3],data[1],data[0],data[3]) } else if(map.size() == 4){ //4 cases are different calculation(data[0],data[1],data[2],data[3]) calculation(data[0],data[1],data[3],data[2]) calculation(data[0],data[2],data[1],data[3]) calculation(data[0],data[2],data[3],data[1]) calculation(data[0],data[3],data[1],data[2]) calculation(data[0],data[3],data[2],data[1]) calculation(data[1],data[0],data[2],data[3]) calculation(data[1],data[0],data[3],data[2]) calculation(data[1],data[2],data[3],data[0]) calculation(data[1],data[2],data[0],data[3]) calculation(data[1],data[3],data[0],data[2]) calculation(data[1],data[3],data[2],data[0]) calculation(data[2],data[0],data[1],data[3]) calculation(data[2],data[0],data[3],data[1]) calculation(data[2],data[1],data[0],data[3]) calculation(data[2],data[1],data[3],data[0]) calculation(data[2],data[3],data[0],data[1]) calculation(data[2],data[3],data[1],data[0]) calculation(data[3],data[0],data[1],data[2]) calculation(data[3],data[0],data[2],data[1]) calculation(data[3],data[1],data[0],data[2]) calculation(data[3],data[1],data[2],data[0]) calculation(data[3],data[2],data[0],data[1]) calculation(data[3],data[2],data[1],data[0]) } if(flag==false) System.out.println('These four cards can't be calculated 24!') } /** * method public static void calculation(int num1, int num2, int num3, int num4) is the method of printing the result and finding the result of each other * @param num1 produces the number one * @param num2 produces the number two * @param num3 produces the number three * @param num4 produces the number four * */ public static void calculation(int num1, int num2, int num3, int num4){ for (int i = 0 i < 4 i++){ / / The first calculation, first select two from the four numbers for calculation char operator1 = operator[i] int firstResult = calcute(num1, num2, operator1)/ / First select the first, and the second number to calculate int midResult = calcute(num2, num3, operator1)/ / First select the second and third two numbers for calculation int tailResult = calcute(num3,num4, operator1)/ / First select the third and fourth two to calculate for (int j = 0 j < 4 j++){ / / The second calculation, continue from the results of the last calculation, this time select two from three numbers for calculation 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++){ / / The third calculation, also the last calculation, calculate the result of two numbers, if it is 24, the output expression char operator3 = operator[k] //In the above calculations, num1, num2, num3, and num4 are integer values, but if you want to output an expression with A, J, Q, and K, you must change these four numbers to String type. The same below if(calcute(firstMidResult, num4, operator3) == 24){ reData[0]=num1 reData[1]=num2 reData[2]=num3 reData[3]=num4 for(int p=0p<4p++){ if(reData[p]==1){ dataString[p]='A'} if(reData[p]==2){ dataString[p]='2'} if(reData[p]==3){ dataString[p]='3'} if(reData[p]==4){ dataString[p]='4'} if(reData[p]==5){ dataString[p]='5'} if(reData[p]==6){ dataString[p]='6'} if(reData[p]==7){ dataString[p]='7'} if(reData[p]==8){ dataString[p]='8'} if(reData[p]==9){ dataString[p]='9'} if(reData[p]==10){ dataString[p]='10'} if(reData[p]==11){ dataString[p]='J'} if(reData[p]==12){ dataString[p]='Q'} if(reData[p]==13){ dataString[p]='k'} } System.out.println('((' + dataString[0] + operator1 + dataString[1] + ')' + operator2 + dataString[2] + ')' + operator3 + dataString[3]) flag = true/ / If there is an expression output, it will explain the solution, the same below } if(calcute(firstResult, firstTailResult, operator3) == 24){ System.out.println('(' + dataString[0] + operator1 + dataString[1] + ')' + operator3 + '(' + dataString[2] + operator2 + dataString[3] + ')') flag = true } if(calcute(midFirstResult, num4, operator3) == 24){ reData[0]=num1 reData[1]=num2 reData[2]=num3 reData[3]=num4 for(int p=0p<4p++){ if(reData[p]==1){ dataString[p]='A'} if(reData[p]==2){ dataString[p]='2'} if(reData[p]==3){ dataString[p]='3'} if(reData[p]==4){ dataString[p]='4'} if(reData[p]==5){ dataString[p]='5'} if(reData[p]==6){ dataString[p]='6'} if(reData[p]==7){ dataString[p]='7'} if(reData[p]==8){ dataString[p]='8'} if(reData[p]==9){ dataString[p]='9'} if(reData[p]==10){ dataString[p]='10'} if(reData[p]==11){ dataString[p]='J'} if(reData[p]==12){ dataString[p]='Q'} if(reData[p]==13){ dataString[p]='k'} } System.out.println('(' + dataString[0] + operator2 + '(' + dataString[1] + operator1 + dataString[2] + '))' + operator3 + dataString[3]) flag = true } if(calcute(num1,midTailResult, operator3) == 24){ reData[0]=num1 reData[1]=num2 reData[2]=num3 reData[3]=num4 for(int p=0p<4p++){ if(reData[p]==1){ dataString[p]='A'} if(reData[p]==2){ dataString[p]='2'} if(reData[p]==3){ dataString[p]='3'} if(reData[p]==4){ dataString[p]='4'} if(reData[p]==5){ dataString[p]='5'} if(reData[p]==6){ dataString[p]='6'} if(reData[p]==7){ dataString[p]='7'} if(reData[p]==8){ dataString[p]='8'} if(reData[p]==9){ dataString[p]='9'} if(reData[p]==10){ dataString[p]='10'} if(reData[p]==11){ dataString[p]='J'} if(reData[p]==12){ dataString[p]='Q'} if(reData[p]==13){ dataString[p]='k'} } System.out.println(' ' + dataString[0] + operator3 + '((' + dataString[1] + operator1 + dataString[2] + ')' + operator2 + dataString[3] + ')') flag = true } if(calcute(num1,tailMidResult,operator3) == 24){ reData[0]=num1 reData[1]=num2 reData[2]=num3 reData[3]=num4 for(int p=0p<4p++){ if(reData[p]==1){ dataString[p]='A'} if(reData[p]==2){ dataString[p]='2'} if(reData[p]==3){ dataString[p]='3'} if(reData[p]==4){ dataString[p]='4'} if(reData[p]==5){ dataString[p]='5'} if(reData[p]==6){ dataString[p]='6'} if(reData[p]==7){ dataString[p]='7'} if(reData[p]==8){ dataString[p]='8'} if(reData[p]==9){ dataString[p]='9'} if(reData[p]==10){ dataString[p]='10'} if(reData[p]==11){ dataString[p]='J'} if(reData[p]==12){ dataString[p]='Q'} if(reData[p]==13){ dataString[p]='k'} } System.out.println(' ' + dataString[0] + operator3 + '(' + dataString[1] + operator2 + '(' + dataString[2] + operator1 + dataString[3] + '))') flag = true } } } } } }

第四に、デバッグとテスト

1、テスト

画像



2、デバッグ

デバッグを開始します
画像
ランダムデータを取得し、最初のループで出力します

画像
画像
ループを続けて乱数を生成します。このとき、サイクル数i = 1はループの2回目です。 2番目のランダムデータを生成します
画像
4つの乱数を取得します
画像
分類検証のためのcalculate()メソッドへのステップ
画像
画像
計算のためにcalculation()メソッドにステップインします
画像
calcute()メソッドにステップインして結果を取得します
画像
画像
結果を出力し、デバッグを終了します



画像

V.まとめ

Due to the time problem, the improvement part is not finished, the completion page of the basic part is not very good, but the basic requirements are basically realized. The logic of the basic part of this question is not too difficult, mainly because the calculation is more and more complicated. The current part of the improvement It is more convenient to solve the time problem with multi-threading, and I will continue to improve the part later.