Software Documentation

INTRODUCTION

Blackjack

Blackjack is thrilling card game played in casinos throughout the world. Blackjack has been hotly debated as being first seen in French casinos in the 1700’s.

The game consists of the players and the dealer, where in some casinos as many as seven players can play at the same table. All players start off with two face up cards and the dealer is dealt a face and face down card. Proceeding the cards being dealt, players can receive more cards or keep the current amount they have by simply saying “hit” or “stick”.

The goal is to get the total of your hand to exactly 21 (no more) and players are encouraged to think strategically about their hand before they end their turn. At the end, hands are compared with the dealers’ to see which is higher. The higher hand wins the round.

Connect 4

Connect 4 consists of red and black playing coins, one color belonging to each player, and a 7×6 playing board. The yellow playing board is flat with 42 holes connecting two sides, allowing the players to see the coins placed inside through the slit at the top.

The player who chooses to play with the black coins gets to hold the first turn. They place a coin through the slit at the top of the board, and it falls to the bottom of the column chosen. Player 2 then takes their turn doing the same. Coins cannot be stacked past the top of the slit.

The goal of the game is to be the first person to connect four coins in a consecutive row; this can be done horizontally, vertically, or diagonally. The first player to construct a line of four coins wins!

 

USER MANUAL

Blackjack_UserManual

Connect 4:

The game will begin by asking the user if they are ready to begin. The player enters ‘Y’ to begin the game. The program will commence by creating a 6×7 matrix of zeros. Two players will take turns depositing their coin (‘1’ or ‘2’) into the matrix from the top. This is done by entering the column number in which the player would like their coin to go when prompted. For example, if player 1 chooses column 1 for their first turn, that user’s coin will be placed in the lowest available row of column 1. Player 2 will then be prompted to choose a column, and their coin (‘2’) will be placed at the lowest available slot in that column. Should the column be inconsistent with the constraints of the matrix, or if the column is already full, the player will be prompted to choose another. As soon as there is four consecutive ‘1’s or ‘2’s in a row, column, or diagonal, that player will win the game.

 

Connect Four Technical Manual:

  • The program begins with a welcome statement and a segment explaining the rules and objectives of Connect Four. This is done using the fprintf() command.
  • The program then prompts the user for text input which is then stored in ‘gameStart’
  • A while loop iterates until the user input is valid, that being, a case sensitive ‘Y’
  • The variable ‘ROW’ is created and assigned to the value 6; this holds the number of rows in the game board, also a constant
  • The variable ‘COLUMN’ is created and assigned to the value 7; this holds the number of columns in the game board, also a constant
  • The variable ‘board’ is created; an empty matrix with dimensions ‘ROW’ and’ COLUMN’ are created and filled with zeros. This is done by using the zeros() function.
  • The variable ‘player’ is created and assigned to the value 1; this represents the current player’s marker. It is assigned to 1 because player one always goes first.
  • The variable ‘gameOver’ is created and assigned to false; this represents the status of the game. This was initialized here because a condition needed to exist to execute the next part of the program
  • A while loop iterates indefinitely until ‘gameOver’ equals something other than false (true).
    • The ‘c4_drawGame’ function is called and checks to see if all the possible slots have been filled. This is done by adding all the values in ‘board’ and seeing if the ‘sum’ is equal to 63 which represents a completely filled board. The output is assigned to ‘gameDraw’
      • If ‘gameDraw’ is true, the game ends and a message saying the game ended in a draw is displayed
      • Else, the game continues
    • The current player is asked to enter a column number, this is then stored in ‘playerMove’ which used the input() function
    • The ‘c4_inputChecker’ function is then called and checks to see if the user inputted number is a whole integer in the range 1-7. 
      • While ‘columnNum’ is not within this range, the program prompts the current player to re-enter a valid column number
    • The ‘c4_columnValid’ function is then called and checks to see if the current player’s ‘playerMove’ is a column that is already filled
      • While the selected column is already filled, a message is displayed asking the current player to re-enter their column number
      • If the current player selected a valid column, the ‘c4_rowFinder’ function is called and finds the next available row in the selected column starting from the bottom up. The result is then assigned to ‘row’
      • The ‘c4_updateBoard’ function is then called and places the current player’s marker (which is the player’s number, either 1 or 2), into the validated row and column. The result is then assigned to ‘board’
    • The ‘c4_winnerWinner’ function is then called and checks to see if the previous ‘playerMove’ created a connect four. This was done by splitting the board into sections and using nested for loops to check for all the possible connect four combinations (vertical, horizontal, or diagonal). The result is then assigned to ‘gameOver’
      • If ‘gameOver’ is true, a message is displayed congratulating the current player for winning and ending the game
      • Else, the game continues because ‘gameOver’ is still false 
    • If-else conditional branching is then used to switch players, either 1 to 2 or 2 to 1. The result is then assigned to ‘player’

 

