[Addition on 8/23: Your program must be entirely in C, with no C++ features.]
[Addition on 8/25: For stage 1, present numbers in order, going clockwise starting from the bottom number.]
[Addition on 8/29: You may use global variables for this program.]
[Addition on 9/1: Details given on what is required to turn in your program on Blackboard.]
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:
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: #1, TwentyFour TA: Donald Clinton, Th 4-5 Aug 22, 2016 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 clockwise starting from the bottom number, such as:
The numbers to use are: 3, 9, 4, 1
...
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: #1, TwentyFour TA: Donald Clinton, Th 4-5 Aug 22, 2016
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: #1, TwentyFour TA: Donald Clinton, Th 4-5 Aug 22, 2016
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...
Turn in your program using Blackboard. Multiple submissions are allowed, but only the most recent submission will be graded.
The name of the program you will turn in should be prog1 followed by your netid and the .c file extension. In other words, if your netid is reed then your program would be called prog1reed.c
You must also zip up the file you turn in, which typically is done by right-clicking on it and choosing the "compress" option. After zipping your program (e.g. prog1reed.c), you will likely end up with a file called something like prog1reed.zip. Only turn in this single file, turning it in on Blackboard into the assignment Program 1. Please do not put it into a folder before zipping it, rather zip the one file by itself.
Failing to follow these naming conventions and failure to turn in a zip file will result in a 5 point deduction, even if everything else is perfect in your program.
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. For example, to generate random numbers you could use the following:
#include <stdio.h> #include <stdlib.h> // Needed for random number libraries on some compilers /* Random number generator example */ int main() { int i; // counting variable srand( 1); // Initialize random number generator. // Only do this ONCE in your program! //use srand( time(0)); to give different values each time. // In this example do mod 7 to give random numbers from 0 to 6 for ( i = 0; i < 15; i++) { printf(" %d \n ", rand()%7 ) ; } }
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.
For the full list of 3185 possible answers see 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