MONT 105S, Spring 2009

Home | | Course Schedule | | Assignments | | Lecture Notes

MONT 105S Homework 15

Tic Tac Toe

Due Friday, April 3, at the beginning of class.

Introduction
In this assignment, you will work with the TicTacToe program you wrote in assignment 13. You will add to this program so that it only allows legal moves (i.e. moves on a board position where there is no X or O yet), determines a winner if a player gets 3 in a row, and declares a draw if all 9 squares are filled with no winner. The purpose of this assignment is to continue working with functions and to increase your familiarity with return statements.

Program Description
In order for your program to check for legal moves, you must provide a representation of the board that the computer can easily check. Although you can see where each move has been made by looking at the board that was drawn by the turtle, the computer cannot see, and once the X or O has been drawn, it does not remember where they were put. We will use a list of integers to represent the squares on the tic-tac-toe board. Each number in the list will represent a location on the tic-tac-toe game board. The value at a given list position will indicate if the corresponding game square is empty (value = 0), or whether there is an X (value = 1) or an O (value = 2) drawn in the square.

The following diagram shows the position numbers assigned to the squares on the board:

For example, if your list consists of the following values:


The 1 in position 2 of the list means that there is an X at position 2 of the board, the lower right corner. The 2 in position 5 of the list means that there is an O in position 5 of the board, the middle right square. The zeros in the other list positions mean that there is nothing in any of the other squares.

In this program, instead of entering a row and column number for each move, the players will simply enter the number of the position where they want to make a move. The program will check to make sure the move is legal (the number is between 0 and 8 and the square is empty) and then make the move. It will then check to see if there is a winner (one or the other player has 3 in a row). As long as there is no winner, and fewer than 9 moves have been made, the program will continue the game. As soon as one player wins, or when all the squares have been filled (9 moves have been made), the program will end the game and either declare the winner or declare a draw.

Writing the Program
To write this program, you will add three functions and make some modifications to the main program you wrote in HW 13. If you like, you can start with the Solution to HW 13 and modify that program.

The three functions you will add are:

Part 1) Writing the getMove( ) and isLegal( ) functions
Start by making a copy of the <username>_TicTacToe.py program you wrote for HW 13 (or a copy of the Solution to HW 13) and save it as <username>_TicTacToe1.py.

a) Modifying the main program
Modify the main program with the following steps:

b) Writing the isLegal( ) function
Add a function, isLegal(pos, board), to your program. The function definition should be placed after the definition of the drawO() function and before the main program. The function takes two parameters. The pos parameter is the position of the move it is checking for legality. The board parameter is the list representing the current moves on the board.

Your function should do the following:

c) Writing the getMove( ) function
Add a function, getMove(player, board), to your program. The function definition should be placed after the definition of the isLegal( ) function and before the main program. The function takes two parameters. The player parameter gives the number associated with the player who's turn it is. The board parameter is the list that indicates the current moves on the board.

Your function should do the following:

Sample Output
The following is an example of the output in the Shell window for a game:

>>> 
Welcome to Tic Tac Toe!
Would you like to play tic tac toe? yes
Where would you like to move (0 - 8)? 0
Would you like to continue playing? yes
Where would you like to move (0 - 8)? 9
You must choose a value between 0 and 8.
That is not a legal move.
Where would you like to move (0 - 8)? 8
Would you like to continue playing? yes
Where would you like to move (0 - 8)? 8
There is already a marker there.
That is not a legal move.
Where would you like to move (0 - 8)? 5
Would you like to continue playing? yes
Where would you like to move (0 - 8)? 7
Would you like to continue playing? no
Game over!
>>> 

The turtle drawing from this game will look as follows:

Save your <username>_TicTacToe1.py program. You will be turning in a copy of this version of the program.

Part 2) Writing the get_winner( ) function.
Make a copy of your <username>_TicTacToe1.py program and name it <username>_TicTacToe2.py You will modify the <username>_TicTacToe2.py program to add the get_winner( ) function and to make use of it to end the game.

a) Writing the get_winner( ) function.
Add a function, get_winner(board), to your program. The definition of get_winner( ) should be after the definition of getMove( ) and before the main program. The get_winner( ) function should test to see whether any player has three in a row along any row, column or diagonal. You can check this by testing whether the appropriate board positions have equal, non-zero, values. For example, if positions, 0, 1 and 2 all have the same non-zero value, then whatever value is stored in those positions indicates the player who has won. The following line of code can test for a winner by three in a row on the bottom row:

	if board[0] != 0 and board[0] == board[1] and board[1] == board[2]:
		return board[0]

There are 8 possible combinations that make three in a row. You need to figure out the position numbers for the remaining 7 combinations and include elif statements for those seven. Finally, if there is no three in a row combination, the function should return a value of zero.

b) Modifying the main program
Modify the main program so that it now ends the game if there is a winner or if it is a draw (9 moves have been made and there is no winner). Modify the main program so it does the following:

Sample Output
The following is an example of the output in the Shell window for a game:

>>> 
Welcome to Tic Tac Toe!
Where would you like to move (0 - 8)? 0
Where would you like to move (0 - 8)? 1
Where would you like to move (0 - 8)? 4
Where would you like to move (0 - 8)? 4
There is already a marker there.
That is not a legal move.
Where would you like to move (0 - 8)? 2
Where would you like to move (0 - 8)? 8
Game over!
Player 1 wins!
>>> 

The following is a picture of the game board after this game:

The following is an example of the output in the Shell window for another game:

>>> 
Welcome to Tic Tac Toe!
Where would you like to move (0 - 8)? 0
Where would you like to move (0 - 8)? 8
Where would you like to move (0 - 8)? 2
Where would you like to move (0 - 8)? 1
Where would you like to move (0 - 8)? 3
Where would you like to move (0 - 8)? 6
Where would you like to move (0 - 8)? 7
Where would you like to move (0 - 8)? 4
Where would you like to move (0 - 8)? 5
Game over!
The game is a draw.
>>> 

The following is a picture of the game board after this game:

To Submit Your Finished Project:
1. Hand in hard copies of the files: <username>_TicTacToe1.py and <username>_TicTacToe2.py. Hand these to your instructor in class on the project's due date.

2. Print your name at the top of the cover page and staple it to the top of your hard copy.

3. In addition to the hard copy listed above, email your <username>_TicTacToe1.py and <username>_TicTacToe2.py files to me at croyden@mathcs.holycross.edu

Get started early and have fun!