F. Discussion

Connect 4 is a rather simple game upon first look, however coding the logic behind the game proved to be rather tricky. Storing the game board in a variable was the first of many obstacles. At first we tried to make a 6×7 array that was full of either 1 for token there or 0 for no token there. We realized that while it would store what columns would be full of tokens, it wouldn’t store what color the tokens were, which is a pretty important part of the game. Upon further thought we decided to make a 6×7 array full of either 0 for no token, 1 for red, and 2 for black. This array would store the color and position of the tokens, allowing us to continue with the game. With the array constructed, we needed a way to check for 4 tokens in a row to win the game. It is too inefficient to hard-code every single possible combination of 4 tokens in a row inside a 6×7 array, so we had to come up with an algorithm to figure out when some player had won. The method that we came up with was to take the last move by the player and check 3 tiles around it in 8 directions being left, right, up, down, diagonally up and left, diagonally up and right, diagonally down and left, diagonally down and right. The first implementation of the method worked in many cases, but didn’t in many others. Originally we had the method testing 1 tile away all around it, then 2, then 3.

Testing showed that diagonally up and to the right didn’t work, but everything else did, some indexes of the array were out of bounds if you placed the token in a certain spot, and the function didn’t stop when it reached a token of the opposite color leading to connect 4s that shouldn’t have been possible. To fix these problems, we refactored the code to loop in one direction at a time. It checked the direction right up to 3 tiles then moved onto left and so on. This way the function could break out of the loop when it hit a token of a different color or went out of bounds of the array. We also fixed bugs by changing silly mistakes to what they should have been. After testing with players from other groups, we changed the player input from entering the number of the column you want to put a token in to clicking where you want to drop a token. Clicking was more intuitive and was mostly the feedback we got when we had other groups test the game. After the changes made to our code, the game works like a charm.