Prog 1: Guess01
Addition made on 9/4: Sample output shown for Stage 0
Clarification made on 9/9 shown in blue.
Can you write a program that can outsmart you? You might be surprised! Write a program to play a 0/1 number guessing game against the computer.
At each move first the computer makes a forecast of 0 or 1, then you make your guess. The score starts at 0. If the computer correctly forecasts (matches) your choice, we subtract 1 from the score. If the computer doesn't match your move, we add 1 to the score. The computer wins if the score reaches -10. You win if the score reaches +10.
A sample of this game (written in Scratch) is shown below. For the best experience play it here.
Our version will not be graphical but will be text-based, and should be written in stages. While generally poor form, we will be extensively allowing the use of global variables for this first program. If you know how and prefer not to use global variables for this program, you can do so, but don't have to. Of the 55 points available for execution, different numbers of points will be awarded depending on how far you get.
Stage 0 (0 points)
Create a Qt Creator project in C, as demonstrated in class on 8/31. (Watch the class video through Echo 360 on Blackboard if you don't remember this.) The name of your project should be your netid. Add the code to:
Display your identifying information
Display program instructions
Declare global variables for computer forecast, score and user input. (Global means after your #include <...> statements but before any functions.) Within main() set the computer forecast to be the value 1 and set the score to be 0.
Prompt for and get user input.
Compare the computer forecast to the user input and update the score. If the computer forecast matches then subtract 1 from the score, otherwise add 1 to the score. Display the new score and exit the program.
Running this program should look like the following (but with your own information), where user input is shown in bold:
Program #1: 0 or 1 number guessing game
Author: Dale Reed
Lab: Tues 8am
Instructions:
For each move the computer will forecast what it thinks you will enter.
When prompted you then enter 0 or 1. The computer forecast and your
input will be compared. If the computer got it right, the score is
decremented. If the computer got it wrong, the score is incremented.
The score starts at 0. If it reaches -10 the computer wins.
If it reaches 10, the human wins! Good luck, you'll need it!
1. Score is: 0
Computer has made forecast.
What is your input (0 or 1): 1
Computer's forecast was 1.
Final score is: -1
Exiting program...
Note that if the user input instead of 1 was 0, the score would have gone up to 1 instead.
Stage 1 (5 points)
Use a random number generator to make the computer forecast. Do the following:
Rather than just hard-coding the computer forecast, now use a random number generator to set the initialize computer forecast to be 0 or 1. If you can't remember how to use a random number generator, refresh your memory from Zyante section 2.16.
Add a loop so that the program can continue to make moves. Each time through the loop your program should again use a random number generator to make the computer forecast, until the user enters input of -1, which should display a message and exit the program.
Stage 2 (15 points)
Use the possible values from the single previous user move to make the computer forecast.
Create a new previous move variable. Just before reading in the user's input on each turn, the current user input value should be stored into the "previous move" variable. When the user's previous move was 0 (row 1 in table below), consider how many times the user then followed this by choosing 0 (call this situation 00 ). Similarly consider how many times the user instead then followed this by choosing 1 (call this situation 01 ). Create two global variables to store these values as play progresses, and similarly create and use two more global variables ( 10 and 11 ) for the case where the user's previous move was 1 (row 2 in table below). Call these the table variables.
To make the computer forecast, use the user's previous move to determine which row in the table matches the current situation (row 1 or row 2). For instance if the previous move was 1, then this corresponds to row 2. Look at the values of the two variables in row two that represent how many times after a move of 1 the user then chose 0 , and how many times the user then chose 1 . Whichever of those two is the larger number is the one the user is most likely to guess, based on past history. If the variable corresponding to 10 is larger than the one for 11 , then the computer forecasts 0.
Once the computer forecast has been made, the user makes her/his move and the program adds 1 to one of the 4 table variables. As play progresses the values in each row that are larger should indicate the choices the user is more likely to make. If there is a tie then simply choose the opposite of the user's last move.
Stage 3 (15 points)
Store and use the previous two moves to make the computer forecast. You should create two global variables for these previous two moves. There are 4 combinations of the previous two moves, leading now to 4 table rows. For each of these 4 combinations store how many times 0 was chosen by the user in that situation, and store how many times 1 was chosen by the user in that situation. Each of these will again be a global variable, for a total of 8 variables. Consider the following table to represent these values:
Initialize Move 2 and Move 1 to both be 0. Create a variable to store the user input. When the user makes a move, "shift these values left" by storing the old Move 1 value "left" into Move 2, and the new user input into Move 1. Use the Move 2 and Move 1 values to determine which row in the table you care about. As in the previous stage, then compare the variables that store the history for that row, identifying the largest of the two to make the computer forecast.
Stage 4 (20 points)
Store and use the previous three moves to make the computer forecast. There are 8 combinations of the previous three moves, leading now to 8 table rows and 16 variables. Follow the same approach as in the previous stages, but this time filling in the table information for all 16 variables.
Turning In Your Program
Remember that the name of your project should be your netid. Zip up your entire project and turn it in to Blackboard into the Program 1 assignment. Be sure to review the grading criteria on the course syllabus, particularly regarding variable names and comments.