Memory Code Description

Memory:

List of variables:

Shuffledeck

memorycards2

n = 1;

count=1;

counter=12;

memorycards3

flippedCards

BlueDeck

row1

row2

 

Functions-

Imshow()- used to show the images in a vector

Randperm()- creates a random permutation of numbers from 1 to the input

The code begins with the creation of the vector “shuffledeck” which contains random numbers from 1 to 12 generated using the function “randperm()”. A vector called “memorycards2” is created with length 24 at this point it does not matter what values are in the vector. First half of “memorycards2” (cells from 1 to 12) is populated with values from “shuffledeck” using a for loop. Another random permutation of values between 1 and 12 is created and stored in “shuffledeck”. The other half of “memorycards2” (cells from 12 to 24) is populated with the new values in “shuffledeck” using a for loop.

Another vector is created called “memorycards3” to store the images in the same order as the values in “memorycards2”. The value stored in “memorycards2” is used as the index value for “BlueDeck”(contains the images of the cards). The value obtained from “BlueDeck” is stored in the same index as “memorycards2”. This process is completed through a for loop that runs to the 24. Next, the vector “flippedCards” is created with contains 24 images of the flipped card obtained from “BlueDeck”.

A variable is created called “counter” with the value 12. A while loop is created that runs until the value of counter is above 12. This is done so that the game runs until all the pairs are found. “row1” and “row2” are values that store the position of the card entered by the user. If the value of either “row1” or “row2” are out of range or their values are equal, a message of “invalid Input” is displayed to the screen. It is important to note that the “continue” command causes the loop to return to its starting position. Thus, it is a useful command in this situation. The values of “row1” and “row2” are then used as index values in “memorycards2” and used to check whether the values in those cells are equal. If these values are equal a message of “Awesome” is displayed to the screen and counter is decreased by 1.Continueis used again to return to the beginning of the code. If the none of the above conditions are met “Sorry try again” is displayed to the screen. The game runs to completion until “counter” =0.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *