The game was created in MATLAB using a given simple game engine code and sprites of the game pieces. At the beginning of the game, one person is assigned to be player one and the other player two. Players taking turns placing their different colored chips onto the board. Once one player gets four of their chips in a row (horizontally, vertically, or diagonally) the player wins and the game is over. The game being in MATLAB is a simple code and should be easy to edit. The game is an array of board sprites with sprites of players’ pieces printed onto the board and subsequently checked with if-else statements to determine if there was a winner.
To develop the game, using a sprite sheet, the team assigned each sprite (empty, red, and black) with variables that can be referenced throughout the code. Next, the team created an empty game board with dimensions 6×7 empty sprites. A loop was created that checks a column for the lowest empty space, and if no empty space is found then a chip isn’t dropped into the column. If there is an empty space, the chip is placed, and the player tun is then switched. At the end of the loop, the code checks to see if a player has won yet using the function “testwin”. If “testwin” is false, then “isplaying” continues and the loop between players keeps running. “Testwin” is a function that checks all possible combinations for four in a row using if-else statements based on the row and column of the last chip that is placed. If there are four chips in a row, the output is true and the game stops running. Then, an if-else statement is used to determine if the black player or red player won, and a win message is displayed accordingly. Then, an if statement is used to check all the columns in the top row of the game board, and if all the columns are not empty sprites, then the output of “testwin” is true and a tie message is displayed.
Some problems that were encountered while creating and testing the game were issues with the testwin function exceeding the bounds of the array. This was fixed by setting bounds at the beginning of the if-else statements that would only run when there was enough space in the array to test in that direction, using the short-circuiting “and” operator. The statement would evaluate to false if the conditions were not met and that test would not run. Then, it would move on to the next test, once again checking if the bound requirements were met. Another issue encountered was how to add a test for a tie. This was accomplished by checking the top row of each column of the board, knowing that if the top row is filled, all the other rows must also be filled. If the top rows were filled and none of the testwin function if-else statements evaluated to true, then there would be a tie. A third issue was trying to create a message box that pops up when there is a win or a tie using the msgbox function. The team wrote the conditions for when the message should pop up using if statements and the “and” operator, but it resulted in an error message. It was determined that the reason for this problem was that the variables used in the msgbox function were defined earlier in the script itself but not in the actual function. Once the variables were redefined within the testwin function, the message box popped up when the conditions were met. After fixing all the encountered problems, the game ran smoothly.