Python代写 | CS 177 Spring 2020 Project #2

这个作业是用Python完成一个井字棋游戏
CS 177 Spring 2020
Project #2
Due Date
This assignment is due by 11:59 pm, Monday, April 6
th and should be submitted to the Project 2
assignment on BrightSpace. Late submissions will receive a 50% grade deduction
Read the entire Project 2 document before starting this assignment
The Project Description: Tic-Tac-Toe v2.0
Your Tic-Tac-Toe program from Project 1 was a smashing success and you’ve now been assigned the
task of creating a fully interactive, graphic version of the game. The user will interact with the game
using only the mouse instead of the IDLE terminal window. There will be two Graphic windows: the
Control Panel and the Tic-Tac-Toe Game Board. You’ve been asked to have the game startup with a
“Splash Screen” graphic to add a bit of flavor and excitement to the program and given some sketches
of how each of the Graphic windows should appear. NOTE: The figures provided in this assignment
are merely intended as sketches of the Graphics windows, their contents and functionality. They are
not meant to be matched exactly.
The Control Panel Window: Do you want to play a game?
Tic-Tac-Toe v2.0 starts by displaying the Control Panel, a 400×400 pixel Graphics window which starts
by displaying a two Images as a “splash screen” for 3 seconds as seen in Figure 1. This splash screen
is made up of two GIF files which are provided with the Project 2 assignment on BrightSpace. A Text
object displaying “v2.0” should be placed between them in the Control Panel window.
After the 3 second delay, the lower Image is undrawn and the Control Panel should display controls
allowing the user to choose between playing a 1-PLAYER or 2-PLAYER game, or to EXIT the program
entirely (see Figure 2). The 1-PLAYER and 2-PLAYER controls should be Ovals filled with colors of
your choice, the EXIT control should be a red-filled Rectangle. Label the controls using Text objects
placed on top.
Figure 1 –The Splash Screen displays for 3 seconds Figure 2 – The Control Panel after the Splash Screen
CS 177 – Project #2 Spring 2020
Page 2 of 7
The Game Board Window: Playing the Game
If the user clicks on the 1-PLAYER or 2-PLAYER control, a 600×600 Graphics window for the Game
Board should be created and a game should start. A Tic-Tac-Toe grid should be drawn in the center of
the window. Two Text objects will be used to display prompts and directions to the players. These
should be placed near the top and bottom center of the Game Board window, (see Figures 3-4 for
sketches of the Game Board window).
Game play rules:
 Essentially the same as in Project 1 except there is no interaction with the IDLE terminal window
 In a 2-Player game, Player 1 uses the “X” marker, Player 2 uses “O”
 In a 1-Player game, Player 1 uses the “X” marker and the computer uses “O”
 Human players will use the mouse to click in the Game Board window grid to place their marker
 The computer player will select their location automatically (see Implementing A.I.)
 The message at the top of the Game Board window should change to indicate the current
player’s turn
 While a game is being played, the message at the
bottom of the Game Board window will show,
“Click to place a marker”
 After each marker is placed, the program should
check for a winner
When a winner is detected (see Figure 5):
 A red Line (width 4) should be drawn through the
winning markers
 The message at the top of the Game Board window
will indicate the winner, (ie: “Player 2 wins”)
 The message at the bottom of the Game Board
window will show, “Click to close”
Figure 3 – The Game Board window Figure 4 – The Game Board window
Figure 5 – Player 2 is the winner
CS 177 – Project #2 Spring 2020
Page 3 of 7
Messages at the Top of the Game Board window:
There are seven (7) different messages that might be displayed at the top of the Game Board window
depending on number of players and the current status of the game:
 Your turn Player 1
 Your turn Player 2
 My Turn
 Player 1 wins!
 Player 2 wins!
 I win!
 It’s a tie…
Messages at the Bottom of the Game Board window:
There are three (3) different messages that might be displayed at the bottom of the Game Board window
depending on whether a game is in progress or the game is over:
 Click to place a marker
 Thinking…
 Click to close
