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

/* * Play a memory game with three LEDs and three buttons. * The game will blink out a pattern of lights. Each light corresponds * to a button in the game. Try to 'play back' the pattern to the game. * Get it right and you'll hear a beep and move on to the next (harder) * level. Get it wrong and you'll hear a deep tone and start over. */ byte computer[10]; //array to hold the computer's moves up to 10 moves byte player[10]; //array to hold player's (your) moves up to 10 moves byte computersTurn = true; byte numGuess = 0; //count guesses and hold them to compare to the computer's byte level = 3; //how many lights in the sequence player has to copy const int wait = 500;//pause between each play. Change for faster/slower play const byte speaker = A5; //speaker pin A5 //When player presses button, put it in the array in order void setPlayer(byte _guess) { player[numGuess] = _guess; digitalWrite(_guess,HIGH); delay(300); digitalWrite(_guess,LOW); numGuess++; //move to the next position in array of player's guesses } void setup() { pinMode(4, OUTPUT); //LEDs to display moves pinMode(5, OUTPUT); pinMode(6, OUTPUT); pinMode(speaker, OUTPUT); //Buttons pinMode(0, INPUT_PULLUP); //corresponds to pin 4 LED pinMode(2, INPUT_PULLUP); //corresponds to pin 5 LED pinMode(A0, INPUT_PULLUP); //corresponds to pin 6 LED } void loop() { //Assign random numbers 4, 5, or 6 to computer's array if (computersTurn == true) { for (int i = 0; i < level; i++) { computer[i] = random(4, 7); } } //Write moves from the computer[] array to LEDs for (int i = 0; i < level; i++) { digitalWrite(computer[i], HIGH); delay(wait); digitalWrite(computer[i], LOW); delay(wait); } computersTurn = false; //Swap to player's turn //Put player's guesses into player's array via setPlayer() function //Keep accepting guesses until number of guesses = the level player is on while (numGuess < level) { if (digitalRead(0) == LOW) { setPlayer(4); } if (digitalRead(2) == LOW) { setPlayer(5); } if (digitalRead(A0) == LOW) { setPlayer(6); } } //Check if player array matches computer's array for (int i = 0; i < level; i++) { if (player[i] == computer[i]) { tone(speaker, 950); delay(50); noTone(speaker); } else { //if the arrays don't match, play the low tone and restart tone(speaker, 150, 1000); level = 2; } } delay(2000); //wait after player's placement before the computer's turn computersTurn = true; numGuess = 0; //reset the number of player guesses to 0 level++; //increase the number of lights the computer will show by 1 //If the player beats 10 levels, victory display and reset game! if (level > 10) { for (int i = 0; i < 80; i++) { tone(speaker, 950); digitalWrite(5, HIGH); digitalWrite(6, LOW); delay(20); tone(speaker, 550); digitalWrite(6, HIGH); digitalWrite(5, LOW); delay(20); noTone(speaker); } level = 3;//now reset back to level 3 after victory } } // Try to adjust the value of 'wait' after each level in order to make // the computer place its values faster each time // Feeling ambitious? Try to add in a timer so that the player has only a // limited window to put in their answers before they automatically lose! // (c) 2017 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

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!