MONT 105S, Spring 2009

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

MONT 105S Homework 13

Tic Tac Toe

Due Friday, March 20, at the beginning of class.

Introduction
In this assignment, you will write a program to allow two users to play Tic Tac Toe. The goal of this assignment is to increase your familiarity with functions in Python.

Program Description
Your program will do the following. First, using a Turtle object, it will draw the Tic-Tac-Toe board. It will then ask whether you would like to play Tic Tac Toe. If you answer yes, it will ask for the row and column where you want to play. It will draw an X in the appropriate row and column. It will then ask if you would like to continue playing.

As long as the user answers yes, the program will get the row and column numbers for the next move. It will alternate drawing X's and O's (i.e. on the first move it will draw an X, on the second move it will draw an O, on the third move it will draw an X, and so on).

Writing the Program
a) Writing the function definitions
Your program will use 3 functions. Two of the functions are the ones we wrote in lab 6: drawX(row, col) and drawO(row, col). You might want to make a copy of your lab 6 program and use that as a starting point for your tic tac toe program.

Add a third function definition, drawBoard( ), that will draw the Tic Tac Toe grid. The grid consists of 4 lines, 2 horizontal and 2 vertical, whose endpoints have the following coordinates:

The definition of this function should come after you declare the Turtle object (yertle = Turtle()) and before the other two function definitions.

To write this function, use the goto(x, y) function to move the Turtle to the beginning point of a line (e.g. (-75, -25)). Then move the turtle forward by 150 pixels. For the vertical lines, you will have to use the left( ) or right( ) functions to turn the Turtle 90 deg so it will move up or down on the screen. Make sure you pick the pen up when you are moving the turtle to the beginning of a line, and put the pen down when you want to draw.

NOTE: You must end with the turtle facing to the right (otherwise the other functions will not work properly). So if you turn the turtle right by 90 degrees to draw a vertical line, you will need to turn it back to the left by 90 degrees at the end of the function.

b) Writing the main program
Write the main program with the following steps:

The row and column numbers for each grid square are given in the following diagram:

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

>>> 
Would you like to play tic tac toe? yes
What row (0 - 2) is your move in? 0
What column (0 - 2) is your move in? 0
Would you like to continue playing? yes
What row (0 - 2) is your move in? 2
What column (0 - 2) is your move in? 2
Would you like to continue playing? yes
What row (0 - 2) is your move in? 1
What column (0 - 2) is your move in? 2
Would you like to continue playing? yes
What row (0 - 2) is your move in? 2
What column (0 - 2) is your move in? 1
Would you like to continue playing? no
Game over!
>>> 

The turtle drawing from this game will look as follows:

To Submit Your Finished Project:
1. Hand in a hard copy of the file <username>_TicTacToe.py. Hand this 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>_TicTacToe.py file to me at croyden@mathcs.holycross.edu

Get started early and have fun!