C. Program Description for Developers

Connect 4

First, use the input function to let users perform a series of operations, including choosing whether to play the game, choosing the color of the pieces, and entering the players’ names. When everything is ready, print a 7 × 6 board using the simpleGameEngine function, and use different variable names to represent empty sprites, red chips, and black chips. After that, the program enters a while loop, and the condition for jumping out of the loop is the variable winner == 1, that is, a player wins or draws. First preset winner = 0 to enter the loop. The variable color is changed between 0 and 1 to control the alternate placement of red and black chips. The placement of the chips is completed by the function getMouseInput and clicked directly by the mouse. The first chip (usually the red one) at the beginning of the first round of each game must be placed directly at any position in the bottom row, controlled by a while loop, and an error will always be reported if it is not placed as required. Subsequent chips are clicked on any empty position without repeated placement. The variable count is used to calculate the number of empty sprites under the clicked position. After that, the chip is automatically dropped to complete the placement. After each placement, four for loops will be used to check whether there are four consecutive chips of a certain color in a row or column or diagonal in the entire board. If exists, jump out of the loop and the game ends; if not, continue to place chips. If the game continues until the entire board is full (no one won before the board was filled), the system determines that the game is a tie, and it jumps out of the loop and the game ends.

 

List of variables:

 

playedBefore – Store input of 0 or 1 entered by the user to check if user wants to play or not

errordlg – built in function to show an error message

msgbox – built in function to display a message box

player_1 & player_2 – stores the name of each player.

empty_sprite – used to store the state of each spot in the grid. 

red_sprite – stores the red chip state assigned to player 1

black_sprite – stores the black chip state assigned to player 2

color – to determine the color of a chip player, assign 1 to red and assign 0 to black.

winner – when winner = 1, there is a winner or the board is full(even).

[r,c] – vector to store the location of mouse input

diagonalA & diagonalB – used to keep track of diagonal pattern for winning conditions

 

Given MATLAB Game Commands

  • simpleGameEngine –  a program given to the programmers that allows different pictures to be displayed
  • drawScene – a vector of sprite IDs that will be what allows the programmers to print the board to the screen
  • getMouseInput – gets the row,column of where the user clicks on the board
  • connectFour – a picture/vector of a blank space, a space filled up with a red chip, and a space filled up with a black chip that allows the programmer to display the board and create the visual that is desired

 

References:

  • “Connect Four.” ConnectFour.docx | Powered By Box, app.box.com/embed/s/8lhqjr7n38tdmjzvppxwztlbco8gxjm5?view=list&sortColumn=name&sortDirection=ASC&showItemFeedActions=true&showParentPath=true.
  • “MATLAB.” MathWorks, www.mathworks.com/products/matlab.html.
  • “SimpleGameEngine.m.” Box, osu.app.box.com/s/3l9u97bhp673lh3e1coyx8t6tjjxlp9y.
  • “Software Design Project.” Box, osu.app.box.com/s/758htgbd1uuruzhjdp4emn3clkcqobe0.

 

Memory

Using the input function, when the user first loads up the program and begins to play, they are asked whether or not they know how to play memory (0 meaning no, 1 meaning yes). If they enter a 0, then the rules are printed out to the screen for the user using fprintf. If they enter a 1, then it goes straight to the next step of the game because the program reads this as the user already knowing how to play, thus not needing rules. Next, using a while loop, if the user enters anything other than a 0 or 1, an error message will print out and they will continuously be asked to re-enter a 0 or 1 until they enter one of them. After this, a bunch of variables are created (listed below with their uses) in order to create the board/the desired card arrangement. At the beginning of the game, 4 rows of 13 purple face down cards are shown. A big while loop with the condition of while endLoop ~= 52 is utilized in order to keep the game running until the user wins. This while condition basically means that as long as the board still has face down cards (the user has not yet won), all of the code within the loop will repeat — the variable endLoop is incremented at the very end of the loop and will reach 52 if the cards are face up, which will end the loop. At the beginning of the loop the getMouseInput (a function given to the programmers) is taken for the user’s first guessed card. The card is flipped over from the purple card back to the face up card at this position. Then, the user’s getMouseInput was taken for their second guess and the card was flipped over. The two cards that were flipped over were checked against each other using a relational operator (==) to see if they were equal. If they were, then they stayed face up and a message box (msgbox) was displayed to the user telling them that they found a match. However, if they were not a match, then the cards went back to face down and a message box was displayed to the user telling them that they did not find a match. Using nested for loops, every time the user finds a match, the board is checked to see if every card is face up. If this is true, then this signifies that the game is over and a winning message prints out, along with a print out of the amount of pairs of guesses that it took the user to win the game. Since all of this code is inside of the original while loop, this process continues all the way until the user gets all of the cards face up and wins the game.

 

List of variables:

my_scene – This utilizes the given simpleGameEngine program and the retro cards picture in order to create a matrix of sprite IDs that will be used every time the board needs to be printed out

empty_sprite – used for the creation of the card_displayFace and card_displayBack vectors

