Implementing a Python Tic-Tac-Toe game
Let's build a basic implementation of Tic-Tac-Toe so we can see what an implementation of the min-max algorithm looks like. If you do not feel like copying all of this, you can find the full code in the GitHub repository https://github.com/DanielSlater/PythonDeepLearningSamples in the tic_tac_toe.py
file.
In the game board, we will be represented by a 3 x 3 tuple of integers. Tuples are used instead of lists so that later on, we can get equality between matching board states. In this case, 0 represents a square that has not been played in. The two players will be marked 1 and -1. If player one makes a move in a square, that square will be marked with their number. So here we go:
def new_board(): return ((0,0,0), (0,0,0), (0,0,0))
The new_board
method will be called before the play for a fresh board, ready for the players to make their moves on:
def apply_move(board_state, move, side): move_x, move_y = move state_list = list...