DEV Community

ryanfriedrich1
ryanfriedrich1

Posted on

Connect 4 Python terminal game

This is a program that I created as a part of my portfolio task for Code Academy.
Image description
Here is the class that I made to represent the "board" for connect 4 using a list to represent the columns of the board and a function to print out the board.

Image description
Here is the function I used to search the board for a connect 4. I ended up using a brute-force method searching each row, column and possible diagonal for 4 in a row.

Image description
Here is the function I used to place the pieces on the "board". I would get the input from the terminal then replace the first available space in the column they chose to place their piece in.

Image description
Here is the main code that was used to run the game and get input from the user.

Here is a link to my GitHub code:
https://github.com/ryanfriedrich1/connect4

Conclusion

This was a fun small project that I made that is a simple Python terminal game. Some of the main struggles that I faced were figuring out how I would create the "board" in the code and ended up using a list of the columns. Another struggle that I faced was figuring out how I would find a connect 4 but ended up using a brute-force method since the data isn't too complicated. I could have made it slightly faster in hindsight by seeing if it was searching through empty spaces and ending the search then.

Top comments (0)