Prog 2: TwentyFour
Write a program in C to play the game of Twenty Four. The way the game works is that 4 digits are chosen, and the user must choose a combination of mathematical operators (+,-,/,*) to be applied to those numbers to yield the number 24. You will write your program in stages as described below, where the number of points for each of the two sections is shown:
Stage 1 (20 points):
The first stage allows users to solve a single puzzle. Running this first stage should look like the following, where user input is shown in bold:
Author: Dale Reed Program: #2, TwentyFour TA: Aaron Swartz, Th 4-5 Jan 28, 2013 Welcome to the game of TwentyFour. Using each of the four numbers shown below exactly once, combining them somehow with the basic mathematical operators (+,-,*,/) to yield the value twenty-four. The numbers to use are: 3, 9, 4, 1 Enter the three operators to be used (+,-,*, or /): *-+ 3.0 * 9 is: 27.0 27.0 - 4 is: 23.0 23.0 + 1 is 24.0 Well done, genius! Would you like to play again (Y/N): N Thanks for playing. Exiting program...
The four numbers chosen to be presented to the user should randomly chosen to be one of the following:
8 5 7 4 9 8 5 1 1 1 8 8 9 1 2 2 6 4 8 6 2 3 5 2
If the user indicates that they do want to try again, the program should again randomly choose one of the 6 sets of numbers and display them:
The numbers to use are: 3, 9, 4, 1
...
Stage 2 (35 points):
The second stage generates all 3,185 possible solutions and then chooses one at random, which gets presented to the user. Running this second stage should look like the following, where only the first and last chunks of possible solutions are displayed:
Below is an excerpt of the output from running this program. User input is shown in bold
Author: Dale Reed Program: #2, TwentyFour TA: Aaron Swartz, Th 4-5 Jan 28, 2013 Welcome to the game of TwentyFour. Using each of the four numbers shown below exactly once, combining them somehow with the basic mathematical operators (+,-,*,/) to yield the value twenty-four. *** Start Debug Display *** All possible solutions are: 1: 1+1+1*8 2: 1+1+2*6 3: 1+1*2*6 4: 1*1+2*8 5: 1/1+2*8 6: 1+1*3*4 7: 1*1+3*6 8: 1/1+3*6 9: 1-1+3*8 10: 1*1*3*8 11: 1/1*3*8 12: 1+1*4*3 13: 1+1+4*4 14: 1-1+4*6 15: 1*1*4*6 ... all lines are not shown here, but should be in your program ... 3174: 9+9-6*2 3175: 9-9+6*4 3176: 9/9*6*4 3177: 9+9/6*8 3178: 9+9+7-1 3179: 9/9+7*3 3180: 9+9+8-2 3181: 9-9+8*3 3182: 9/9*8*3 3183: 9+9*8/6 3184: 9+9+9-3 3185: 9*9-9/3 Puzzle to choose is #3176: 9/9*6*4 *** End Debug Display ***
The numbers to use are: 9, 9, 6, 4 Enter the three operators to be used (+,-,*, or /): /** 9.0 / 9 is: 1.0 1.0 * 4 is: 4.0 4.0 * 6 is 24.0 Well done, genius! Would you like to play again (Y/N): N Thanks for playing. Exiting program...
Running the program again could look like the following:
Author: Dale Reed Program: #2, TwentyFour TA: Aaron Swartz, Th 4-5 Jan 28, 2013 Welcome to the game of TwentyFour. Using each of the four numbers shown below exactly once, combining them somehow with the basic mathematical operators (+,-,*,/) to yield the value twenty-four. *** Start Debug Display *** All possible solutions are: 1: 1+1+1*8 2: 1+1+2*6 3: 1+1*2*6 4: 1*1+2*8 5: 1/1+2*8 6: 1+1*3*4 7: 1*1+3*6 8: 1/1+3*6 9: 1-1+3*8 10: 1*1*3*8 11: 1/1*3*8 12: 1+1*4*3 13: 1+1+4*4 14: 1-1+4*6 15: 1*1*4*6 ... 3174: 9+9-6*2 3175: 9-9+6*4 3176: 9/9*6*4 3177: 9+9/6*8 3178: 9+9+7-1 3179: 9/9+7*3 3180: 9+9+8-2 3181: 9-9+8*3 3182: 9/9*8*3 3183: 9+9*8/6 3184: 9+9+9-3 3185: 9*9-9/3 Puzzle to choose is #910: 3*9-4+1 *** End Debug Display *** The numbers to use are: 3, 9, 4, 1 Enter the three operators to be used (+,-,*, or /): *-+ 3.0 * 9 is: 27.0 27.0 - 4 is: 23.0 23.0 + 1 is 24.0 Well done, genius! Would you like to play again (Y/N): Y
*** Start Debug Display *** All possible solutions are: 1. 1+1+1*8 2. 1+1+2*6 3. 1+1*2*6 4. 1*1+2*8 5. 1/1+2*8 6. 1+1*3*4 7. 1*1+3*6 8. 1/1+3*6 9. 1-1+3*8 10. 1*1*3*8 11. 1/1*3*8 12. 1+1*4*3 13. 1+1+4*4 14. 1-1+4*6 15. 1*1*4*6 ... 3174: 9+9-6*2 3175: 9-9+6*4 3176: 9/9*6*4 3177: 9+9/6*8 3178: 9+9+7-1 3179: 9/9+7*3 3180: 9+9+8-2 3181: 9-9+8*3 3182: 9/9*8*3 3183: 9+9*8/6 3184: 9+9+9-3 3185: 9*9-9/3 Puzzle to choose is #1657: 5+9+8+2 *** End Debug Display *** The numbers to use are: 5, 9, 8, 2 Enter the three operators to be used (+,-,*, or /): +-** *** Sorry, that input is the wrong length. Please retry. *** Enter the three operators to be used (+,-,*, or /): +-$ *** Sorry, invalid characters in input. Please retry. *** Enter the three operators to be used (+,-,*, or /): */* 5.0 * 9 is: 45.0 45.0 / 8 is: 5.6 5.6 * 2 is: 11.2 Sorry, that was not correct. The correct answer was: 5+9+8+2 Would you like to play again (Y/N): N Thanks for playing. Exiting program...
Other
Turn your program in using Blackboard. Multiple submissions are allowed, but only the most recent submission will be graded.
Note that debugging information is shown in the sample program run above, and must also be in the program you turn in to us. This is to help us grade the program and verify it's correctness, and will help you in debugging as well.
You may NOT use arrays for this program. If you do, you will lose 40 points.
I suggest you write a separate program to start, where all that program does is generate the possible solutions. I recommend you write this in stages. First write the code to generate all possible combinations of two single digits that give 24. This might look something like:
// check all combinations of 2 single-digit numbers for( float i=1; i<10; i++) { for( float j=1; j<10; j++) { // check the 4 mathematical operators: i (+,-,*,/) j // Addition if( (i + j) == 24) { printf( "%d. %d + %d", solutionNumber++, (int)i, (int)j); } // Subtraction if( (i - j) == 24) { printf( "%d. %d - %d", solutionNumber++, (int)i, (int)j)); } // Multiplication if( (i * j) == 24) { printf( "%d. %d * %d", solutionNumber++, (int)i, (int)j); } // Division if( (i / j) == 24) { printf( "%d. %d / %d", solutionNumber++, (int)i, (int)j); } }//end for( float j... }//end for( float i...
To get the same output as shown in the sample program, always test possible answers in the order: +, -, *, /
Next modify your code so that instead of checking just arithmetic combinations of 2 values, you do it for 3 values. You must be careful here. Keep in mind what the user interface looks like. Answers are accumulated one step at a time. For instance 6 + 3 * 6 would not be a valid possible solution, since our program will solve this as ((6+3) * 6) = 54. In addition, I recommend that you use float values in your for loops, otherwise a solution such as 7 / 2 * 8 would be valid, since 7/2 would get truncated using integer division to 3, which we don't want. Note that when using float values, an answer such as 1/2*6*8 is valid.
After getting this to work properly for 3 values, now add the code so that it works for all 4 values (3 pairs) of single-digit input numbers.
You will need to generate a random number to select which of all the possible puzzles you would like to use. See this sample random number generator code.
Think very carefully about designing this program. One possible solution is to generate the 64 possible combinations of i?j?k?m variables, where each ? ends up being one of the operators +,-,*,/. There are better ways to do this using functions, as we will see.
See here for the full list of 3185 possible answers can be seen here.
Grading:
Of the 35 points for execution for stage 2 of this program, an approximation of the points breakdown is:
10 Displays all 3185 possible puzzles, numbered 1..3185.
No Stage 2 credit is given for the elements for below if all puzzles are not generated.
5 Selects one of the puzzles at random, storing its values to be used
5 Allows playing the puzzle, correctly doing the math at each step
3 Verifies whether or not the result equals 24, giving an appropriate message
2 Allows retrying another puzzle
5 Does error checking for length of input, allowing the user to retry
5 Does error checking for invalid input characters, allowing the user to retry
====
35 Total