0

The 24 Game is an arithmetical game in which the objective is to find a way to manipulate four integers so that the end result is 24. Addition, subtraction, multiplication, or division in any order of the numbers may be used to make the four digits operations from one to nine equal 24.

The rules are simple: you have to use each number only once and only the 4 numbers that were read from the user to find one equation to obtain 24.

For example, for the numbers 4,7,8,8, a possible solution is: (7-(8/8))*4=24.

Most sets of 4 digits can be used in multiple equations that result in 24: for example the input: 2, 2, 4 and 7 can be used in multiple ways to obtain 24:

2+2*(4+7) = 24

2+2*(7+4) = 24

(2+2)*7-4 = 24

(2*2)*7-4 = 24

2*(2*7)-4 = 24

There are also combinations of 4 numbers that cannot result into any equation equal with 24. For example 1,1,1,1. In this case, your program should return that there is no possible equation equal with 24.

Note: Although we will enter 4 integers between 1 and 9, we will use doubles to compute all the operations. For example, the numbers 3,3,8,8 can be combined into the formula: 8/(3-8/3) = 24.

Workflow: Your program should read the 4 numbers from the user and output a formula that results in 24. The algorithm should enumerate all the possible orders of 4 numbers, all the possible combinations and all the possible formulas. There is no required GUI for this project I need help with a method that will shuffle the operators for all 64 possible combos so 4 operators and 3 being used in each equation and also account for parenthesis during the equations . I have no idea where to begin.

n94pro
  • 27
  • 1
  • 2
  • 6
  • 3
    `"I have no idea where to begin."` Write your program incrementally. First write a program which accepts input from the user and prints it out. Then start adding functionality until it does everything you need it to. – azurefrog Nov 25 '14 at 03:45
  • I did that part i just needed help to figure out functionlity . How would i go about writing a method that shuffles operators to generate every possible equation . I have written a permutation method that returns a 2d array with 24 combos of abcd . I was just wondering how to actually implement another method to get the operators to form equations. – n94pro Nov 25 '14 at 03:56
  • Show the code you've written. – Chris Martin Nov 25 '14 at 04:57
  • 2
    Does this answer your question? http://stackoverflow.com/questions/27160339/buiding-a-math-game-java/27166151 – Michael Laszlo Nov 27 '14 at 08:57

1 Answers1

2

If you can generate permutations of a string. You need to do that for all the numbers to get all the permutations possible for those numbers.

Now you just need to plugin permutations of operators (3 at a time).

For that you can generate all the permutations of the operators and store them in an array, as that would remain constant for each case. And out of each permutation generated, just pick the first 3 characters as we are looking at groups of 3 out of the 4 possible.

Once you have that, it's simply a matter of reading a permutation of the numbers and then reading a permutation of operators and evaluating the expression.

For reference, I have made a simple demo of a function that finds permutations of a string in Java. The recursive function looks something like (from a relevant SO Post):

 public void permut(String str1,String str2){
   if(str2.length() != 0){
     char ch = str2.charAt(0);
     for(int i = 0; i <= str1.length();i++)
        permut(str1.substring(0,i) + ch + str1.substring(i,str1.length()),
                 str2.substring(1,str2.length()));
  }else{
    System.out.println(str1);
  }
 }

If you can successfully generate all permutations of a string, the above exercise should be doable. I hope it gets you started in the right direction.

Community
  • 1
  • 1
Vivek Pradhan
  • 4,777
  • 3
  • 26
  • 46
  • I think you are missing the fact that the parantheses can be set randomly. – Turakar Nov 28 '14 at 07:10
  • If you try all the permutations, parentheses would be taken care of by operator priority I suppose. Parenthesising is just a way of generating different permutations for the expression. – Vivek Pradhan Nov 28 '14 at 16:42