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.
To make it easy to download and run all of the "pyc" files, there is a ZIP file that contains all of them.
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 # length of the square in pixels (PPS = "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.
When you finish this step, you may want to look at our solution; It may give you some ideas that can simplify your work in future steps. If our solution to this step or any later step contains any code that you do not understand, you should ask your instructor or one of the student assistants to help you understand it.
TicTacToe.py
program (don't start a new program) so it asks the user for the board size, then draws the grid, as in this program:
tictactoe1.pyc . The number the user enters can
become the new value of BOARDSIZE; then after calculating WINDOWSIZE so that it
will hold the different-sized board, you probably do not have to change the rest
of the program . If you are a beginning programmer, you may want to skip the
part that lets the user simply press ENTER, and instead require the user to
always enter a number. (Hint for everyone else: if the user just presses ENTER,
the returned string will be the empty string: ""
).
When you finish this step, you may want to look at our solution; It may give you some ideas that can simplify your work in future steps. If our solution to this step or any later step contains any code that you do not understand, you should ask your instructor or one of the student assistants to help you understand it.
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).
When you finish this step, you may want to look at
our solution; If you don't
understand something there, ask.
When you finish this step, you may want to look at
our solution; If you don't
understand something there, ask.
When you finish this step, you may want to look at
our solution; If you don't
understand something there, ask.
When you finish this step, you may want to look at
our solution;
It may give you some ideas that can simplify your work in future steps.
When you finish this step, you may want to look at
our solution;
It may give you some ideas that you can use in future programs that you write.