Tic-Tac-Toe

TicTacToe

Using Minimax, I implemented an AI to play Tic-Tac-Toe optimally. AI50 course project for tictactoe.

There are two main files in this project: runner.py and tictactoe.py. tictactoe.py contains all of the logic for playing the game, and for making optimal moves. You can run python runner.py to play against your AI! I’ve chosen to represent the board as a list of three lists (representing the three rows of the board), where each internal list contains three values that are either X, O, or EMPTY.

image

Video

Functions description

For all functions that accept a board as input, it was assumed that it is a valid board (namely, that it is a list that contains three rows, each with three values of either X, O, or EMPTY).

To test the code, run

python runner.py

and play against your AI. Since Tic-Tac-Toe is a tie given optimal play by both sides, you will never be able to beat the AI (though if you don’t play optimally as well, it may beat you!).