24 Game
Does anyone remember the 24 card game? It's played with a deck of cards minus the face cards and 10. So there are four suits of 1(ace) to 9. Then the cards are divided between two players and each player lays two cards on the table. The first player to use those cards, along with addition, subtraction, multiplication, and division, to get to 24 wins the pile. The game is fun and easy but how can you calculate if four numbers are solvable? How many combinations of the 40 cards have solutions?
Yesterday I wrote a program that there are 6561 possible combinations and 2133 card combinations with solutions of 24. First the program loops through each combination of cards. Then it checks each combination to see of there is a solution of 24. I also wrote a method that will print the solution with parenthesis to keep the precedence of each operator correct. There are also options to customize the game. You can change the low and high cards, check for solutions other than 24 and even check for a range of solutions. Instead of putting this project in a jar file I'm just going to post the code here and the output here.
public class Main {
public static String ops = "";
public static int solutions = 0;
public static int cardSolutions = 0;
public static long totalOpsCount = 0;
public static int tryCount = 0;
public static int lowNumber = 1;
public static int highNumber = 9;
public static int getResult(int op, int a, int b) {
int result = 0;
switch(op) {
case 0:
result = a + b;
break;
case 1:
result = a - b;
break;
case 2:
result = a * b;
break;
case 3:
if(a % b == 0)
result = a/b;
else
result = -1;
break;
}
return result;
}
public static String getOperator(int op) {
String result = "";
switch(op) {
case 0:
result = "+";
break;
case 1:
result = "-";
break;
case 2:
result = "*";
break;
case 3:
result = "/";
break;
}
return result;
}
public static String getMath(int i, String o1, int j, String o2, int k, String o3, int l) {
boolean o1p = o1.equals("*") || o1.equals("/");
boolean o2p = o2.equals("*") || o2.equals("/");
boolean o3p = o3.equals("*") || o3.equals("/");
/*
* these are the cases
* ttt
* ttf, tft, ftt
* tff, ftf, fft
* fff
*/
if(o1p == false && o2p == true) { //ftf and ftt
return "(" + i + o1 + j + ")" + o2 + k + o3 + l;
} else if(o2p == false && o3p == true) { //tft, fft
return "(" + i + o1 + j + o2 + k + ")" + o3 + l;
}
return i + o1 + j + o2 + k + o3 + l; //ttt, ttf, tff, fff
}
public static boolean checkNumbers(int i, int j, int k, int l, int total) {
boolean result = false;
for(int x = 0; x < 4; x++) {
for(int y = 0; y < 4; y++) {
for(int z = 0; z < 4; z++) {
totalOpsCount++;
int a = getResult(x, i, j);
int b = getResult(y, a, k);
int c = getResult(z, b, l);
if(a < 0 || b < 0 || c < 0) {
//System.out.println("* bad division " + getMath(i, getOperator(x), j, getOperator(y), k, getOperator(z), l) + " != " + c);
} else if(c == total) {
solutions++;
System.out.println(getMath(i, getOperator(x), j, getOperator(y), k, getOperator(z), l) + " = " + c);
result = true;
} else {
//System.out.println("* " + getMath(i, getOperator(x), j, getOperator(y), k, getOperator(z), l) + " != " + c);
}
}
}
}
return result;
}
public static void findSolutions(int q) {
for(int i = lowNumber; i <= highNumber; i++) {
for(int j = lowNumber; j <= highNumber; j++) {
for(int k = lowNumber; k <= highNumber; k++) {
for(int l = lowNumber; l <= highNumber; l++) {
tryCount++;
if(checkNumbers(i, j, k, l, q)) {
cardSolutions++;
}
}
}
}
}
if(cardSolutions > 0)
System.out.println(q + ", " + cardSolutions);
System.out.println(q + " has " + tryCount + " possible combinations.");
System.out.println("There are " + cardSolutions + " combinations with solutions.");
System.out.println("There are " + solutions + " total solutions.");
System.out.println("There are " + (float)solutions/cardSolutions + " solutions per card combination with solutions.");
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("low card " + lowNumber + " high card " + highNumber);
for(int i = 24; i <= 24; i++) {
tryCount = 0;
solutions = 0;
cardSolutions = 0;
findSolutions(i);
}
System.out.println("Total Search Count = " + totalOpsCount);
}
}