Implementing A.I: The Unbeatable Tic-Tac-Toe Wizard
When a 1-Player game is selected, a human is Player 1 playing the
“X” marker and their opponent is the computer artificial intelligence
(A.I.) utilizing an “O” marker. In Project 1, the computer player
selected a random, empty location on the Tic-Tac-Toe board. For
Project 2 you will implement an A.I. Tic-Tac-Toe computer player
that cannot lose. This will be done through pattern matching where
the program compares the locations that “X” and “O” markers
have been played (the current state) to a series of possibilities in a
data file to find the best option for the next move.
For example: If the current Tic-Tac-Toe game grid looked like the
example in Figure 6, the locations could represented by the following List of 9 characters:
grid = [‘X’, ‘ ‘, ‘X’, ‘ ‘, ‘O’, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘]
Each index position represents a location on the grid with indexes 0-2 representing the top row,
indexes 3-5 representing the middle and indexes 6-8 representing the bottom row. In this case,
grid[0] and grid[2] contain the ‘X’ markers and grid[4] contains the ‘O’ marker.
To determine the best option, the program would compare the current List with a series of values in a
data file to find one with “X” and “O” markers in the same location. The data file would then indicate
the best possible location for the A.I. to place the next “O” marker.
Figure 6 – It’s the A.I.’s turn
CS 177 – Project #2 Spring 2020
Page 4 of 7
Reading the data file: AIPlayer.txt
Provided with this assignment is a data file named AIPlayer.txt
containing more than 2,100 unique possibilities for the current state of
the board. The first nine characters of each row represent one position
on the Tic-Tac-Toe grid, followed by a colon “:” then a single Integer
representing the best location for the computer to place their marker.
When it’s the A.I.’s turn in a 1-Player game, the program should
compare the current state of the board to the patterns from the data file.
When a matching pattern is found, the Integer at the end of the line
represents the List index where the A.I. player should place their “O”
marker.
For example: If the current Tic-Tac-Toe game grid looked like the
example in Figure 6, and locations are represented by the List of 9
characters:
[‘X’, ‘ ‘, ‘X’, ‘ ‘, ‘O’, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘]
This pattern matches the last line shown in Figure 7 above, so once that pattern is found and matched,
the A.I. player knows to place their “O” marker at index 1, resulting in the List:
[‘X’, ‘O’, ‘X’, ‘ ‘, ‘O’, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘]
Note that this is effectively a blocking move keeping Player 1 from winning across the top row.
About the Project: The Tasks
In this project, you will complete several tasks:
1. Setup your project2.py file
2. Defining the overall program structure
3. The Control Panel window
4. The Game Board window
5. Playing the game with 2 Players
6. Implementing 1 Player mode with the computer player
TODO #1: Setup your project2.py file
The Python program file should be setup with the following guidelines:
 File name is project2.py
 All library import statements should be at the top of the file
 File includes a header describing program, its purpose and function
 Header includes your name, the program name and a description of its function
 All Python code should be contained within function definitions with the exception of:
 Library import commands
 Header comments
 Function definition statements themselves — i.e. def main():
 The main() statement which starts the program
