For the rest of this exercise, we’re going to be writing the minimax algorithm to be used on a game of Tic-Tac-Toe. We’ve imported a Tic-Tac-Toe game engine in the file tic_tac_toe.py
. Before starting to write the minimax function, let’s play around with some of the Tic-Tac-Toe functions we’ve defined for you in tic_tac_toe.py
.
To begin, a board is represented as a list of lists. In script.py
we’ve created a board named my_board
where the X
player has already made the first move. They’ve chosen the top right corner. To nicely print this board, use the print_board()
function using my_board
as a parameter.
Next, we want to be able to take a turn. The select_space()
function lets us do this. Select space takes three parameters:
- The
board
that you want to take the turn on. - The
space
that you want to fill in. This should be a number between1
and9
. - The
symbol
that you want to put in that space. This should be a string — either an"X"
or an"O"
.
We can also get a list of the available spaces using available_moves()
and passing the board as a parameter.
Finally, we can check to see if someone has won the game. The has_won()
function takes the board
and a symbol
(either "X"
or "O"
). It returns True
if that symbol has won the game, and False
otherwise.
Let’s test these functions! Write your code in script.py
, but feel free to take a look at tic_tac_toe.py
if you want to look at how the game engine works.
Instructions
Call print_board()
using my_board
as a parameter.
Call select_space()
to put an "O"
in the center of the board. Print the board again after making this move.
Make two more moves of your choice and print the available moves. You can make a move with an "X"
or an "O"
Remember, you can use the available_moves()
function using my_board
as a parameter to get a list of the available moves.
Make enough moves to win the game as "X"
. Use has_won()
to check to see if "X"
has won. Check to see if "O"
has won as well. Print both results.