Memory Game
Step 1 - Build the Project
In order to play the memory game, the code has to decide some values for the computer's moves, then store them and wait for the player’s moves. Once the player has entered their moves, the code needs to check the moves and see if they match the computer's moves. If the moves all match, the next round begins. If they don’t match, the player loses and needs to start over. Storing and checking these values is simple to do with arrays. Arrays are basically lists of variables that are all stacked together.
Step 2 - Upload the Code
Step 3 - Read the Walkthrough
The first step in the code is to tell the code that you will have an array, give it a name, and then tell the code how many values will be in the array. For this game, there are a maximum of 10 moves that the computer can make, so you can make the array named computer [10] integer values long.
Since the player needs a place to store their values, player array [10] should hold 10 integers, too.
Next, you create variables for whose turn it is, which guess number the player is making, and how many moves the computer made (i.e. the level of the game). Since there are many delays in the code, an integer ‘wait’ will easily allow you to adjust all of the delays in one place.
The setPlayer function takes in the player’s guess and puts it in a slot in the array. Then it blinks the LED corresponding to the guess and shifts over one spot in the array, waiting with an empty slot for the next guess. The ‘byte _guess’ variable is what the function expects to receive: a byte value from the loop. It will rename that byte ‘_guess’ for the function.
In this code’s setup(), each LED and the speaker should be set as an OUTPUT. Each button corresponds to one of the lights that you will hook up. Make sure you read the comments closely or your button presses may not do what you expect them to!
The first section of the loop uses a ‘for’ loop to insert random values into the computer's array. The first item in an array is item 0, so a random value 4, 5,or 6 is put into the position 0 when i = 0. Then i increases to 1 and picks a random value to put in position 1 of the computer's array. This continues until the array position equals the variable ‘level’.
Next, the code goes back and reads from computer's array and lights up the pin that corresponds with the value in that position in the array. For example, if computer’s first move is 2, then computer[0] holds the value 2. So when the ‘for‘ loop creates the variable ‘i’ and says go to computer[i], it will first go to computer[0], get the 2, and run the ‘for’ loop with the value 2, then increase the value of i and check the array again. Once the entire array up to the number ‘level’ has been flashed out, the variable computerTurn becomes false and the player gets to enter their guesses.
As long as the number of guesses is lower than the level the player is on, the code will wait for user input. When the user does press a button, the value that button corresponds to is sent up to the setPlayer() function and renamed _guess. Then setPlayer goes to player [0] and puts in the value of _guess there, flashes the corresponding LED, and moves to the next position in the array, waiting for the next guess. This process repeats until the guesses from the player = computer’s moves.
Now that both computer’s moves and the player’s moves are in their arrays, it’s time to compare them to each other. The ‘for’ loop ensures that the number of comparisons made between computer and the player is equal to the variable ‘level’. For level 6, there should be 6 comparisons, and so on. The ‘if’ loop inside the ‘for’ loop takes value player[0] and checks if it equals computer[0]. If that check is true, the next check is player[1]==computer[1] and so on, until the number ‘level’ has been reached.
If the check is not true, the code drops to the ‘else’ condition where a sad tone is played and the level is reset to 2.
If the player matched all of computer’s moves, the variable computersTurn becomes true again, the player’s guesses are reset to 0, and the level variable increases by one, so computer's turn and the player’s turn will last one move longer than the previous round.
The final ‘if’ condition of the code only occurs if the player has just matched 10 of computer’s moves, pushing the level to 11. When that happens, a victory ‘for’ loop starts, running through a series of lights flashing and tones beeping for 80 cycles. Good luck getting to victory!