X X OOX :1
X X X OO:6
X X X O O:7
X X X OO :8
X X XO O:1
X X XO O :1
X X XOO :1
X X XOOXO:1
X X O :1
Figure 7 – Excerpt from data file
CS 177 – Project #2 Spring 2020
Page 5 of 7
TODO #2: Defining the overall program structure
You may write your program to meet the requirements in this document however you choose,
however the 1-PLAYER, 2-PLAYER and EXIT controls in the Control Panel window must be
functional at all times – including while a game is being played. This will require the use of the
.checkMouse() method when looking for mouse clicks in the Graphics windows.
The example structure in Figure 8 would allow the program to continue to loop, checking for clicks on
the Control Panel window even while the game is being played. Of course, there’s much more to the
overall program, but this might give you a helpful start when writing your main() function.
# initialize variables
# loop until click on EXIT
# checkMouse() in control panel window
# was 1Player clicked?
# is game already active?
# close current game board window
# initialize 1Player variables
# open game board window for 1 player
# was 2Player clicked?
# is game already active?
# close current game board window
# initialize 2Player variables
# open game board window for 2 players
# is game in progress?
# give current player chance to take turn
# is there a winner?
# display winner text
# wait for click
# close game board window
# reset variables
Figure 8 – Example program logic and loop structure
CS 177 – Project #2 Spring 2020
Page 6 of 7
TODO #3: The Control Panel window
Write a separate Python function that creates the Control Panel window as defined in the program
description. This Graphics window must meet the given specifications.
This function should perform the following tasks:
1. Define a 400×400 pixel Graphics window with a light grey background
2. Draw the two Image objects using the GIF files provided with Project 2
3. Draw the Text object displaying “v2.0” as specified
4. Waits for 3 seconds before continuing
5. Remove the lower Image object
6. Draw the three controls: 1-PLAYER, 2-PLAYER and EXIT as specified
7. Return the Graphics window and three control objects
TODO #4: The Game Board window
Write a separate Python function that creates the Game Board window as defined in the program
description. This Graphics window must meet the given specifications.
This function should perform the following tasks:
1. Define a 600×600 pixel Graphics window with a white background
2. Draw the two Text objects, “top” and “bottom” as specified in the program description
3. Draw the lines representing the Tic-Tac-Toe grid
4. Return the Graphics window, Text objects and any other control objects necessary
TODO #5: Playing the game with 2 players
Write the code necessary to allow 2 players to play a full game, including detecting clicks in the Game
Board window, placing the “X” and “O” markers and detecting a winner.
Keep in mind that your program should continue to respond to clicks in the Control Panel window
even while a game is in progress.
TODO #6: Implementing 1 player mode with A.I.
Write the code necessary to support a 1 player game including the logic to perform the computer’s
turn utilizing the artificial intelligence patterns in the AIPlayer.txt data file. This should support a
full game including detecting Player 1’s clicks in the Game Board window, placing the “X” and “O”
markers and detecting a winner.
Keep in mind that your program should continue to respond to clicks in the Control Panel window
even while a game is in progress.
Submit to BrightSpace
 Upload your completed file (project2.py) to BrightSpace by 11:59 pm on Monday, April 6
th
 Late submissions will receive a 50% grade deduction
CS 177 – Project #2 Spring 2020
Page 7 of 7
Project 2 Grading Rubric Points
TODO #1: Setup your project2.py file
File name is project2.py 5
All library import statements are at the top of the file 5
Program header included with student name, program name and description 5
All Python code is contained within functions 10
TODO #2: The overall program structure and game play
Program responds to clicks on Control Panel controls at all times 10
Program ends without errors when click is detected on EXIT control 10
Overall loop controlling program operation defined within a function 10
Game play follows rules and guidelines provided 15
TODO #3: The Control Panel window
Separate function written to create Control Panel window 5
Graphics window is created as specified 5
Image and Text objects are defined as specified 10
After 3-second delay, Image object is removed 5
1-PLAYER, 2-PLAYER and EXIT controls drawn as specified 10
Graphics window and objects are returned 5
TODO #4: The Game Board window
Separate function written to create Game Board window 5
Graphics window is created as specified 5
Text and Tic-Tac-Toe grid objects are defined as specified 10
Graphics window and objects are returned 5
TODO #5: Playing the game with 2 players
Two player game is started when 2-PLAYER control is clicked 10
Clicks on game grid are detected and “X” and “O” markers placed correctly 10
Winner/tie is detected correctly, red Line drawn to indicate winning markers 10
TODO #6: Implementing 1 player mode with A.I.
One player game is started when 1-PLAYER control is clicked 10
Program reads all data from AIPlayer.txt file correctly 5
Program uses data to select the correct placement of “O” markers 20
Computer player always wins or ties. Computer cannot be beat 20
Winner/tie is detected correctly, red Line drawn to indicate winning markers 10
Style / Format: Python code is formatted, commented and easy to understand 20
Total Points 250