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, (e.g. 3, 4, or 5 for a 3-by-3, 4-by-4, or 5-by-5 squared board respectively) and press the <ENTER> key, 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.
Before beginning, check out the TicTacToe project from your Subversion repository by following these steps:
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 of 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, it helps because 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 of white 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 graphics module.
If you want the user to click on the window to make it
disappear instead of making it lingering for a few seconds,
include the line
window.getMouse()
before your
window.close()
statement instead of the line
time.sleep(3).
Before going on to the next step, commit your project to your Subversion repository by right-clicking it and choosing Team → Commit... Please make sure your commit comment indicates Phase 0.
Before going on to the next step, commit your project to your Subversion repository. Please make sure your commit comment indicates Phase 1.
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, drawX() and drawO(), 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).
Before going on to the next step, commit your project to your Subversion repository. Please make sure your commit comment indicates which phase you just completed.
Before going on to the next step, commit your project to your Subversion repository. Please make sure your commit comment indicates which phase you just completed.
Before going on to the next step, commit your project to your Subversion repository. Please make sure your commit comment indicates which phase you just completed.
, then the corresponding nested list is
Before going on to the next step, commit your project to your Subversion repository. Please make sure your commit comment indicates which phase you just completed.
Congratulations on completing this interesting project. This was a challenge but you did well by completing it.
Commit your project to your Subversion repository. Please make sure your commit comment indicates that you have completed the project.