I saw this video on Youtube recently in my recommended column, and I was so completely blown away by the programmer's skill that I decided to get back into programming for fun. I've learned a bit of Python in college, but I was hungry for a toy project that I could use to refresh my memory. Thus, I decided to try and implement the classic game "Connect Four" in the console. It took me about five hours to write up this first implementation, and I had a good deal of fun working on it. For those who may be unfamiliar with Connect Four, it's a table game played between two people where the objective is to be the first person to get four of your pieces laid out in a row on the board. The gameboard is usually 7 spots wide and 6 spots tall, and the pieces are red and yellow. I chose this game to implement instead of others (like Tic-Tac-Toe or Hangman) because I loved playing Connect Four growing up, and I also had a really memorable experience proving in one of my math classes that Connect Four is a solved game.
The part I personally had the most fun (and trouble) working on was the logic for checking whether or not a player has won the game, as that involved checking for diagonal instances of connect four. I made things a bit harder for myself by allowing the player to specify a board of any arbitrary dimensions to play on, and thus I had to solve a more general problem of taking in a 2D array and finding all of its diagonals. I believe that the NumPy library has a method that will allow you to do this easily, but I wanted to work through it myself because I thought it was an interesting problem. I have heard it said before that programming follows the Pareto Principle pretty closely, in the sense that you will get 80% of your code written in 20% of the time you are working, but the last 20% or so will take 80% of the time. I am certain that if I timed myself as I worked through the separate stages of this project, I would find the hard last 20% of the work to be the diagonal finding algorithm that I had to write. But that was the most fun part!
The program itself is fairly typical. It follows the typical initialization-gameplay loop-endstate structure of game design. The initialization step consists of the program requesting the player to input the dimensions of the gameboard and the color of the player who will play first. This data is then used to build the gameboard as a 2D list of lists of characters representing empty spaces and to prompt the first player to give the coordinates of where they wish their piece to go on the board. From there, the program takes the coordinates, checks to see if they are valid (i.e. within the bounds of the board and not already occupied by a piece), and then edits the gameboard list to have either an 'R' or 'Y' character where there was previously an empty space ('O'). The current state of the gameboard is printed at the start of every turn. Every time the player makes a move, the game checks to see if connect four has been reached or if the gameboard is full of pieces, and if not, the game keeps going. If a player has won, a message of "Congratulations!" is printed, declaring the winning player, and the program completes. If the board is full, a message of "Stalemate" is printed, and the program completes.
I've listed this under "Current Projects" because I would like to keep working on this project. As it stands, there are three main things I would like to develop: 1. A proper graphical display of the game, 2. Altering the game to allow for the effects of gravity (because in real life, one is limited where they can place their pieces - they must either be on the bottom row of the board or on top of another piece), and 3. Allowing for the player to select an AI opponent. The first part requires some study of graphics libraries for Python, the second requires altering the underlying logic of the code (namely the part where the player is prompted to give piece coordinates -- I would need to have another check where the coordinates must point to a spot either at the bottom of the board or above an occupied spot), and the third requires refreshing my memory of the random number library in Python.
Anyways, here is the github link to my project repository if you want to check it out!
-g, 4/2/2022