CIS200: Assignment 6
10 points.
Due Saturday, 5 April, 10:00pm
We will write a Graphical User Interface for the Pente game that
we developed in Assignment 5.
Program behavior:
When the game is started, the board displays as an empty 9-by9 grid:
|
The human places a mark by clicking on an empty square. If the
move is legal, an X appears, and the computer immediately plays
its "O". The square where the computer placed its mark is
highlighted in yellow:
|
|
Captures are displayed in the usual way, e.g., for
|
|
if the human places a mark, say, in the upper right corner, the computer
does a capture:
|
|
If the human tries to place a mark in an occupied square, nothing
happens. Finally, when there is a winner, this is announced at
the bottom of the GUI, and all the buttons are deactivated so that
no more marks can be inserted:
|
|
Designing the program:
We will reuse the modules, ComputerPlayer.py and
PenteBoard.py. The latter is extended with two new, useful
functions:
def contents(r, c) :
"""contents returns the contents of the board at coordinates, r,c.
parameters: r - an int, the row number; c - an int, the column number.
returns the contents, represented as a one-letter string.
(If r,c are bad coordinates, an empty string is returned.)
"""
def getCapturesFor(mark) :
"""getCapturesFor returns the number of captures made by mark"""
I have supplied complete codings of these two modules. (So, if you
didn't finish assignment 5, you can start fresh on this assignment.)
You can find them on the assignments page, at
http://www.cis.ksu.edu/~schmidt/200s08/Assign.
Your job is to write the controller/view module, PenteGUI.py.
This module generates the view of the Pente board and the labels that display
the numbers of captures. Your module must also generate the event-handling
functions for each of the buttons (squares) in the GUI, so that when the
human player presses a button, an X is deposited into the
computer's PenteBoard and an X is painted on the pressed button.
The computer player is also asked to make its move.
Follow these stages of development
Although the PenteGUI module is small, it employs many
technical ideas from Chapter 8. You should build the module in
these stages:
-
Write the commands that construct a 9-by-9 grid of buttons.
For each button on the grid, you must call PenteBoard.contents
to get the letter that is displayed as the button's text. For example,
if you construct a new_button that is placed at row i,
column j of the grid, you write the command,
new_button.configure(text = PenteBoard.contents(i,j))
to configure the new button's text. Let PenteBoard
remember where the marks lie on the game board --- do not rebuild
the board in PenteGUI!
-
Next, write a function that constructs event-handling functions
for the buttons:.
def makeHandler(myrow, mycolumn) :
"""makeHandler constructs a handler function for a new button.
parameters:
myrow - an int, the row coordinate where the new button lives
mycolumn - an int, the column coordinate where the new button lives
returns: the handler function customized for a new button
"""
Then, use the technique shown in the lecture notes to construct a new
event-handling function for each button. Write the event-handling
function so that when the human presses a button, the event handler
calls PenteBoard.makeMove(row,column,"X"), where row
and column are the coordinates where the button is situated
in the GUI.
Next, write a function, repaint(),
that repaints all the texts on all the buttons
of the GUI. Call this function within the event handler after the
human's move is made.
-
Next, activate the ComputerPlayer module so that
when a human presses a button and the human's move is completed,
a call to ComputerPlayer.makeMove() causes the computer
to insert an "O" within the PenteBoard. Call repaint()
so that you can see both the human's move and the computer's move.
If you did this properly, even the captures will occur automatically,
because they are computed by PenteBoard.makeMove.
-
Once the GUI is correctly displaying the rounds of moves between
human and computer, add two labels to the top of the GUI that
display the count of captures made by human and computer.
Use PenteBoard.getCapturesFor to learn how many
captures have been made by a player.
-
Next, reset the background text to yellow for the square selected
by the computer player:
computers_move = ComputerPlayer.makeMove()
c_row = computers_move[0]
c_column = computers_move[1]
. . .
but.configure(bg = "yellow") # where but is the button located at
# coordinates, c_row, c_column in the grid
-
Finally, add a label to the bottom of the GUI to display when a player
has won the game. Use PenteBoard.checkWinner to learn
if a player has won.
Once a player wins, disable all the event-handling functions so that
the human cannot add any more marks to the board.
Customizing the GUI:
After you finish the program as required, you are welcome to customize
the GUI as you wish.
You can learn how to customize the widgets at
http://www.pythonware.com/library/tkinter/introduction.
This link also has useful information about other functions
(``methods'') for widgets that might prove useful
to simplifying your solution.
Documenting and submitting the program:
You must write documentation strings for all functions and
modules in your program.
Follow the format shown in the Lecture Notes.
Place all files into a folder named Assign6.
You must ``zip'' (compress) the folder before you submit it
at the CIS200 web page:
-
Locate the file-folder icon for folder Assign6 and
right-click on it.
-
From the menu that you see, select Send to, and from the
new menu you see, select Compressed (zipped) folder.
This will build a file named Assign6.zip.
Submit Assign6.zip at
http://www.cis.ksu.edu/~schmidt/200s08/Assign.
Reminder
This is a single-person assignment. You are welcome to discuss the
assignment with your instructors, tutors, and other students,
but the algorithm you design and the Python program that you write and
submit must be your own work, just as if you are submitting
an essay for an English Composition course.