24点游戏--java打开方式
2016-01-02 14:57
435 查看
首先呢,为那些不知道24点游戏的盆友们介绍一下24点游戏的游戏规则:
对于给出的4个数字进行四种运算(最基础的加减乘除)最终得到运算结果是24的时候就算游戏通关(每个给出的数字都要用到,且数字不可以重复使用)。
当然,对于简单的问题跟着感觉就可以快速解答出来,但是对于难度稍大的题,想出一种方法都要费些时间,更别说解出所有的运算方法。于是呢,最近刚刚Java入门的我来了点尝试,写了个程序,bug找的深夜的我最后还是搞定了这个程序,好不容易创作的处女之作当然要在这里稍微秀一秀full mark的cw就不算咯,当然了还是为了分享下自己的成果,计算机的大神们不要嘲笑,身为应数狗的我(初二才会下游戏)也是不容易的。。。。
废话少说,先简单的说一下大概程序算法:
为了找到所有正确的运算方法,当然要首先找到所有可能的运算方法,基于这个简单的思路,整个编程过程包括三次运算,数字的个数由4到2一次递减,当然运算的可能性也是依次递增。在这里我给个例子: 对于12 6 9 11 (四个数)首先固定12和6 ,对11 和9进行四种最基本的运算方式得到至少三种的的运算可能(结果为整且为正)2 20 99 那么接下来四个数字演变成12 6 2,12 6 20,12 6 99三个数组,然后对刚刚的得到的三个数组(三个数)分别运算,在这里举例数组12
6 2,固定12 对6和2进行运算,得到 4 8 12 3,就得到了四个新的数组(两个数)12 4,12 8,12 12,12 3,接着对这三个数组进行运算,举例 12 12,12+12=24 最终得到希望的答案。
以上大概就程序的简单思路,处女之code紧跟着来:
package piont24;
import java.util.ArrayList;
import java.util.Scanner;
//created for storing data
class base01 {
double Z;
double x;
double y;
//caculation01 is for storing the caculation procedure
String caculation01;
base01(double x, double y, double z, String caculation01) {
super();
this.x = x;
this.y = y;
//Z is for storing the result of the caculation.
this.Z = z;
this.caculation01 = caculation01;
}
}
//created for storing data
class base02 {
double sum;
double left;
//caculation02 is for storing the caculation procedure
String caculation02;
String caculationOFleft;
base02(double sum, double left, String caculation02, String caculationOFleft) {
super();
//sum is for storing the result of the caculation.
this.sum = sum;
this.left = left;
this.caculation02 = caculation02;
this.caculationOFleft = caculationOFleft;
}
}
public class Piont24 {
static Scanner kb = new Scanner(System.in);
// game start, input number first for getting the answer
public static void main(String[] args) {
System.out.println("Please, input the four numbers for 24 piont game: ");
String data = kb.nextLine();
PotionGame(data);
}
// first procedure for finding the suitable caculation
public static void PotionGame(String data) {
String number[] = data.split(" ");
Double[] NUMBER = new Double[4];
for (int i = 0; i < 4; i++) {
NUMBER[i] = Double.parseDouble(number[i]);
}
//Left is a ArrayList for storing the left number of two number which is under four caculations.
ArrayList<Integer> Left = new ArrayList();
//BASE01 is for storing the left number and the new caculation result.
ArrayList<base01> BASE01 = new ArrayList();
//X and Y is for storing the left number. Z is for storing the caculation result.
double X, Y, Z, A, B;
// A and B is for storing the two number (for four caculations(+ - * /)) by natural order.
String caculation01;
for (int x = 0; x < 4; x++) {
//fix one number of the input numbers.
caculation01 = null;
//try each number of the left.
for (int y = x + 1; y < 4; y++) {
Left = Left(x, y);
X = NUMBER[Left.get(0)];
Y = NUMBER[Left.get(1)];
A = NUMBER[y];
B = NUMBER[x];
//caculation is for recording the caculation procedure.
Z = A - B;
caculation01 = A + "-" + B;
BASE01.add(new base01(X, Y, Z, caculation01));
Z = B - A;
caculation01 = B + "-" + A;
BASE01.add(new base01(X, Y, Z, caculation01));
Z = A + B;
caculation01 = A + "+" + B;
BASE01.add(new base01(X, Y, Z, caculation01));
Z = B + A;
caculation01 = B + "+" + A;
BASE01.add(new base01(X, Y, Z, caculation01));
Z = A * B;
caculation01 = A + "*" + B;
BASE01.add(new base01(X, Y, Z, caculation01));
if (A != 0 && B != 0) {
Z = A / B;
caculation01 = A + "/" + B;
BASE01.add(new base01(X, Y, Z, caculation01));
Z = B / A;
caculation01 = B + "/" + A;
BASE01.add(new base01(X, Y, Z, caculation01));
}
}
}
//procedure 2 for getting the result.
Procedure2(BASE01);
}
//try all of the possible caculation for the data got from Procedure1.
public static void Procedure2(ArrayList<base01> BASE) {
ArrayList<base02> BASE02 = new ArrayList();
int len = BASE.size();
double left, sum, a, b;
String caculationOFleft;
Double NUMBER02[];
//F and S is for recoding down the caculation procedure.
String caculation02, F, S;
for (int i = 0; i < len; i++) {
NUMBER02 = new Double[3];
NUMBER02[0] = BASE.get(i).x;
NUMBER02[1] = BASE.get(i).y;
NUMBER02[2] = BASE.get(i).Z;
for (int p = 0; p < 3; p++) {
for (int u = p + 1; u < 3; u++) {
caculationOFleft = null;
a = NUMBER02[p];
b = NUMBER02[u];
left = NUMBER02[3 - (p + u)];
if (p == 2) {
F = "(" + BASE.get(i).caculation01 + ")";
} else {
F = Double.toString(a);
}
if (u == 2) {
S = "(" + BASE.get(i).caculation01 + ")";
} else {
S = Double.toString(b);
}
//when the left number is got from the caculation in the procedure1.
if (u != 2 && p != 2) {
//use caculationOFleft to record the caculation procedure.
caculationOFleft = BASE.get(i).caculation01;
}
sum = a - b;
caculation02 = F + "-" + S;
BASE02.add(new base02(sum, left, caculation02, caculationOFleft));
sum = b - a;
caculation02 = S + "-" + F;
BASE02.add(new base02(sum, left, caculation02, caculationOFleft));
sum = a + b;
caculation02 = F + "+" + S;
BASE02.add(new base02(sum, left, caculation02, caculationOFleft));
sum = b + a;
caculation02 = S + "+" + F;
BASE02.add(new base02(sum, left, caculation02, caculationOFleft));
sum = a * b;
caculation02 = F + "*" + S;
BASE02.add(new base02(sum, left, caculation02, caculationOFleft));
if (a != 0 && b != 0) {
sum = a / b;
caculation02 = F + "/" + S;
BASE02.add(new base02(sum, left, caculation02, caculationOFleft));
sum = b / a;
caculation02 = S + "/" + F;
BASE02.add(new base02(sum, left, caculation02, caculationOFleft));
}
}
}
}
RESULT(BASE02);
}
//test all of the possible caculations on such four numbers.
public static void RESULT(ArrayList<base02> BASE02) {
String caculationOFleft;
double LEFT, SUM;
base02 temp01, temp02;
int length = BASE02.size(), k = 0;
//here is for removing the repeated solution.
while (k < length) {
temp01 = BASE02.get(k);
for (int l = k + 1; l < length; l++) {
temp02 = BASE02.get(l);
if (temp01.caculation02.equalsIgnoreCase(temp02.caculation02)) {
BASE02.remove(temp02);
l--;
length = BASE02.size();
}
}
k++;
}
int LEN = BASE02.size(), u = 1;
for (int r = 0; r < LEN; r++) {
caculationOFleft = BASE02.get(r).caculationOFleft;
LEFT = BASE02.get(r).left;
SUM = BASE02.get(r).sum;
if (result(LEN, BASE02, LEFT, SUM, BASE02.get(r).caculation02, caculationOFleft, u)) {
u++;
}
}
if (u == 1) {
System.out.print("Sorry, the input data is not correct!!");
} else {
System.out.print("\r\n\r\nThe above is all of the possible solution.");
}
}
//get all of the correct caculations on the four numbers for 24point game.
public static boolean result(double LEN, ArrayList<base02> BASE02, double LEFT, double SUM, String CACULATION, String caculationOFleft, int u) {
boolean result = false;
//F and S is for recoding down the caculation procedure.
String S, F;
S = Double.toString(LEFT);
F = "(" + CACULATION + ")";
if (caculationOFleft != null) {
S = "(" + caculationOFleft + ")";
}
if (LEFT + SUM == 24) {
System.out.print("\r\n\r\n" + "Solution" + u + ": " + S + "+" + F);
result = true;
u++;
}
if (LEFT * SUM == 24) {
System.out.print("\r\n\r\n" + "Solution" + u + ": " + S + "*" + F);
result = true;
}
if (LEFT - SUM == 24) {
System.out.print("\r\n\r\n" + "Solution" + u + ": " + S + "-" + F);
result = true;
}
if (SUM - LEFT == 24) {
System.out.print("\r\n\r\n" + "Solution" + u + ": " + F + "-" + S);
result = true;
}
if (LEFT != 0 && SUM != 0) {
if (LEFT / SUM == 24) {
result = true;
System.out.print("\r\n\r\n" + "Solution" + u + ": " + S + "/" + F);
}
if (SUM / LEFT == 24) {
result = true;
System.out.print("\r\n\r\n" + "Solution" + u + ": " + F + "/" + S);
}
}
return result;
}
//get the left number of the two numbers under caculations.
public static ArrayList Left(int x, int y) {
ArrayList<Integer> left = new ArrayList();
for (int i = 0; i < 4; i++) {
if (i != x && i != y) {
left.add(i);
}
}
return left;
}
}
对于给出的4个数字进行四种运算(最基础的加减乘除)最终得到运算结果是24的时候就算游戏通关(每个给出的数字都要用到,且数字不可以重复使用)。
当然,对于简单的问题跟着感觉就可以快速解答出来,但是对于难度稍大的题,想出一种方法都要费些时间,更别说解出所有的运算方法。于是呢,最近刚刚Java入门的我来了点尝试,写了个程序,bug找的深夜的我最后还是搞定了这个程序,好不容易创作的处女之作当然要在这里稍微秀一秀full mark的cw就不算咯,当然了还是为了分享下自己的成果,计算机的大神们不要嘲笑,身为应数狗的我(初二才会下游戏)也是不容易的。。。。
废话少说,先简单的说一下大概程序算法:
为了找到所有正确的运算方法,当然要首先找到所有可能的运算方法,基于这个简单的思路,整个编程过程包括三次运算,数字的个数由4到2一次递减,当然运算的可能性也是依次递增。在这里我给个例子: 对于12 6 9 11 (四个数)首先固定12和6 ,对11 和9进行四种最基本的运算方式得到至少三种的的运算可能(结果为整且为正)2 20 99 那么接下来四个数字演变成12 6 2,12 6 20,12 6 99三个数组,然后对刚刚的得到的三个数组(三个数)分别运算,在这里举例数组12
6 2,固定12 对6和2进行运算,得到 4 8 12 3,就得到了四个新的数组(两个数)12 4,12 8,12 12,12 3,接着对这三个数组进行运算,举例 12 12,12+12=24 最终得到希望的答案。
以上大概就程序的简单思路,处女之code紧跟着来:
package piont24;
import java.util.ArrayList;
import java.util.Scanner;
//created for storing data
class base01 {
double Z;
double x;
double y;
//caculation01 is for storing the caculation procedure
String caculation01;
base01(double x, double y, double z, String caculation01) {
super();
this.x = x;
this.y = y;
//Z is for storing the result of the caculation.
this.Z = z;
this.caculation01 = caculation01;
}
}
//created for storing data
class base02 {
double sum;
double left;
//caculation02 is for storing the caculation procedure
String caculation02;
String caculationOFleft;
base02(double sum, double left, String caculation02, String caculationOFleft) {
super();
//sum is for storing the result of the caculation.
this.sum = sum;
this.left = left;
this.caculation02 = caculation02;
this.caculationOFleft = caculationOFleft;
}
}
public class Piont24 {
static Scanner kb = new Scanner(System.in);
// game start, input number first for getting the answer
public static void main(String[] args) {
System.out.println("Please, input the four numbers for 24 piont game: ");
String data = kb.nextLine();
PotionGame(data);
}
// first procedure for finding the suitable caculation
public static void PotionGame(String data) {
String number[] = data.split(" ");
Double[] NUMBER = new Double[4];
for (int i = 0; i < 4; i++) {
NUMBER[i] = Double.parseDouble(number[i]);
}
//Left is a ArrayList for storing the left number of two number which is under four caculations.
ArrayList<Integer> Left = new ArrayList();
//BASE01 is for storing the left number and the new caculation result.
ArrayList<base01> BASE01 = new ArrayList();
//X and Y is for storing the left number. Z is for storing the caculation result.
double X, Y, Z, A, B;
// A and B is for storing the two number (for four caculations(+ - * /)) by natural order.
String caculation01;
for (int x = 0; x < 4; x++) {
//fix one number of the input numbers.
caculation01 = null;
//try each number of the left.
for (int y = x + 1; y < 4; y++) {
Left = Left(x, y);
X = NUMBER[Left.get(0)];
Y = NUMBER[Left.get(1)];
A = NUMBER[y];
B = NUMBER[x];
//caculation is for recording the caculation procedure.
Z = A - B;
caculation01 = A + "-" + B;
BASE01.add(new base01(X, Y, Z, caculation01));
Z = B - A;
caculation01 = B + "-" + A;
BASE01.add(new base01(X, Y, Z, caculation01));
Z = A + B;
caculation01 = A + "+" + B;
BASE01.add(new base01(X, Y, Z, caculation01));
Z = B + A;
caculation01 = B + "+" + A;
BASE01.add(new base01(X, Y, Z, caculation01));
Z = A * B;
caculation01 = A + "*" + B;
BASE01.add(new base01(X, Y, Z, caculation01));
if (A != 0 && B != 0) {
Z = A / B;
caculation01 = A + "/" + B;
BASE01.add(new base01(X, Y, Z, caculation01));
Z = B / A;
caculation01 = B + "/" + A;
BASE01.add(new base01(X, Y, Z, caculation01));
}
}
}
//procedure 2 for getting the result.
Procedure2(BASE01);
}
//try all of the possible caculation for the data got from Procedure1.
public static void Procedure2(ArrayList<base01> BASE) {
ArrayList<base02> BASE02 = new ArrayList();
int len = BASE.size();
double left, sum, a, b;
String caculationOFleft;
Double NUMBER02[];
//F and S is for recoding down the caculation procedure.
String caculation02, F, S;
for (int i = 0; i < len; i++) {
NUMBER02 = new Double[3];
NUMBER02[0] = BASE.get(i).x;
NUMBER02[1] = BASE.get(i).y;
NUMBER02[2] = BASE.get(i).Z;
for (int p = 0; p < 3; p++) {
for (int u = p + 1; u < 3; u++) {
caculationOFleft = null;
a = NUMBER02[p];
b = NUMBER02[u];
left = NUMBER02[3 - (p + u)];
if (p == 2) {
F = "(" + BASE.get(i).caculation01 + ")";
} else {
F = Double.toString(a);
}
if (u == 2) {
S = "(" + BASE.get(i).caculation01 + ")";
} else {
S = Double.toString(b);
}
//when the left number is got from the caculation in the procedure1.
if (u != 2 && p != 2) {
//use caculationOFleft to record the caculation procedure.
caculationOFleft = BASE.get(i).caculation01;
}
sum = a - b;
caculation02 = F + "-" + S;
BASE02.add(new base02(sum, left, caculation02, caculationOFleft));
sum = b - a;
caculation02 = S + "-" + F;
BASE02.add(new base02(sum, left, caculation02, caculationOFleft));
sum = a + b;
caculation02 = F + "+" + S;
BASE02.add(new base02(sum, left, caculation02, caculationOFleft));
sum = b + a;
caculation02 = S + "+" + F;
BASE02.add(new base02(sum, left, caculation02, caculationOFleft));
sum = a * b;
caculation02 = F + "*" + S;
BASE02.add(new base02(sum, left, caculation02, caculationOFleft));
if (a != 0 && b != 0) {
sum = a / b;
caculation02 = F + "/" + S;
BASE02.add(new base02(sum, left, caculation02, caculationOFleft));
sum = b / a;
caculation02 = S + "/" + F;
BASE02.add(new base02(sum, left, caculation02, caculationOFleft));
}
}
}
}
RESULT(BASE02);
}
//test all of the possible caculations on such four numbers.
public static void RESULT(ArrayList<base02> BASE02) {
String caculationOFleft;
double LEFT, SUM;
base02 temp01, temp02;
int length = BASE02.size(), k = 0;
//here is for removing the repeated solution.
while (k < length) {
temp01 = BASE02.get(k);
for (int l = k + 1; l < length; l++) {
temp02 = BASE02.get(l);
if (temp01.caculation02.equalsIgnoreCase(temp02.caculation02)) {
BASE02.remove(temp02);
l--;
length = BASE02.size();
}
}
k++;
}
int LEN = BASE02.size(), u = 1;
for (int r = 0; r < LEN; r++) {
caculationOFleft = BASE02.get(r).caculationOFleft;
LEFT = BASE02.get(r).left;
SUM = BASE02.get(r).sum;
if (result(LEN, BASE02, LEFT, SUM, BASE02.get(r).caculation02, caculationOFleft, u)) {
u++;
}
}
if (u == 1) {
System.out.print("Sorry, the input data is not correct!!");
} else {
System.out.print("\r\n\r\nThe above is all of the possible solution.");
}
}
//get all of the correct caculations on the four numbers for 24point game.
public static boolean result(double LEN, ArrayList<base02> BASE02, double LEFT, double SUM, String CACULATION, String caculationOFleft, int u) {
boolean result = false;
//F and S is for recoding down the caculation procedure.
String S, F;
S = Double.toString(LEFT);
F = "(" + CACULATION + ")";
if (caculationOFleft != null) {
S = "(" + caculationOFleft + ")";
}
if (LEFT + SUM == 24) {
System.out.print("\r\n\r\n" + "Solution" + u + ": " + S + "+" + F);
result = true;
u++;
}
if (LEFT * SUM == 24) {
System.out.print("\r\n\r\n" + "Solution" + u + ": " + S + "*" + F);
result = true;
}
if (LEFT - SUM == 24) {
System.out.print("\r\n\r\n" + "Solution" + u + ": " + S + "-" + F);
result = true;
}
if (SUM - LEFT == 24) {
System.out.print("\r\n\r\n" + "Solution" + u + ": " + F + "-" + S);
result = true;
}
if (LEFT != 0 && SUM != 0) {
if (LEFT / SUM == 24) {
result = true;
System.out.print("\r\n\r\n" + "Solution" + u + ": " + S + "/" + F);
}
if (SUM / LEFT == 24) {
result = true;
System.out.print("\r\n\r\n" + "Solution" + u + ": " + F + "/" + S);
}
}
return result;
}
//get the left number of the two numbers under caculations.
public static ArrayList Left(int x, int y) {
ArrayList<Integer> left = new ArrayList();
for (int i = 0; i < 4; i++) {
if (i != x && i != y) {
left.add(i);
}
}
return left;
}
}
相关文章推荐
- 用js写“算24”游戏的思路分析与实现代码
- 分享自己写的一个贪吃蛇的游戏(Linux)
- 分享一个蛋疼的俄罗斯方块小游戏
- Linux下的字符界面扫雷游戏
- linux下的图形界面扫雷游戏(Gtk+2.0)
- 第三章 实现Eclipse Android与J2me平台切换(下)
- cocos2d里面如何实现MVC(二)
- cocos2d里面如何实现MVC(四)
- HR绝不会告诉你的24条面试“潜规则”
- 依赖链接器最小化编译时间(Abusing the Linker to Minimize Compilation Time)
- 智力题研究(二)
- JS开发HTML5游戏《神奇的六边形》(一)
- JS开发HTML5游戏《神奇的六边形》(三)
- JS开发HTML5游戏《神奇的六边形》(五)
- JS开发HTML5游戏《神奇的六边形》(六)
- JS开发HTML5游戏《神奇的六边形》(七)
- JS开发HTML5游戏《神奇的六边形》(八)
- 开源免费的HTML5游戏引擎——青瓷引擎(QICI Engine) 1.0正式版发布了!
- vs2010 c++ 调试时(exe或dll中有bug)