C. Program Description for Developers

Connect 4

The Connect 4 program requires just two files: Connect4.m and Connect.mat (can be found here). Connect4.m first loads in Connect.mat and displays the empty board. A matrix called matrix is created that keeps track of the locations of each players pieces. At the start, matrix is a 6×7 array with a zero in each position. The variable noWinner is introduced and set equal to one as well. This variable keeps track of if either player has won yet.

Next, the game begins. A while loop is started that continues until noWinner no longer equals one. A menu is displayed that asks player 1 to select one of the 7 columns. The variable column is set equal to their selection. After Player 1 selects a column, the variable row is set equal to 6. A while loop is started that checks the position in matrix (row,column). If that position does not equal zero, row is decreased by 1 and the next position is checked. The loop continues until a position is found in which the value is zero. The position in matrix (row,column) is set equal  to one to indicate that player 1 has a piece there. Then, a black piece is added to the board and the updated board is displayed.

After Player 1 takes their turn, the program checks for a win. The variable Winner is created and set equal to zero. This variable will indicate which player is the winner. The variable numinCol is also introduced and set equal to zero. This keeps track of the number of pieces connected in the column. A for loop is started that goes from i=1 to 1=6. An if statement checks if matrix(i,column) is equal to 1. If it is, numinCol is increased by 1. Another if statement checks if numinCol has reached 4. If it has, Winner is set equal to 1. An elseif statement sets numinCol equal to zero if matrix(i,column) is not equal to 1. To check for a horizontal win, the variable numinRow is created and set equal to zero and a for loop starts from i=1 to i=7. Like for a vertical win, within the for loop is an if/elseif statement that counts up the number of pieces connected in the row and another if statement that sets Winner equal to 1 if numinRow reaches 4. Then, the program checks for diagonal wins in a “/“ shape and in a “\” shape. In either case, a nested for loop checks every possible diagonal win. If either loop finds a diagonal win, Winner is set equal to 1.

At this point, an if/elseif statement assesses if Winner is equal to 1. If so, noWinner is set equal to 0 to end the main while loop, and a message box is displayed that tells the players that Player 1 has won. If player 1 is not equal to 1, the program checks for a tie by checking if the entire first row(in this case row 1 is the top row) is not equal to zero. If there is a tie, noWinner is set equal to zero to end the main while loop, and a messsage is displayed informing the players that there is a tie.

Virtually the same process is then done for Player 2. After Player 2 takes their turn, if a win is found, noWinner is set equal to 0 to end the main while loop and a message tells the players that Player 2 has won. If a tie is detected, noWinner is is set equal to 0 and a message box informs the players of the tie. If at this point noWinner is still equal to 1, then neither player has won and the loop starts again at Player 2’s turn.

 

Variables used:

  • matrix: an array that keeps track of the location of all pieces
  • noWinner: used to keep track of whether or not a player has won
  • column: used to keep track of which column has been selected
  • row: used to keep track of which row a piece will be placed in
  • Winner: keeps track of which player has won, if any
  • numinCol: keeps track of the number of pieces connected in a column
  • numinRow: keeps track of the number of pieces connected in a row

 

 

Over 7 / Under 7

The Over 7 / Under 7 program contains 2 files: Dice.mat which provides the image of two dice and Over_Under_7.m which is the script file that contains the source code of the program. In the beginning of the code, in order to directly use the image when the dice are rolled, the mat file must be loaded using the load command.

The program is mainly consisted of a while loop that checks the amount of money in the current pool. If the money in the pool is greater than 0, the game could run. Otherwise, the program should give the user a message and end the game. So before the first check, the computer should ask users to input an amount of money they want to put in the pool and assign the value the user inputs to the variable currentPool. After checking the current pool, the computer should remind the user the amount of money they have now and let the user input an amount of money they want to bet in this single round and store the value to the variable bet.

Then, two if statements and a while loop inside the first if statement are used to check if the value of bet is legal or not. In the circumstance that bet is not equal to 0, the program should apply a while loop to check if the money the user bet is greater than the money in the pool or if the bet is lower than 0. If one of the conditions of the while loop is true, the computer should give the user a message with the amount of money they have now and prompt the user again to input the amount of money they want to put in this round. The while loop should run continuously until a value that does not match the conditions of the loop is inputed by the user. Then in the second if statement, the value of bet should be checked again if the value is equal to 0. If it is 0, the computer should present a message to screen telling the user the amount the money they have now and use a break statement to jump out of the big while loop. And then end the program after print the goodbye message to screen.

However, if the value of bet is not equal to 0, the program should keep executing. The computer should then ask the user to input what they want to bet and store it to the variable highLow. H, L, and S represent High, Low, and Sevens respectively. The program is not case sensitive so user could input whether uppercase or lowercase. Moreover, the user could input without any single quotes because in the input statement, the ‘s’ has set the value type of highLow to String.

After assign the user’s input to the variable highLow, the computer should roll the dice and store the value of the two dice to the array roll. The computer should then show the image of two dice to screen and then add the value of two dice and store it to the variable total by retrieving the two values of the array and they print the message of the dice to screen. Then a new variable of winnings is created and assigned value of 0. Then the program should use an if statement with 2 elseifs to check if the result the user bet matches the result of two dice. Inside every if or ifelse, if the user’s guess does not match the result of the dice, the winnings should be set to the negative value of the money the user bet in this round by assigning -1 * bet to winnings. And if the user bet high or low and matches the result of the two dice, the winnings should be set to the value of bet. But if the user’s bet is sevens and matches the result of the two dice, the winnings should be set to 4 times the value of bet by assigning 4 * bet to winnings.

After assigning values to winnings, another if should be used to check if the user lost or won by checking the winnings. If the user lost, a message of “You lost!” would be printed to screen, otherwise, a message of “You won” and the money the user won should be printed to screen. After that, the currentPool should be updated using the winnings. At this point, the code is running to the end of the big while loop, so the program should check the condition (currentPool is not equal to 0) of this while loop again to see if the while loop is going to run again. If yes, the program should run in the while loop until the condition is not matched. If not, the code should jump out of the while loop and execute the next line to print a goodbye message to screen and to end the whole program.

 

Variables used:

  • currentPool: used to store the value of money in the pool
  • bet: used to store the money the user wants to put in a single round
  • highLow: used to store the user’s guess of high, low, or sevens
  • roll: an array that has a length of 2 that stores the two dice’s value
  • total: used to store the amount of value rolled by two dice
  • winnings: used to store the winnings (the amount of money the user won or lost) in a single round