src.logic
Submodules
src.logic.grid module
Tic-Tac-Toe Grid
- class src.logic.grid.Grid(turn: str = 'X', size: int = 3)
Bases:
objectTic-Tac-Toe Grid Base Class
Base tic-tac-toe grid supporting pretty-printing, object subscription, winner detection, etc.
- Parameters:
- turn: str, optional {‘X’, ‘O’ }
First player’s symbol, which can be either ‘X’ or ‘O’. Default is ‘X’.
- size: int, optional
Playing field grid size. Defaults to
GRID_SIZEconfig.
Examples
Printing to console:
>>> grid = Grid() >>> grid[0, 1] = 'X' >>> print(grid) . . . X . . . . .
Object subscription:
>>> grid = Grid() >>> grid[2, 1] = 'O' >>> print(grid[2, 1]) O
Iteration over available slots:
>>> grid = Grid() >>> grid[0, 0] = 'X' >>> grid[2, 2] = 'O' >>> for x, y in game.logic.available_slots(): ... print('%d, %d' % (x, y)) 0, 1 0, 2 ... 2, 1
- Attributes:
- grid: `list` [`list` [str or `None`]]
Actual tic-tac-toe grid as a 2D list.
- turn: str {‘X’, ‘O’}
Current player’s symbol.
- size: int
Size of the grid.
Methods
Iterate through available slots in the grid
is_end()Check if the board is full
is_valid(player, pos)Check whether a move is valid.
- available_slots()
Iterate through available slots in the grid
- Yields:
- tuple [int, int]
Position (x, y) on the grid of available slot.
- is_end()
Check if the board is full
Test rows, columns, diagonals and anti-diagonals for a possible winner
- Returns:
- bool or str or int
Returns
Falseif the game is not overReturns
0if the game is a tieReturns the symbol of the winning player if a player has won
Notes
This method starts by walking along the diagonal of the grid, while ruling out impossible rows. Therefore, the minimum possible runtime is
n, wherenis the size of the grid.\[\Omega(is\_end()) \in n\]However, in unlucky situations, the algorithm could possibly have to check every row, column, and the entire board again! This results in a maximum time complexity of:
\[O(is\_end()) = n + 3n^2 \ O(is\_end()) \in n^2\]
- is_valid(player: str, pos: tuple)
Check whether a move is valid.
Checks if it’s the specified player’s turn and the move is on an empty field on the grid.
- Parameters:
- player: str
Symbol of the player trying to make a move.
- pos: `tuple` [int, int]
Position (x, y) of the desired move.
- Returns:
- bool
Whether or not the move is valid.
src.logic.logic module
Tic-Tac-Toe Logic
- class src.logic.logic.Logic(_game, turn: str = 'X')
Bases:
GridTic-Tac-Toe Logic Class
- Parameters:
- _game: Game
Game instance passed from Game initializer.
- turn: str, optional {‘X’, ‘O’}
Beginning player’s turn. Must be either ‘X’ or ‘O’. Default is ‘X’.
Methods
available_slots()Iterate through available slots in the grid
is_end()Check if the board is full
is_valid(player, pos)Check whether a move is valid.
Switch current player's turn to next player
play(x, y)Execute a player's turn on the board
set_at(x, y, val)Set a valid value at a given point on the grid
- next_turn()
Switch current player’s turn to next player
- play(x, y) bool
Execute a player’s turn on the board
Place a player’s symbol on a specified position on the board and change turns.
- Parameters:
- x, y: int
Position on the board at which to place player’s symbol.
- Returns:
- bool or str or int
Returns
Falseif the game is not overReturns
0if the game is a tieReturns the symbol of the winning player if a player has won
- set_at(x, y, val)
Set a valid value at a given point on the grid
- Parameters:
- x, y: int
Position on the grid.
- val: str
Value to be set.
Module contents
Tic-Tac-Toe Logic Module