Player Base Class
The abstract base class for all player types in the Tic-Tac-Toe game.Constructor
__init__(letter)
Initializes a player with their assigned letter.
The player’s letter, either ‘X’ or ‘O’
None
letter: String (‘X’ or ‘O’) representing the player’s mark
Methods
get_move(game)
Abstract method to get the player’s next move. Must be implemented by subclasses.
The current game instance
int - The board position (0-8) for the move
HumanPlayer Class
Implements a human player that accepts input from the console. Inherits from:Player
Constructor
__init__(letter)
The player’s letter, either ‘X’ or ‘O’
Methods
get_move(game)
Prompts the user to input a move from the console and validates it.
The current game instance
int - The validated board position (0-8) chosen by the user
This method loops until the user provides a valid move (0-8) that corresponds to an available square.
- Input must be a valid integer
- Input must be in range 0-8
- The chosen square must be available (not already occupied)
RandomComputerPlayer Class
Implements a computer player that makes random valid moves. Inherits from:Player
Constructor
__init__(letter)
The player’s letter, either ‘X’ or ‘O’
Methods
get_move(game)
Randomly selects an available move from the board.
The current game instance
int - A randomly selected board position from available moves
GeniusComputerPlayer Class
Implements an unbeatable AI opponent using the minimax algorithm. Inherits from:Player
Constructor
__init__(letter)
The player’s letter, either ‘X’ or ‘O’
Methods
get_move(game)
Determines the optimal move using the minimax algorithm.
The current game instance
int - The optimal board position calculated by minimax
For the first move (empty board), the algorithm chooses randomly to add variety. For all subsequent moves, it uses minimax to guarantee optimal play.
minimax(state, player)
Recursively evaluates all possible game states to find the optimal move using the minimax algorithm.
The current game state to evaluate
The current player (‘X’ or ‘O’) in the recursive evaluation
dict - Dictionary containing:
position: The optimal move position (0-8) orNonefor terminal statesscore: The evaluated score for this state
Minimax Algorithm Explained
Minimax Algorithm Explained
The minimax algorithm works by:
- Base Cases:
- If there’s a winner, return a score weighted by remaining squares
- If the board is full (tie), return a score of 0
- Recursive Evaluation:
- For each available move, simulate making that move
- Recursively evaluate the resulting game state
- Undo the move to restore the original state
- Maximizing vs Minimizing:
- The AI (max_player) tries to maximize the score
- The opponent tries to minimize the score
- This creates an adversarial search tree
- Scoring:
- Positive scores favor the AI
- Negative scores favor the opponent
- Scores are weighted by the number of empty squares (faster wins are better)
Usage Example
The GeniusComputerPlayer is unbeatable when playing optimally. The best a human player can achieve is a tie.