C. Program Description for Developers

Connect Four:

In using MATLAB for this code, many different variables were used to define the different aspects of this game. A list of these variables can be seen below:

  • Board: 6×7 cell given in the Connect.mat file
    • Playing board
  • blackchip: 86x101x3 uint8 given in the Connect.mat file
    • Player #2 chip, displays on the figure, black chip
  • redchip: 86x101x3 uint8 given in the Connect.mat file
    • Player #1 chip, displays on the figure, red chip
  • drop:  integer between 1 and 7
    • The players are asked to input a value for drop
  • height: using the height of the column it makes sure the chip is placed in the lowest available spot
  • m: 6×7 matrix
    • This matrix is manipulated as the game is played to record where the player’s chips are
    • Starts with zeros
  • player_index: which players turn it is
    • Always equals either 1 or 2 depending on who’s turn it is
  • t: used as a counter in a while loop to make sure the drop input is valid

Once the variables were defined, many commands were used to manipulate the game and update the the code after each turn. The commands used are as follows:

  • clear: clears the workspace of all variables
  • clc: clears the command window
  • load: loads a mat file with the game board and chips given in the lab resources
  • fprintf: prints the command window
  • imshow: displays the playing board and the game progresses
  • creating matrix: matrix is created to represent game board
  • defining variables: variables defined as numeric values
  • while loop: loops through lines of code while a certain condition is met
  • if-elseif-else statements: these statements are used to make decisions about wins, losses, when the game board is full, if the input is valid, etc.
  • waitforbuttonpress: waits until button is pressed before the game continues
    • Used in the beginning to start the game after the rules are displayed
  • break: breaks the most inside loop of the code

 

Over/Under Seven:

The variables used in this code include:

  • Money: an integer greater than 0 determined by user
  • Wager: an integer greater than 0 and less than Money determined by user
  • OverUnder: string determined by user, only options include ‘Over’, ‘Under’, and ‘Exactly’
  • Roll: rolls two die, given in the Dice.mat file
  • Result: string consisting of either ‘Over’, ‘Under’, or ‘Exactly’ depending on the sum of the roll, if it matches OverUnder, player wins
  • t: random variable set to 1, used to trigger waitforbuttonpress command whose condition is if t=1 so player can press space bar if they wish to play again

 

The following commands were used in this code:

  • clc: clears the command window
  • clear: clears the workspace
  • load: loads in the Dice.mat file containing the randi and imshow commands and the roll variable
  • fprintf: prints out a statement
  • while loops: checks the two conditions that must be met to play the game: the player must start with more than 0 dollars and they can’t wager 0 or more than the amount of money they have
  • randi: rolls two 6 sided dice, given in Dice.mat file
  • imshow: displays the dice figure, given in Dice.mat file
  • sum: determines the sum of the two dice rolled
  • input: prompts questions player must answer regarding the amount of money they would like to start with, the amount of money they would like to wager, and whether the roll will be over, under, or exactly seven
  • if/elseif/else statements: used to determine winning conditions, if the player wagered more than 0 and less than the amount of money they have, etc.
  • waitforbuttonpress: allows for the user to play again by pressing the space bar

 

War:

Variables:

  • CardDeck: mat file with the playing card figures
  • B: The vector for the deck of blue cards
  • BlueDeck: The vector for the deck of blue cards
  • RedDeck: The vector for the deck of red cards (not used)
  • ShuffledDeck: a random permutation of 52 elements, same as a shuffled deck
  • COM: the computers deck, starts with the first 26 cards of ShuffledDeck
  • PLA: the players deck, starts with the last 26 cards of ShuffledDeck
  • t: the number of rounds it takes to end the game, counter
  • x: the index for the card in the player’s deck
  • y: the index for the card in the computer’s deck

 

Commands Used:

  • randperm(52): a random permutation in a vector, 1 through 52
  • clear: clears the workspace of all variables
  • clc: clears the command window
  • load: loads a mat file with the playing cards
  • fprintf: prints the command window
  • imshow: displays the player’s card and the computer’s card
  • defining variables: variables defined as numeric values
  • while loop: loops through lines of code while a certain condition is met
  • if-elseif-else statements: these statements are used to make decisions about wins, losses, when to end the game, and draws
  • waitforbuttonpress: waits until button is pressed before the game continues
    • Used to play next round of war
  • break: breaks the most inside loop of the code