C. Program Description for Developers

Blackjack:

During the “initial run” of the program, the user is prompted to enter the amount of money they intend to bring to the table using an input command. There are checks (using while + for loops) to ensure the user’s input is both a number and positive.

The user is then prompted to enter the amount of money they want to bet for this particular round. Similarly, this value is obtained using an input statement and there are checks to prevent any foul play.

Cards are “dealt” utilizing a random number generator and placing the generated values into a specific variable. To account for the fact that face cards are also considered to be worth 10, the random random number generator spans 2-14. If the RNG settles on an integer 2-9, then that value can be translated directly to a card. Conversely, if the RNG settled on 10, 12, 13, or 14 then the card would become a 10, Jack, Queen, or King. If the RNG settled on 11, then the card would become an Ace.

The user’s first two cards and the dealer’s face up card are displayed to the command window using an fprintf statement. The user’s sum is calculated by adding the value of their first two cards. If, on the odd chance, the user is dealt 2 aces, there are considerations to turn one of the aces into a 1. If the dealer has a card valued at 10 or an Ace turned up, then the dealer “checks” for blackjack. If the sum of the dealer’s first two cards is 21, then the round is over and the player loses unless they also were dealt a blackjack. The user’s total and the dealer’s check is all displayed to the command window using the fprintf command.

If the dealer or the player does not have a blackjack, the player may choose to hit. Using the same method as before, the user is “dealt” a new card and the new card is added to their total. So long as the player’s total is less than 21, they are given the ability to hit. This bit is achieved by putting the hit input command within a while loop bounded by the user’s total and their decision to hit. Each time the user hits, the new card and the user’s new total is displayed in the command window using an fprintf statement.

Once the user is content with their total, the dealer’s “undisclosed” card is revealed. The dealer must “hit” if their total is less than or equal to 16. This is achieved using a while loop bound only by the dealer’s total. Once the dealer reaches 17-21 or busts, the results are displayed to the command window.

If the player wins, their initial bet is multiplied by two, and their winnings are added to their bankroll. If the player loses, their initial bet is subtracted from their bankroll. If the dealer and the player tie/push, then the player’s initial bet is returned to their bankroll.

Through an input command, the user is asked if they want to continue to play. Depending on the user’s answer, they are led to another round with their updated bankroll, or they are booted. This “continuous” playing effect is created by a while loop whose parameters are initially met when the program is started, then altered when the user decides they don’t want to play anymore.

There are a series of “gifs” that play at the end of each round depending on the outcome. The code used to create this effect was primarily found online and altered to fit our program.

Mastermind: 

To start off the game, there is a random permutation done to create the answer array. This was assigned to a1, the original answer, as this will be changing down the line, and need to have the original saved. This permutation is a 4 number array, and does not contain duplicates. The initial part of the game also presents some of the rules and valid colors, creates the blank game array, and starts the multiple play while loop.

Next, the main loop of the game begins, and it will repeat 10 times, meaning you get 10 guesses when you play a game. First thing to happen is the resetting of the answer array to the original, useless on the first loop, but is necessary, as the feedback changes the answer. Next, all the numbers are assigned to their colors, all in an attempt to set up the str2num command, in order to produce an array if these values. Then, due to errors converting numbers to strings and visa versa, loops were not used to create a numeric  array that represents a users guess. For each of the four inputs for the guess, it collects the input as a string, then uses str2num, which converts it to its number, and each of the four is saved as a different variable, as then the four variables are put into an array. Next, the game board variables for each of the color values, and each of the peg values, are reset, in order to keep each round independent. Also, here there is a check for a win, if the answer array is the same as the guess array, then it prints you win, and breaks out of the main loop.

Now, the feedback section of the main loop begins, as this part contains each of the peg values, and the game board output. First, the game board array values are generated. This is done by using 4 separate if loops for all the colors, due to converting errors again, in order to make each number back to a color. Then, the peg checks and feedback occurs. this is done in order of priority in peg distribution, so black peg value is calculated first, then white pegs. So, for the black pegs, a for loop is used for each 4 of the values in the array, and then an if loop is used. The if loop states that if the value of the current position of guess array is the same value in the current position of the answer array, then it adds one to the amount of black pegs for the round, and sets that value of the answer array to zero, so that that same color wont trigger another white peg later (and is why answer is reset later). Then, in a separate for loop, there is another if loop, this one stating that if the current number of the guess array is equal to either 4 of the answer array values, a white peg is added to the total, and whichever answer slot it used, if it does use one, that answer slot is set to zero as well. Both of these loops are repeated four times, and any answer slots still unused by either loops are simply left alone, as they wont count for anything.

Lastly, the game board output must be shown. This is done by taking the variables for each loop for all four variables, and the two peg values. For the peg value statement, if loops were used to get different statements in the game board array for different peg values. Then fprintf’s and disp commands were used to print and label all the different outputs of the game board. The game continues like this for all 10 loops, and after the player loses or wins, it asks to play again, if yes, the game repeats, if no, it prints a goodbye statement and ends.