References:

https://www.mathworks.com/help/matlab/ref/fprintf.html

https://www.mathworks.com/help/matlab/ref/input.html

https://www.mathworks.com/help/matlab/ref/zeros.html

https://www.tutorialspoint.com/matlab/matlab_nested_loops.htm

 

Blackjack Technical Manual:

Through the use of many MATLAB functions and the creation of variables, the team was able to successfully code Blackjack.

Below are the functions and variables along with a brief description of their purpose.

Functions ||

  • fprintf() – Used in the script to display results and card draws to the player.
  • input() – Request player input for wager, number of players, and for response in regards to hitting, sticking, or doubling down.
  • int64() – Changes value entered to a 64-bit integer. Used to check if the inputted number of players is an integer.
  • zeros() – Creates a matrix of rows (n) by  columns (m) with only zeros i.e. (zeros(n, m)). Used to allocate space for matrices to be updated further in the script.
  • strings() – Creates a matrix of  blank strings of rows (n) by columns (m) i.e. (strings(n, m)). Used to allocate space for string matrices to be updated further in the script.
  • randi() – Outputs a randomly generated number given a specified range. Used to draw cards for both the player and the dealer.
  • clear – Clears all variables from the workspace.
  • clc – Clears the command window.
  • pause – Pause the script until any key is pressed. Used to clean the program up and more user-friendly.
  • additional remarks – In addition to the above functions, features such as if-else statements, try-catch statements, while loops, for loops, return, and break were used as the structure of the program in order for it to function.

Variables ||

  • blckjack – Stores values greater than zero if a player or multiple players have a blackjack hand.
  • card1 – Matrix that stores all card draws for players throughout the game.
  • cardNum – Stores current card number to be drawn for players and dealer.
  • cardsDealt – Vector storing two values for the dealers initial two-card draw.
  • cardTotValDealer – Stores the sum of card values from the vector cardValueDealer.
  • cardValue1 – Holds card values for player drawn cards from card1.
  • cardValueDealer – Holds card values for dealer drawn cards from cardsDealt.
  • cardValueTotal – Stores the sum of card values from the matrix cardValue1.
  • continueX – Vector the length of the number of players used to hold values of 1 or 0, instructing whether the player will end their turn.
  • dealerBlckJack – Stores values greater than zero if the dealer has gotten a blackjack.
  • doubleDown – Vector the length of the number of players used to hold values 1 or 0, instructing whether the player has doubled down or not.
  • isGreater17 – Holds either true or false values depending on whether cardTotValDealer is less than 17 or greater than or equal to 17.
  • isInputOkay – Holds either true or false depending on whether there is an error created in inputting an invalid data type for playerWager.
  • numPlayers – Holds inputted number of players from the user.
  • player – Stores amount of players to iterate through a for loop.
  • playerResponse – Stores player response in string format, which is then used to determine whether the player wants to change the value of their Ace to either 1 or 11.
  • playerResult – Stores number values from function CheckCardVal, arbitrary and only used as placeholders for the outcome scenario a player wins or loses in.
  • playerWager – Stores input values above 0 from the player, specifying the amount in dollars they want to bet.
  • stringCardType – Holds a vector of strings for cards drawn by the dealer. For example, if a King is drawn stringCardType will hold the string “King”.

Functions ||

  • CheckCardVal() – Function that outputs results of player (how they won or lost) with fprintf().
  • DealerCard() – Function assigns card values to each one of the dealer’s cards and additionally assigns strings for the cards respective card type. i.e. Ace = “Ace”, 10 = “10”
  • DealerCheck() – Checks if the dealers hand total is greater than or equal to 17 or less than 17.
  • PlayerCard() – Draws a card for the player and assigns a card value for each card for every player.
  • HitStickDouble() – Requsts input for a player to hit, stick, or double down if their hand total is below 21.

 

References:

strings. (n.d.). Retrieved from https://www.mathworks.com/help/matlab/ref/strings.html.

try, catch. (n.d.). Retrieved from https://www.mathworks.com/help/matlab/ref/try.html.

function. (n.d.). Retrieved from https://www.mathworks.com/help/matlab/ref/function.html.