C. Program Description for Developers

Memory

The program begin with “clear” and “clc” to clear all variables and the command window. The 2×55 matrix “CardDeck” is then loaded containing all the images for the cards used in the game figure (A link to CardDeck can be found in the references section). Next is the algorithm to create the array of the card values, which is named “cards”. First, a for loop is used which runs five times. Within the loop, a random number between 1 and 54 is generated, corresponding the the 54 possible cards that can be used to represent the values of the cards. This value is then added to the end of the cards array. Once there are five cards in the cards array, a for loop, which runs from 6 to 10, essentially adds the cards array to itself, creating an array of length 10 and 5 pairs of values. This is done using two different indexing variables, i and j. I is used to index the index which the value is going to be added to, while j is used to get the value of cards that is to be added at position i. Next, the values of cards are shuffled to give the random appearance, which is done using cards = cards(randperm(length(cards)));. Next, a new array called flippedCards is creating which consists of 10 elements, all of which is 55. This is because the number 55 is the index that corresponds to a face down card in the RedDeck subset of the CardDeck matrix. The function imshow([RedDeck{flippedCards}]) is then used to to create the game figure using the values from RedDeck, at the indexes given by the numbers contained in the flippedCards array. At this point they are just 10 face down cards. A textbox is then created underneath the cards in the game figure which labels the cards 1-10 using the annotation function. The combination of the imshow and the annotation function appear many times in the game, so we will refer to them as “updating the game figure” from now on. In summary, the cards array contains the actual numerical values of the 10 playing cards and does not change throughout the game. The flipped cards array contains either a 55 if the card is face down, or the number which corresponds to the correct face up card image in RedDeck, and changes with user input. Now the game begins.

The entire game is played within a while loop which runs while the variable game is equal to 1 (game is initialized to equal 1). The user is prompted to select a card 1-10 and the value is stored in the variable flipCard. The element of the array flippedCards at index flipCard is then set equal to the value of the element in array cards at position flipCard. In other words, the card which the user wants to flip (which is controlled by the flippedCards array) is now equal to its face up value, instead of 55, the face down value. the game figure in then updated. The variable temp is then set equal to the value of flipCard. The user is then prompted to enter another card 1-10, and a while loop will continue to prompt the user while the value of temp is equal to the new value of flipCard. Once again, the element of the array flippedCards at index flipCard is then set equal to the value of the element in array cards at position flipCard and the game figure is updated. An if statement is then used with the condition of if cards(temp) is not equal to cards(flipCard) and the code within the if block resets the elements of flippedCards at indices temp and flipCard back to 55, effectively flipping them back over if they don’t match. If the values do match, a variable called matches (initialized to be 0 at beginning of program) is incremented by 1. If matches equals 5, game is set equal to 0 which will break the loop, print “You Win!” to the console and end the game, otherwise the loop repeats. Note that if the cards do not match, the game figure does not update until another value is entered. This gives the player plenty of time to memorize the value and location of the cards.

 

Hangman

The program begins by creating the variable “word”. This variable is created by an input that stores the word that the player has to guess. Next, the variable “Array” is initialized to turn “word” into a character array. The variable “len” finds the length of the character array. Next, the arrays “un” and “chr” are initialized along with “correct” which is set to zero which represents the number of correct letters the player guesses. The variable l is set equal to the length of “chr” a variable that represents the character array that is replaced by stars to hide the correct word. The “chr” array is then looped based on the length of the word to display the number of stars corresponding to the number of letters. Then the star array is displayed. Next, the number of guesses is looped in a while loop that displays each time the player guesses a new word which is represented by “m”. The number of guesses only changes if the player guesses an incorrect letter. Within the loop, the “un” array is used to replace the stars in the “chr” array when a correct letter is guessed. The variable “un” concatenates the correct letter with the “chr” array to display the letter(s). Next an if loop is used to determine whether the inputted letter is in the character array. If it’s not, it prints out that you did not guess the correct letter. If the letter is contained in the character array, it will print out that the letter is correct. The counter “correct” adds 1. “m” subtracts 1 so that the guess is not considered incorrect and will not add the guesses loop. After all 9 incorrect guesses are looped through, the correct word is displayed.