src.ai
Submodules
src.ai.ai module
Base AI Player
- class src.ai.ai.AIPlayer
Bases:
objectBase Class for all AI Computer Opponents
src.ai.minimax module
Minimax Opponent
- class src.ai.minimax.MinimaxPlayer(depth=None)
Bases:
AIPlayerMinimax Computer Opponent
- Parameters:
- depth: int, optional
Maximum search depth for the minimax algorithm. If no depth is specified, infinity (
math.inf) will be used in the minimax algorithm. Default isNone.
Methods
minimax(grid, depth, maximizing_player)Minimax algorithm
play(play_fn, game)Play the AI opponent
score(winner)Calculate the game score for a winner node
- minimax(grid, depth: int, maximizing_player: bool)
Minimax algorithm
- Parameters:
- grid: Grid
Current game state to calculate the minimax value from.
- depth: int
Current search depth of the minimax algorithm.
- maximizing_player: bool
Whether the current player is maximizing or minimizing his score.
- Returns:
- int
Score of the given grid layout.
See also
src.ai.ABPrunedMinimaxPlayer.minimaxminimax with alpha-beta pruning
- play(play_fn, game)
Play the AI opponent
- Parameters:
- play_fn: `callable` [tuple[int, int]]
Play function receiving a board position (x, y) on which to place the AI’s symbol.
- game: Game
Game object containing the game logic and the current game state.
- Returns:
- tuple[int, int]
Position on the board chosen by the algorithm.
- score(winner)
Calculate the game score for a winner node
Return the score for a given winner. This will be a positive number if the winner is the AI, and a negative number if the winner is the human.
- Parameters:
- winner: str or int
Symbol of the winner node. Can also be a
0if the state is a tie.
- Returns:
- int
Score of the final game state
src.ai.minimax_abpruning module
Minimax Opponent with Alpha-Beta-Pruning
- class src.ai.minimax_abpruning.ABPrunedMinimaxPlayer(depth=None)
Bases:
MinimaxPlayerAlpha-Beta-Pruned Minimax Computer Opponent
- Parameters:
- depth: int, optional
Maximum search depth for the minimax algorithm. If no depth is specified, infinity (
math.inf) will be used in the minimax algorithm. Default isNone.
Methods
minimax(grid, depth[, alpha, beta, ...])Alpha-beta-pruned minimax algorithm
play(play_fn, game)Play the AI opponent
score(winner)Calculate the game score for a winner node
- minimax(grid, depth: int, alpha: int = None, beta: int = None, maximizing_player: bool = False)
Alpha-beta-pruned minimax algorithm
- Parameters:
- grid: Grid
Current game state to calculate the minimax value from.
- depth: int
Current search depth of the minimax algorithm.
- alpha, beta: int
Values used for alpha-beta pruning.
- maximizing_player: bool
Whether the current player is maximizing or minimizing his score.
- Returns:
- int
Score of the given grid layout.
See also
src.ai.MinimaxPlayer.minimaxminimax without alpha-beta pruning
src.ai.random module
Random Opponent
- class src.ai.random.RandomPlayer
Bases:
AIPlayerRandomly Playing Computer Opponent
Methods
play(play_fn, game)Play the AI opponent
- play(play_fn, game)
Play the AI opponent
- Parameters:
- play_fn: `callable` [tuple[int, int]]
Play function receiving a board position (x, y) on which to place the AI’s symbol.
- game: Game
Game object containing the game logic and the current game state.
- Returns:
- tuple[int, int]
Random position on the board chosen by the algorithm.
Module contents
Collection of AI Computer Opponents