Question
PROGRAM SPECIFICATION This program plays tic tac toe. The game window is 600 pixels wide and 500 pixels high. The game board is 300
"""
PROGRAM SPECIFICATION
This program plays tic tac toe.
The game window is 600 pixels wide and 500 pixels high. The game
board is 300 x 300 centered in the screen. Each cell is 100 x 100.
The play area is drawn with two horizontal lines and two vertical lines,
which separate the 9 cells on the grid.
The program begin the grid blank, and it being xs move. Players x and
o will take turns by clicking in one of the nine cells on the grid until
the game is over. If a player clicks in a cell where it is not legal to
move (either because the game is over, or the cell is occupied), nothing
happens.
The reset button will be a rectangle located above and to the left of
the play area, with the text 'reset' inside it. If the reset button is
clicked, the game is reset to its initial state with all cells blank
and x to move.
When the game is over, a message giving the result of the game
("x won", "o won", or "tie game") will be displayed until the
reset button is clicked.
The file graphics.py must be in the same folder as this source code in order for the program to run. graphics.py is downloadable at http://mcsp.wartburg.edu/zelle/python/
"""
"""
DATA MODEL
A *cell* is a natural number in {1..9}. Cells represent
squares on the tic tac toe board as pictured below:
1|2|3
-----
4|5|6
-----
7|8|9
An *image* is a drawable object from graphics.py. This includes points,
lines, and circles, and texts, written as below:
*) Point(x,y) is a point with coordinates x and y (horizontal and vertical
distance from the top left corner of the graphics window)
*) Line(p,q) is a line segment from point p to point q.
*) Circle(p,r) is a circle with center point p and radius r.
*) Text(p,str) is an image of the string str centered at point p.
A *sprite* is a set of images.
A *position* is a set of cells, representing the set of cells occupied by
a player at some stage of the game.
A *gameState* is a pair of positions. The game state (xs,os) represents
the game state in which xs is the set of cells occupied by 'x', and os
is the set of cells occupied by 'o'.
"""
# display: position*position->sprite
#
# display(xs,os) is the sprite consisting of the hash marks in the play
# area, the reset button, and all x's and o's on the board, as well as
# the results message in case the game is over.
# YOUR CODE GOES HERE
# newXsOs:position*position*cell -> gameState
#
# If legalMove(c,xs,os), then newXsOs(xs,os,c) is the game state
# resulting from making a move in cell c in the state where x occupies
# the cells in xs and o occupies the cells in os.
# YOUR CODE GOES HERE
#legalMove: cell*position*position -> Bool
#
# (c,xs,os) means that in game state (xs,os) it is legal to move
# in cell c.
# YOUR CODE GOES HERE
# inCell: cell*int*int -> bool
#
# inCell(c,x,y) if and only if point p is in the interior of cell c.
# YOUR CODE GOES HERE
# inResetButton: int*int -> bool
#
# inResetButton(x,y) if Point(x,y) is in the interior of the 'reset' button
# YOUR CODE GOES HERE
######################################################################
# Tic Tac Toe
# Import graphics library by John Zelle,Wartburg Univ.
# downloadable at http://mcsp.wartburg.edu/zelle/python/
from graphics import *
# Create a window to play in
displayWidth = 600
displayHeight = 500
gameWindow = GraphWin("My game", displayWidth , displayHeight)
# Initialize the program
# The state variables in this program are xs and os. The game state
# (xs,os) reflects the state of the board at any point in program
# execution.
(xs,os) = (set(),set())
######################################################################
# The main loop
while(True):
print(xs,os)
# Draw the display
images = display(xs,os)
for x in images: x.draw(gameWindow)
# wait for a mouse click, and store its
# coordinates in click
c = gameWindow.getMouse()
click = (c.getX(),c.getY())
# if the reset button has been clicked, reset the
# game state
if inResetButton(click[0],click[1]):
(xs,os) = (set(),set())
# If a cell has been clicked and it is legal to move there,
# add an appropriate mark to that cell.
for c in range(1,10):
if inCell(c,click[0],click[1]) and legalMove(c,xs,os):
(xs,os) = newXsOs(xs,os,c)
# undraw the screen
for I in images: I.undraw()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started