F. Discussion

Connect 4

Connect 4 started out really slow, though it only took two days to code. It was simple enough to get the board set up and figure out which values in a vector of numbers to use, but that was the only thing that went smoothly the first night. After creating the game-long while-loop, time was spent unsuccessfully attempting to get the rotation of player’s turns to work. Nothing more was done on the first night. The next day, after continually struggling with the turns system and player input, we restarted on a new file from solely the code to create and display a 7×6 board. We then took the base of what worked from the first code and used it in the new code, excluding the dysfunctional parts. Eventually, the turn system was figured out and then the players were able to input which column they wished to place a chip in with messages prompting them to select a valid column, instead of an error message, if the input was invalid. The last major thing done was checking the scenarios for a win or a stalemate. This was done using multiple for-loops with if-statements for the different scenarios. The final thing done was to display the graphic for the board. The empty board was displayed at the beginning of the game and the graphics for the two different colored chips were inserted in the code when the player placed a chip.

Throughout this process, continuous testing was done. Every time a new line of code was inserted, the program was run to test that single line. To see the results of each addition, since the graphics were not added until the end, a vector matching the dimensions of the game board was displayed with ones and twos and zeros for the two different colored chips and empty spaces. This vector was the actual board, and the graphic displayed to the user in the final product is just a reflection of the vector using images while the actual vector is muted. The testing was necessary after almost every line of code created so that the program was not blindly written with numerous and difficult to address flaws. The longest stretch of code created without testing in between was when the check win scenarios for player two were created. The reason for this was it was essentially a copy of the player one code with a few variables changed to make it for player two.

 

Hangman

While hangman is a basic game in theory, we discovered many troubles along the way which came to be major road blocks. During the initial stages of the game we struggled with figuring out how to load the different strings into a vector. After working with different students and online sources, we figured out manually typing in every string into one vector was the best decision for this process. Additionally we struggled to figure out how to randomly select a string value, as the only randomization code we knew coming into the project was the “randi()” command. Using the mathworks website as well as online sources, we were able to code in a random integer for the length of the array. From there, the number selects a certain string, and the characters of that string make up the word that the player must guess. Our biggest problem fell in the inability to display the letters which have been guessed, along with the blank spots in the word that still need to be guessed. The solution to this problem was to run a for-loop for the length of the word, which compares the guess that the player inputs to every character within the string. For every point which the guess matches the string, that position within the display inputs the guess to the spot aligning with the actual word. Initially, the display is told show a “-“ for the given spot. Each time a letter is guessed correctly, it simply replaces a “-“ with the correct letter. Additionally, the “contains” operation is performed on every guess. This operation detects if the guess is within the word. If the guess is not within the word, then the operation will yield “0” or “false.” If the guess is within the word, it will yield “1” or “true”. Once the contains statement takes place, an if-else statement determines whether a life should be subtracted or not for an incorrect guess. The game ends when all the values within the string are correctly guessed or when the player runs out of lives.