C. Program Description for Developers

This is a brief overview of the script files used to create Connect 4 in MATLAB. This section is intended to be read by developers familiar with MATLAB logic and its basic function library. It includes a list of every variable used along with a brief description of its use, a list of every function with an explanation of its use, and a general description of how the code decides how to treat a player action.
To start, variables played an important role in the functionality of the script. As is typical, a majority of the variables are defined within the first 25 lines. “Jscene” is the variable associated with loading the simple game engine, as well as loading in the sprite information. The variables “empty”,”red”, and “black” are given the values of their sprite locations within the sprite file loaded in Jscene. These locations are the values 1,2, and 3 respectively. Next, one of the most important variables is created; the globalized display board, shortened in the code to “disboard”. This variable is the matrix that all of the math was based off of. It stores the information of where each coin was placed. The disboard was set to a 6 by 7 matrix of 1’s, or empty sprites, before any coins are placed. As mentioned, the disboard was made a global variable, so that the functions that detected if a player had won the game could access the information stored in the disboard. The disboard was also the only variable defined in each win function.
The next variable that is defined is the win condition, simply dubbed “win” and given a value of 0. The win variable was used in while statement to loop the mechanics of the game, until one of the win functions detected a victory. Finally, the player color was established, written as the “playc” variable. It was initially set to black. The variable playc told the code which sprite to place. In the disboard. The next variables that were defined together were the player input variables “r” and “c”. These took mouse input from the Jscene variable and stored the row, or “r” and column, or “c”, that the player clicked on. These variables were used in conjunction with the disboard to see if the player had placed a winning coin, or if the player had broken a rule. The next variable that was defined was the “elepsedTime” variable. This was used to clear the command window of any excess, irrelevant text. It was assigned whatever the timer value was at the time it was given a value. The “summdis” variable was assigned the value of the sum of the entire disboard matrix. It was used to dectect if the board is full and the game was a tie. The final variable defined in the script was the string variable win color, or “winc”. This variable was used to convert the winning player’s number value into a string that could be displayed with an informative fprintf message.
Functions were equally important in the creation of the code. The functions clear and close were used to avoid any strange behavior from previous game’s values. The function clc was used to clean the command window, which was used in the game to display important information, such as the rules and player turn. The function fprintf was used to give the player feedback on their moves, and display important information such as player turn and if the game had been won. The drawScene function was a function that was added by the simple game engine. It displays the player’s behavior onto the board, if appropriate. The function getMouseInput also comes from the simple game engine, and retrieves information about what the mouse clicks on. The function tic started a timer, and the function toc stopped the timer to store it in the variable elapsedTime. The function sum was used to sum the disboard for the full board check.
16 functions were created to detect if the game had been won by either player. These functions were generally refereed to as “win functions”. These 16 functions checked the 16 different possible ways the player could place a coin to win. The functions rely on the relative placement of the player’s last played coin. Each row and column combination were tried separately. The Checkup function checks if the color matches up to 3 spaces above the placed coin. Checkmid3 function checks if the color matches up to 1 space below the coin and 2 spaces above the placed coin. Checkmid4 function checks if the color matches up to 2 spaces below the coin and 1 space above the placed coin. The Checkdown function checks if the color matches up to 3 spaces below the placed coin. The Checkleft function checks if the color matches up to 3 spaces to the left of the placed coin. The Checkmid1 function checks if the color matches up to 1 space to the left of the placed coin, and 2 spaces to the right of the placed coin. The Checkmid2 function checks if the color matches up to 2 spaces to the left of the placed coin, and 1 space to the right of the placed coin. The Checkright function checks if the color matches up to 3 spaces to the right of the placed coin.
The Checkrightup function checks if the color matches up to 3 spaces up and to the right of the placed coin. The Checkleftup function checks if the color matches up to 3 spaces up and to the left of the placed coin. The Checkrightdown function checks if the color matches up to 3 spaces up and to the right of the placed coin. The Checkleftdown function checks if the color matches up to 3 spaces down and to the left of the placed coin. The Checkmid5 function checks if the color matches up to 1 space down and to the left of the coin, and 2 spaces up and to the right of the placed coin. The Checkmid6 function checks if the color matches up to 1 space up and to the right of the coin, and 2 spaces down and to the left of the placed coin. The Checkmid7 function checks if the color matches up to 1 space up and to the left of the coin, and 2 spaces down and to the right of the placed coin. The Checkmid8 function checks if the color matches up to 1 space down and to the right of the coin, and 2 spaces up and to the left of the placed coin.
The game started by running the script file. The scene was then shown, along with a set of instructions readable through the command window. The variable win was set to 0, and a while loop began. Only when win was 1 did the while loop end. The black player went first. The script started a timer. The black player selected a spot to place using the mouse. Upon the click, the script saved the row and column the player clicked on, and the script checked the elapsed time to clear the command window after the click, if the time elapsed was larger than 0.1 seconds. It then checked three things; if the player obeyed the laws of gravity by placing the coin ontop of another coin or on the bottom row of the scene, if the player tried to place a coin ontop of an already occupied space, and if the player placed a winning coin. This was done using a series of nested if statements. If the player did not play with gravity in mind, their turn was skipped and a message was displayed in the command window telling them what they did wrong. If the player places a tile in a spot where there was already a coin, the script then checked if the board was full. If the board was full, the game tied message was printed into the command window and win was set to 1. If the board was not full, the player’s turn was skipped and a message was printed into the command window explaining to the player what they did wrong. If a coin was placed, the script checked if the placed coin was placed to win the game. This was done by checking every possible win condition at the space that the coin inhabited with the appropriate win functions. If the coin was NOT placed ontop of another existing coin, the coin was placed with the effect of gravity in mind, and there was NOT a winning placement of the coin, the player was allowed to place the coin, and the player color was alternated for the next players turn. If the player placed a winning coin, win would be set to 1, and a message displaying the victor’s color was displayed.