C. Program Description for Developers

Blackjack:

  • Program sets up the deck using a randomized vector from 0 to 52
  • Then both the dealer hand and the player hand receive two cards
  • The decks are then displayed to the user, but the dealers second card is hidden till later
  • The score is checked and displayed to the user and it is checked if the user has black jack
  • It is then checked whether the user wants to hit or stand, hitting results in looping until either busting or the user decides to stand
  • Then the dealers turn begins
  • Checks are ran for making sure the player’s score is less than 21 and if the dealer as blackjack
  • Next checks are ran for if the player and dealer scores are equal to each other, if so the dealer wins
  • The check for the dealer runs until the dealer has 17 or more the program will keep hitting
  • The last thing that happens is the program checks if the player wants to play another.

Connect 4

The program begins by clearing out any outstanding variables and text from the command window. Then it creates a variable cont and sets it equal to 1 this being used for continuous play. Then a while loop is enclosing the rest of the code and breaks if cont is set to 0. The program then clears both variables and the command window again and loads Connect a pre created file which creates the board visual and the commands that go with it. cont is set to 1 again inside the loop as it was just cleared and needs to be recreated. Next a turn variable is created which is used to keep track of which player’s turn it is, it is initially 1. Then a 7×6 matrix is created called matrix which represents the connect 4 board with all values being 0 representing empty space. The board is then shown with the command imshow. Then a new variable called game is created, it is initially 1. This variable is used like the cont variable, as the check for a while loop but only for the current game to allow the switching between the two players. The first line of code for the current game is a while loop checking which player’s turn it is. In this loop a row vector is created and is set to 0. This variable is for a loop which prompts the user for a column entry of values 1-7. The program then checks if there is a space available in that column with our function checkSpace which given the current matrix variable and entered column variable will return which row is available if any and will return 0 if there is not a row available in that column. Once the player enters an available row the loop ends and the players piece is added to the matrix at the lowest available row in their chosen column. The program then updates the Board visual with the corresponding player chip in the correct spot. Next the program checks if the player has won with our function digWin which checks every possible win condition given the matrix variable. This function checks the matrix using a vector called directions and two variables dx and dy which are used to check each direction of possible win. It creates a variable called win which is initially 0 and is changed to 1 if a win condition is found. The function checks each directions furthest possible connected point and ensures that it is not out of bounds. Then it gets what value the spot that it is currently checking and puts it in a variable called check possible values being 0,1 and 2, 1 and 2 being player chips and 0 being empty. If the spot is 0 then the check ends. If it is not 0 then the program checks the current direction for a win condition of 4 of the same pieces either, 1 or 2, being in a row. If there is 4 in a row win is set to 1 and the program returns that the player has won. If digWin returns a win then the program will print which player has won and then sets game to 0 and breaks causing the current game to end. If no win condition is found then the program goes to player two’s turn and repeats the turn process. If no win condition is found after player two’s turn then the program does a new check to see if the board is filled with pieces. Using the variable filled which counts the number of rows currently filled by using checkSpace and if checkSpace returns 0 meaning a filled row it adds 1 to filled. If filled equals 7 then the game ends in a tie. Once the game ends then the players are asked if they wish to continue and can do so by entering any combination of letters or anything except for the singular letter ‘n’. The user is told that entering ‘n’ will end the game which is done by setting cont to 0.