Python TicTacToe Terminal Game

You can find the code that I will be talking about here. Feel free to clone the repo, play it and let me know what you think! Okay, on to talking about the code.

I tried to use all concepts that were learned up to this point that made sense. I didn’t use any dictionaries which I just didn’t think were necessary for what I was trying to do.

First thing I did was create a Player class that was used to instantiate both players. Next, I created a Board class that contains the majority of the logic for the game. I started by creating an empty 2d array that I would use to append the pieces to as the game went on. I then wrote a method for displaying the board. This was a little tricky to get right, but ended up doing what I wanted it to in the end. The next method was the move the player. I did this early on so I could test that everything was working as the code continued to get larger. There are checks to make sure that the player only enters coordinates between 1 and 3. Next, I wrote three methods to check the win condition of getting three in a row. These work by checking the index of the 2d array and seeing if all 3 indexes match a given player piece. After those methods, I wrote another method that checks if any of those methods have returned a win. The next method checks for open spaces in the array to help with checking the status of the game. Same things for the player_count_difference method: this is just a check to make sure that there is not more than 1 piece of the player on the board at a given time. Should be impossible, but just extra assurance in case somehow it could be broken. The game_status method just checks all the possible situations that the game state could be in and return that state. In the main method, I instantiate all the classes that are needed for the game and create a fun little art piece to let you know what game you are playing. I include instructions after that on how to play the game. Quick function to check if the players are ready to play the game. If they are not ready, the program exits. If they are ready, the game starts and the while loops displays the board and checks the game status after every move.

I would like to add functionality at some point to be able to play against and AI, but that’s for a later date.

Feel free to give me feedback on what you think about my project: the structure, the implementation, code clarity, or if there’s anything you would have done differently. One thing I would probably change is the way you place the pieces on the board. That seemed a little convoluted, but hey, it’s not the worst.


1 Like

At first I didn’t realize it was a two-player game so you might need to clarify that.

Before I read your comment I played the game and I also got the idea to play against the computer. :slightly_smiling_face:

Also, like you said, I think the way you make your moves is not the worst but might need to be changed. The coordinates were a little confusing because I wasn’t sure which number was the x-axis and which number was the y-axis.

Overall it’s very fun and I think you did a great job! I also love the ASCII art you used for the logo! :smiley:

1 Like