DEV Community 👩‍💻👨‍💻

DEV Community 👩‍💻👨‍💻 is a community of 964,423 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for Noob's Tic-Tac-Toe on Python
jlop17
jlop17

Posted on

Noob's Tic-Tac-Toe on Python

Hi, my name is Jorge, and here is my completely unguided attempt at tic-tac-toe on Python. For background, I am 3 months into my programming journey and have completed all the main Python courses on Codecademy. This script is the result of my first project from the Computer Science path in Codecademy and is actually the first program I saw through to completion. I'd like to add that I have no doubt that there are "better" ways to create this game. However, I opted not to get any outside help to improve on my own problem solving abilities. That said, constructive feedback would be welcome.

Here is a link to the code on GitHub. All you need is Python to play it and a friend, if you have one of those.

The Game

Game screen with the title, score, board, and current player, awaiting the next move

At the start of the game, the first player is asked to choose between "X" or "O". The players then take turns choosing spaces to mark. The squares on the board are all named with a combination of A, B, or C and 1, 2, or 3. I made it possible for a space to be selected as any combination of letter and number. For example, a player can enter B2 or 2B, in either upper or lower case. The space entered is marked with their letter, and the next player is asked to make their move. If there is only one empty space available, the game automatically fills it in with the next player's mark. The first player to own three sequential spaces either horizontally, vertically, or diagonally wins. Both player's total wins and draws are constantly displayed. At any point, the game can be exited by typing "exit" or "quit".

The Code

Code for display, board, and Player class

Basically, the game uses a dictionary to map the space names to the player marks, or empty spaces, and displays the dictionary as a board using an f-string, accessing each space name from the dict in its corresponding space.

The players are actually a Player class instantiated when selecting "X" or "O" at the start of the game. Through attributes, the class keeps track of each player's assigned letter ("X" or "O"), the number of games they won, and the dictionary spaces they occupy in the current game. The class only has one large "turn" method made up of helper functions that are used to play (or autoplay when there is just one space left), determine if a move is a win or draw, and prompt for a rematch. The game itself is an just infinite loop of alternating Player 1 and Player 2 turns.

while True:
  player1.turn()
  player2.turn()
Enter fullscreen mode Exit fullscreen mode

The Future?

I considered adding an AI, but machine learning is not something I currently possess in my toolkit. As it is, the project has gone on a little too long, and I really should get back to my studies. Perhaps, in the future I'll add a Player 2 bot to make it a true one player experience. Thanks for reading!

Top comments (0)

🌚 Browsing with dark mode makes you a better developer by a factor of exactly 40.

It's a scientific fact.