card_spritesFace – holds the index of all of the desired cards

card_displayFace – use the given ones function and the empty_sprite variable to create card_spritesFace, which is a vector that is used to hold the faces of the cards and create the final vector to be printed to the screen

card_spritesBack – holds the index of the purple card back, which is what is displayed for a face down card

card_displayBack – use the given ones function and the empty_sprite variable to create card_spritesBack, which is a vector that is used to hold the backs of the cards

count – used for incrementation and creation of the vector for the back of the cards. Using a while loop (while count is less than 53), every element in the card_displayBack vector is set to the first element in the card_spritesBack vector, which is the purple card back

cardBack – a matrix of 4 by 13, where all of the elements are the purple card backs. This vector is the beginning game board and is altered throughout the program anytime the user finds a match. This is the vector that is used with the my_scene variable within drawScene to print out the game board

cardBackDuplicate – a duplicate of the cardBack vector that is created so that wrong guesses can be set back to the face down card

numGuesses – incremented every time the user chooses a pair of cards to guess. Is printed out at the end of the program in order to show the user how many guesses it took them to win the game.

boardFace – a vector of randomized order of the first 26 elements within card_displayFace

boardFaceTwo – a second vector of randomized order of the first 26 elements within card_displayFace

boardFaceFinal – boardFace and boardFaceTwo are joined to create this 52 element vector with 26 matches. This vector is used to create the final face up matrix, cardFace, which is the board when every card is face up.

cardFace –  a 4 by 13 matrix of all of the cards face up

endLoop – used to end the big loop/program. When endLoop is 52 (all of the cards are face up and the user has won), the game will end.

[row, col] – the row and column of the user’s first guess. Is used for making the user’s first guess to be face up

firstGuess – used for checking against the user’s second guess to see if the user chose a match

[r, c] – the row and column of the user’s second guess. Is used for making the user’s second guess to be face up

secondGuess – used for checking against the user’s first guess to see if the user chose a match

 

Given MATLAB Game Commands:

simpleGameEngine program – a program given to the programmers that allows different pictures to be displayed

retro_cards – a picture/vector of cards that allow the programmers to display the card faces and create the visual

drawScene – a vector of sprite IDs that will be what allows the programmers to print the board to the screen

getMouseInput – gets the row,column of where the user clicks on the board

 

References:

  • “Card Games.” CardGames.docx | Powered By Box, app.box.com/embed/s/vj8925y7u64r2saowy6ka4q96hwty8vx?view=list&sortColumn=name&sortDirection=ASC&showItemFeedActions=true&showParentPath=true.
  • “MATLAB.” MathWorks, www.mathworks.com/products/matlab.html.
  • “SimpleGameEngine.m.” Box, osu.app.box.com/s/3l9u97bhp673lh3e1coyx8t6tjjxlp9y.
  • “Software Design Project.” Box, osu.app.box.com/s/758htgbd1uuruzhjdp4emn3clkcqobe0.

 

Over/Under Seven

The coding for Over/Under Sevens consisted mostly of while loops and relational operators. First we had an input command asking if the user knows how to play the game. We then created a while loop to ensure that a valid input was entered, either 0 for no or 1 for yes. Next, an if statement was used to print out the rules if the user does not know how to play. If the user chooses to play, their cash amount they have to bet is printed in the command window. An input command asks the user if they want to play, and a while loop is used to make sure a valid input is entered. Another while loop was created on the conditions that the user wants to play and they have cash available to bet. The sum and randi commands were used to find the sum of two randomly rolled die. Next, an input statement asks the user how much they would like to bet, and a while loop is used to make sure the bet is valid (greater than zero or less than their total cash). If the user does not input a valid bet, an error message will be displayed and the program will ask the user to re-enter a valid bet. Furthermore, an input command asks the user what they would like their bet type to be (H,L, or S). We used a while loop to make sure a valid bet type is placed. After that, we used an if statement to see if the user wins or loses money by betting on H or L. If the user wins then their bet amount is added to their total cash. An elseif statement is used if the user bets S and wins because if that is the case then the users bet amount is multiplied by 4 and added to their total cash. Finally, an else statement is used if the user loses; Their bet amount is subtracted from their total cash. An input command then asks the user if they would like to play again and a while loop is used to make sure the user enters either 1 (yes) or 0 (no). If the user chooses to play again the previous code is repeated and if the user does not choose to play again a departing message is printed and the program ends. An if statement is used if the player runs out of money. If this is the outcome, a message is printed and the game ends.

 

List of variables:

playedBefore – stored info (1 or 0) if the user has played the game before

totalCash – total cash that the player has available to bet

again – stored info (1 or 0) if the user wishes to play

sum – stored sum of two randomly rolled die

amountBet – input of what amount the user chooses to bet

betType – input of what the user is betting on (H,L, or S)

 

References:

  • “MATLAB.” MathWorks, www.mathworks.com/products/matlab.html.
  • “Software Design Project.” Box, osu.app.box.com/s/758htgbd1uuruzhjdp4emn3clkcqobe0.