Prog 2: MiniMasterMind
Due at 12 Noon on the date shown in the course schedule
Write a program similar to Mastermind, except you will use 3 digits instead of colors. Running the program should look like the following:
Author: Dale Reed
Lab: Wed 5am
Program: #2, MiniMasterMind
Welcome to MiniMastermind.
The program selects 3 distinct random digits 0..9.
On each turn you guess 3 digits. Program indicates how many are correct.
You have 10 moves to guess the number. Good luck!
In place Out of place
-------- ------------
1. Your guess: 012
You entered: 012 0 1
2. Your guess: 021
You entered: 021 1 0
3. Your guess: 345
You entered: 345 1 0
4. Your guess: 354
You entered: 354 1 0
5. Your guess: 321
You entered: 321 2 0
6. Your guess: 678
You entered: 678 0 1
7. Your guess: 687
You entered: 687 1 0
8. Your guess: 327
You entered: 327 1 0
9. Your guess: 381
You entered: 381 3 0
*** Congratulations! You did it! ***
Thank you for playing. Exiting...
Alternatively if you are not able to find the answer within ten moves, a message and the answer is displayed, as follows:
Author: Dale Reed
Lab: Wed 5am
Program: #2, MiniMasterMind
Welcome to MiniMastermind.
The program selects 3 distinct random digits 0..9.
On each turn you guess 3 digits. Program indicates how many are correct.
You have 10 moves to guess the number. Good luck!
In place Out of place
-------- ------------
1. Your guess: 012
You entered: 012 0 1
2. Your guess: 021
You entered: 021 1 0
3. Your guess: 120
You entered: 120 0 1
4. Your guess: 345
You entered: 345 1 0
5. Your guess: 354
You entered: 354 1 0
6. Your guess: 367
You entered: 367 1 0
7. Your guess: 389
You entered: 389 2 0
8. Your guess: 398
You entered: 398 1 1
9. Your guess: 391
You entered: 391 2 0
10. Your guess: 319
You entered: 319 1 1
Better luck next time. Answer was 381.
Thank you for playing. Exiting...
Notes
You can run my solution to this program from within your CS account. Login and from the command line invoke the program as follows: ~reed/141/prog2
In writing your program make your output look exactly like mine (except the numbers chosen could be different), as we will be using automated scripts to grade the output of your programs.
For this program do not separate out your code into functions, but put it all in main().
Stages
Write your program in stages as follows, where the number of points for each stage (out of the 65 total possible points for "Runs correctly" combined with "Functional Decomposition") is shown:
(0 points) Create just the shell, with the identifying information and ending message. Running your program should look like:
Author: Dale Reed
Lab: Wed 5am
Program: #2, MiniMasterMind
Welcome to MiniMastermind.
The program selects 3 distinct random digits 0..9.
On each turn you guess 3 digits. Program indicates how many are correct.
You have 10 moves to guess the number. Good luck! In place Out of place
-------- ------------
Thank you for playing. Exiting...
(15 points) Use a random number generator to generate the computer-generated random values.
Up near the top of main() declare 3 integer variables (e.g. h1, h2, h3) to store these 3 hidden values, each of which will be a digit 0..9;
There are several pieces you will need to get the number generator to work. At the top of your program you will need to add: #include <stdlib.h> which is where the random number generator is defined. Before calling the random number generator for the first time, you need to seed the random number generator to start it out. The command to do this is: srand( seedValue); where seedValue should be (int)time(0) . Experiment with this sample program to see how the random number generator works.
Note that you should seed the random number generator only once near the beginning of your program. Then each time you want to get a random number, call rand() which returns an integer somewhere between 0 and the maximum integer value. To map this into our desired range of 0..9 use the mod (%) operator, as shown in the sample program above.
Add code to keep calling the random number generator process until all 3 computer generated random values (h1,h2,h3) are different. At this point display h1,h2 and h3 to make sure your program is working correctly. Running your program at this point should look like the following (though your number values will likely be different):
Author: Dale Reed
Lab: Wed 5am
Program: #2, MiniMasterMind
Welcome to MiniMastermind.
The program selects 3 distinct random digits 0..9.
On each turn you guess 3 digits. Program indicates how many are correct.
You have 10 moves to guess the number. Good luck!
Hidden values are: 381
In place Out of place
-------- ------------
Thank you for playing. Exiting...
(10 of the 15 points for this section) To help us to grade your program, at this point you must add some code that will enable us to hard-set the values that your program chooses. I don't expect you to understand exactly how this code works, but suffice to say that when starting your program from the command line it allows us to send in the values to be used. Change int main() to instead be: int main(int argc, const char* argv[]) Just before the line of code where Hidden values are: is displayed, add the following code, where the variables h1, h2 and h3 are the variables where your program stores the randomly chosen hidden values:
// If values supplied on command line, retrieve them for use into variables h1, h2 and h3 if(argc >1) { h1 = argv[1][0] - '0'; //convert char to integer value h2 = argv[1][1] - '0'; //convert char to integer value h3 = argv[1][2] - '0'; //convert char to integer value }
(Now if you run your program from the command line, rather than using randomly selected values you can set the values by supplying them after the program name. For instance if your program is called Prog2MiniMasterMind then you can set the hidden values to 123 by invoking it from the command line as: Prog2MiniMasterMind 123 )
(10 points) Prompt for and read in 3-digit user input for a guess.
You will need to declare an integer variable (e.g. userInput) for this, as well as 3 integer variables (e.g. g1,g2,g3) to store the 3 user guess digits we will extract (parse) from userInput.
To parse the individual digits from userInput use the % and / operators, similar to the example shown in class on how to make change (quarters, dimes, ...) from some amount of pennies. For example g3 = userInput % 10; would extract the right-most digit and store it into g3. Display the message "You entered: " followed by the 3 extracted digits, to make sure your program is working correctly. Running your program at this point should look like the following (though your number values will likely be different), where the user input of 012 is shown in bold:
Author: Dale Reed
Lab: Wed 5am
Program: #2, MiniMasterMind
Welcome to MiniMastermind.
The program selects 3 distinct random digits 0..9.
On each turn you guess 3 digits. Program indicates how many are correct.
You have 10 moves to guess the number. Good luck!
Hidden values are: 381
In place Out of place
-------- ------------
Your guess: 012
You entered: 012
Thank you for playing. Exiting...
(20 points) Count how many digits are correct and in place, and how many digits are correct but out of place.
You will need to declare two new variables for this (e.g. correctInPlace and correctOutOfPlace). These should be initialized to 0 in your program.
To do this use multiple comparison statements. For instance to see if the first user input character matches the first computer-generated hidden value we could use: if( g1 == h1) correctInPlace++;
Once all the comparison statements are in place, display them in their correct position. Running your program at this point should look like the following (though your number values will likely be different), where the user input of 012 is shown in bold):
Author: Dale Reed
Lab: Wed 5am
Program: #2, MiniMasterMind
Welcome to MiniMastermind.
The program selects 3 distinct random digits 0..9.
On each turn you guess 3 digits. Program indicates how many are correct.
You have 10 moves to guess the number. Good luck!
Hidden values are: 381
In place Out of place
-------- ------------
Your guess: 012
You entered: 012 0 1
Thank you for playing. Exiting...
Extensively test your program to make sure it is working correctly so far.
(10 points) Take the section of code that prompts for user input, parses out the individual digits, does comparisons and displays and enclose it in a counting loop. Display the loop counter as the move number when prompting for each user move. Running your program at this point should look like what is shown below (though your number values will likely be different), where the user input is shown in bold). Note that in this version the program does not detect whether or not a solution has been found, giving the appropriate ending message.
Author: Dale Reed
Lab: Wed 5am
Program: #2, MiniMasterMind
Welcome to MiniMastermind.
The program selects 3 distinct random digits 0..9.
On each turn you guess 3 digits. Program indicates how many are correct.
You have 10 moves to guess the number. Good luck!
Hidden values are: 381
In place Out of place
-------- ------------
1. Your guess: 012
You entered: 012 0 1
2. Your guess: 021
You entered: 021 1 0
3. Your guess: 345
You entered: 345 1 0
4. Your guess: 354
You entered: 354 1 0
5. Your guess: 321
You entered: 321 2 0
6. Your guess: 678
You entered: 678 0 1
7. Your guess: 687
You entered: 687 1 0
8. Your guess: 327
You entered: 327 1 0
9. Your guess: 381
You entered: 381 3 0
10. Your guess: 381
You entered: 381 3 0 Thank you for playing. Exiting...
(10 points) Remove the debugging line of code that displays the hidden values. Near the bottom of your main loop, add code to check if the solution has been found. If so, display the message
*** Congratulations! You did it! *** and break out of the loop. If it is the last (10th) move and the solution has not been found, display the message Better luck next time. Answer was nnn where nnn are the 3 hidden solution digits that the user was unable to find.
Turn in your program (the .c or .cpp file only) using Blackboard into assignment Prog 2 TAname where TAname is the name of your TA. For instance if your TA is Janet, then you would turn your program into assignment Prog 2 Janet.
15 points Extra Credit (very hard!): Write a program to flip the game around, where you as the human come up with 3 digits, and the computer has to guess them from you, where on each move you indicate to the computer how many digits are in place and how many are out of place. Please don't bother turning in the extra credit unless the computer correctly guesses the numbers most of the time.