Python Terminal Game: Tic Tac Toe

Hello everyone,

I implement Tic Tac Toe in python and it’s my first portfolio project as I began learning on codecademy. I used classes and objects to make it simple to execute, provided comments for ease of understanding and it’s a version 1 implementation of the game. Please feel free to give your thoughts.

Link: GitHub - Tic Tac Toe

Future Improvements:

  1. Player vs Computer > Easy = Random position selection → Version 2
  2. GUI with PyGame → Version 3
  3. Player vs Computer ->Hard = AI based position selection → Version 4

You don’t always need classes. In particular, if your method doesn’t reference self at all, it may not need to be there. Also, an empty return at the end of a def is unnecessary.

You should be able to derive self.current_player from the current game state. Not sure you want a dict for game_pos; why not a list?

Your checks can be simplified. Consider:

def check_three(self, a, b, c):
    return self.game_pos[a] == self.game_pos[b] == self.game_pos[c] and self.game_pos[a] != ' '

def check_row(self):
    return self.check_three(1,2,3) or self.check_three(4,5,6) or self.check_three(7,8,9)

Or, perhaps:

def check_row(self):
    for three in [(1,2,3), (4,5,6),(7,8,9)]:
        if self.check_three(*three):
            return True
    return False

Given that, you mightn’t need your separate checks?

Good start, keep at it.

2 Likes

I appreciate the feedback, I’ll implement them into my project.