2010 January 12 / j d a v i s @ c a r l e t o n . e d u
The purpose of the assignment is to practice logic and writing Python functions. Specifically, you will write an artificial intelligence to play the game Tic Tac Toe. After you have completed the assignment, your artificial intelligence will be hooked up to a variety of user interfaces:
In order to talk about the work to be done, let's lay out some terms. A board is an object representing the Tic Tac Toe board at some stage in the game. The board is a 3 x 3 grid of squares. Each square contains a player — either
' '. Of course,
'o' represent the actual players of the game; the
' ' is used to mark squares where no player has moved yet. If the board is stored in a variable
b, then here's how you access the individual squares in the board:
For example, to test whether player
'x' is in the bottom right corner of the board
b, you type
if b == 'x': ...
To put player
'o' into the middle space of the board
b, you type
b = 'o'
(The board is actually a list of lists. The players are actually strings. We'll talk about lists and strings in Python soon; for this assignment you need to know very little about them.)
Before continuing, download
tictactoe.py to your computer (by right-clicking or control-clicking on that link and saving the file). It already contains some code that I have written. There are two incomplete functions,
nextBoard(). Your job is to write these functions.
hasWon() takes in two inputs — a board
b and a player
p — and returns a Boolean indicating whether that player has won the game with that board. In the version I gave you, the function simply returns
True all of the time; you need to fix this.
nextBoard() takes in two inputs — a board
b and a player
p — alters the board to reflect the next move by the player, and returns the board. For example, if the board looks like
and the player is
nextBoard() should alter the board to look like
and return that board. You may assume that the player
p is either
nextBoard() will never be asked to make a move on behalf of the "extra" player
' '. In the version I gave you, the function simply returns the board unaltered; you need to fix this.
This function is the essence of the artificial intelligence for Tic Tac Toe. It must recognize when the player in question is about to lose (as in the example above), and prevent the loss. It must recognize when the player in question is about to win, and seal the win. If the player in question is not going to win or lose immediately, then there is more flexibility in choosing what to do. I see two obvious algorithms for choosing a move in this case:
Once you have written these functions, you can test them by running
tictactoe.py as a program (i.e. typing
python tictactoe.py in
bash). This runs the demonstration code at the bottom of the
tictactoe.py file. The demonstration has the artificial intelligence play for both players and prints out the board after each move.
Submit your work electronically, as usual. It will be graded according to these criteria:
hasWon()works (3 points).
nextBoard()works (3 points).