TicTacToe is usually played on a 3-by-3 square board. To make things more interesting (and to help you learn how to write flexible code) our version will allow any square board size. To see what the finished product might look like, run this file: TicTacToe6.pyc. After you enter the board size (I suggest entering 3, 4, or 5), and press Enter, click the mouse on the board. Note that it is possible that the board will pop up behind other windows. If you don't see the board after entering the board size, try moving or hiding some other windows.
This task is fairly simple, but you should do it in a way that will help you plan ahead for future versions. Here are a few lines in my code that you may want to use. Defining named constants and using them in your code instead of numbers can avoid the use of "magic numbers" that can make code difficult to enhance and maintain. In this case, the board size will not always be 3 in later versions of the program,
BOARDSIZE = 3 # number of rows and columns BOARDRANGE = range(BOARDSIZE) # range of rows and columns PPS = 150 # pixels per square WINDOWSIZE = PPS * BOARDSIZE # width (and height) of window INSET = 15 # num pixels around X's and O's in squares
If you want to make the board linger on the screen for a few
seconds and then disappear like the sample program does,
import the time module, and include the line
time.sleep(3) before your window.close() statement.
Don't forget that you also need to import the zellegraphics module.
def rectUpperRight(row, col): 'coordinates of top right of inset X or O' return Point(PPS*(col+1) - INSET, PPS*row + INSET)Call each function with some different values for the parameters and verify that they work correctly. Can you put an X and an O in the same square? (In a later step you will solve this).