Write a program to play the memory game. A 6 x 6 square of Xs and Os are shown on the screen. The user tries to memorize the board, then another person chooses one position to flip (O or X or X to O) and the user tries to figure out which one it was.
See the following video showing how the program works. Note that it is designed for you to play with a friend, and you will have to look away from the screen for parts of it.
Playing the game looks like the following, where user input is shown in bold:
Author: Dale Reed Class: CS 141, Spring 2018 Lab: Mon 5am Program: #2, Memory Game Welcome to the memory game! Look away from the board and have a helper enter r to randomize the board until they have a random board that they like. Then you glance at it and try to imprint it in your mind and look away. Your helper will then select a single piece to be flipped by choosing its row and column. The changed board is then displayed. You then must try to guess which one it was. Enter x to exit the program. 1 2 3 4 5 6 - - - - - - - - A | X X O O X O | A B | X O O O X X | B C | X X O O O X | C D | O X X X X X | D E | X O X X O O | E F | X O X X O O | F - - - - - - - - Enter r to randomize to board, or row and column to change a value -> R 1 2 3 4 5 6 - - - - - - - - A | O X O X X O | A B | O O X O X X | B C | O O X O O O | C D | O X O O O O | D E | O O X O X X | E F | X X O O O X | F - - - - - - - - Enter r to randomize to board, or row and column to change a value -> r 1 2 3 4 5 6 - - - - - - - - A | O X X O X O | A B | O O O X O O | B C | O X O X X O | C D | O O O X O O | D E | O O O X X X | E F | X X O X O O | F - - - - - - - - Enter r to randomize to board, or row and column to change a value -> D 4 1 2 3 4 5 6 - - - - - - - - A | O X X O X O | A B | O O O X O O | B C | O X O X X O | C D | O O O O O O | D E | O O O X X X | E F | X X O X O O | F - - - - - - - - What piece do you think it was? -> b 2 Sorry, it was D4. Better luck next time. Thank you for playing. Exiting...
Playing the game again might look like:
Author: Dale Reed Class: CS 141, Spring 2018 Lab: Mon 5am Program: #2, Memory Game Welcome to the memory game! Look away from the board and have a helper enter r to randomize the board until they have a random board that they like. Then you glance at it and try to imprint it in your mind and look away. Your helper will then select a single piece to be flipped by choosing its row and column. The changed board is then displayed. You then must try to guess which one it was. Enter x to exit the program. 1 2 3 4 5 6 - - - - - - - - A | X X O O X O | A B | X O O O X X | B C | X X O O O X | C D | O X X X X X | D E | X O X X O O | E F | X O X X O O | F - - - - - - - - Enter r to randomize to board, or row and column to change a value -> r 1 2 3 4 5 6 - - - - - - - - A | O X O X X O | A B | O O X O X X | B C | O O X O O O | C D | O X O O O O | D E | O O X O X X | E F | X X O O O X | F - - - - - - - - Enter r to randomize to board, or row and column to change a value -> r 1 2 3 4 5 6 - - - - - - - - A | O X X O X O | A B | O O O X O O | B C | O X O X X O | C D | O O O X O O | D E | O O O X X X | E F | X X O X O O | F - - - - - - - - Enter r to randomize to board, or row and column to change a value -> e1 1 2 3 4 5 6 - - - - - - - - A | O X X O X O | A B | O O O X O O | B C | O X O X X O | C D | O O O X O O | D E | X O O X X X | E F | X X O X O O | F - - - - - - - - What piece do you think it was? -> e1 *** Congratulations, you did it! *** Thank you for playing. Exiting...
Your program must use 36 char variables named p0..p35 to represent the board. They must be used in printing the board and in being checked to validate parity. These board characters may be declared globally.
The secret to this program is that there are an odd number of Xs in each row and column on the board. This is known as odd parity. When a single position is changed, then the corresponding row and column for that position now have an even number of Xs, thus giving away the position for this single position that is changed.
You may not use arrays, or strings, or any other method to circumvent the need to handle fundamentally using the 36 variables throughout your program. Failure to follow this constraint will result in a 20 point deduction. (Hint: Think about how you would write the program if you were using arrays. Consider how you might instead use a function in each location where otherwise you would use an array. If you figure this out, please do not give it away on Piazza.)
Input for 'x' to exit, 'r' to randomize, or row letter should be accepted as either upper or lower case. The row letter and column number should be accepted when entered right next to each other as shown in the example above, or separated by a space. Note that your program needs to be able to handle either a single input ('r' or 'x') or two characters of input (e.g. "C5"). To do this you should have a cin statement to read the first character. Then check it. Only if it is not 'r' or 'x' then read a second character using cin.
After the helper enters two-digit input to select which position to change, your program should print 25 blank lines, so that the original board scrolls off the top of the screen and is no longer visible.
To initialize the board variables use rand() to generate a random number. You should #include <cstdlib> at the top of your program for rand() to work. Consider how you would use rand() to initialize board variable p0. You would generate a random number and then check to see if it is even or odd (using the mod operator). If the random number is even, set the board variable p0 to 'O'. If the random number is odd, set that board variable p0 to 'X'. Repeat this for each board variable, starting with p0 and ending with p35. Only after this is done should you display the board for the first time. When you display the board you can use cout with a string literal (e.g. cout << " 1 2 3 4 5 6 7 \n"; ) to display the parts around the edges, but for the interior section you should be printing the variables (e.g. cout << p0 << " " << p1 ...) that contain the previously set 'X' or 'O' values. I suggest generating and displaying the board like this should be the first part of the program that you write.
In the final stage where you ask the user "What piece do you think it was?" your program does not need to handle 'x' to exit.
The name of the program you will turn in should be your netid followed by Prog2 and the .cpp file extension. In other words, if your netid is reed then your program would be called reedProg2.cpp
Blackboard considers program source code as a threat and sometimes replaces random parts of it. To avoid this you must also zip up the code you turn in. To zip a file right-click on the single .cpp file to be turned in (e.g. reedProg2.cpp) and select "Send To" and then select the "compress" menu option. On a Mac simply right-click and then select the "compress" option. The resulting file (for instance) will then be called reedProg2.zip
Only turn in this single file, turning it in on Blackboard into the Assignment Program2: Memory.
Failing to follow these naming conventions or failing to turn in a zip file will result in a 5 point deduction. (Note that I am not requesting some compression, but specifically zip compression).
You may turn in multiple versions, but only the latest version will be graded.
Inside your program itself you must include the following grading rubric, just below your header documentation, as shown below. We will use this when grading your program as a place to annotate and list any deductions taken off within each category. Note that this is in your code but is within a comment block so should not appear on the screen when you run your program.
/* ------------------------------------------------ prog2memory.cpp Program #2: Memory game of guessing which X or O changed. Class: CS 141 Author: Dale Reed Lab: Tues 5am System: C++ Mac Xcode Grading Rubric: 50 Execution points 5 Displays header info on screen when run 5 Displays instructions 5 Output is formatted as shown in sample output 3 Handles both upper and lower case input 2 Input of 'x' in first prompt exits program 5 Input can be adjacent or have spaces between them 15 Displayed boards all have odd parity in rows and columns 5 Repeated input of 'r' gives new valid random board each time 5 Gives appropriate end of program messages 45 Programming Style (Given only if program runs substantially correctly) 5 Program naming convention is followed 10 Meaningful identifier names 10 Comments: Has header. Comments on each block of code 10 Appropriate data and control structures (-20 if using arrays or strings) 10 Code Layout: Appropriate indentation and blank lines ------------------------------------------------ */
Note that the last 5 points for credit on this program will come from your peer review. Instructions on what to do for this will be posted in Piazza after the program deadline.