-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtictactoe.py
185 lines (149 loc) · 5.75 KB
/
tictactoe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/python
# Tic-Tac-Toe
# pyGame Demo
# Nathan R. Yergler
# 13 May 2002
# import necessary modules
import pygame
from pygame.locals import *
# declare our global variables for the game
XO = "X" # track whose turn it is; X goes first
grid = [ [ None, None, None ], \
[ None, None, None ], \
[ None, None, None ] ]
winner = None
# declare our support functions
def initBoard(ttt):
# initialize the board and return it as a variable
# ---------------------------------------------------------------
# ttt : a properly initialized pyGame display variable
# set up the background surface
background = pygame.Surface (ttt.get_size())
background = background.convert()
background.fill ((250, 250, 250))
# draw the grid lines
# vertical lines...
pygame.draw.line (background, (0,0,0), (100, 0), (100, 300), 2)
pygame.draw.line (background, (0,0,0), (200, 0), (200, 300), 2)
# horizontal lines...
pygame.draw.line (background, (0,0,0), (0, 100), (300, 100), 2)
pygame.draw.line (background, (0,0,0), (0, 200), (300, 200), 2)
# return the board
return background
def drawStatus (board):
# draw the status (i.e., player turn, etc) at the bottom of the board
# ---------------------------------------------------------------
# board : the initialized game board surface where the status will
# be drawn
# gain access to global variables
global XO, winner
# determine the status message
if (winner is None):
message = XO + "'s turn"
else:
message = winner + " won!"
# render the status message
font = pygame.font.Font(None, 24)
text = font.render(message, 1, (10, 10, 10))
# copy the rendered message onto the board
board.fill ((250, 250, 250), (0, 300, 300, 25))
board.blit(text, (10, 300))
def showBoard (ttt, board):
# redraw the game board on the display
# ---------------------------------------------------------------
# ttt : the initialized pyGame display
# board : the game board surface
drawStatus (board)
ttt.blit (board, (0, 0))
pygame.display.flip()
def boardPos (mouseX, mouseY):
# given a set of coordinates from the mouse, determine which board space
# (row, column) the user clicked in.
# ---------------------------------------------------------------
# mouseX : the X coordinate the user clicked
# mouseY : the Y coordinate the user clicked
# determine the row the user clicked
if (mouseY < 100):
row = 0
elif (mouseY < 200):
row = 1
else:
row = 2
# determine the column the user clicked
if (mouseX < 100):
col = 0
elif (mouseX < 200):
col = 1
else:
col = 2
# return the tuple containg the row & column
return (row, col)
def drawMove (board, boardRow, boardCol, Piece):
# draw an X or O (Piece) on the board in boardRow, boardCol
# ---------------------------------------------------------------
# board : the game board surface
# boardRow,
# boardCol : the Row & Col in which to draw the piece (0 based)
# Piece : X or O
# determine the center of the square
centerX = ((boardCol) * 100) + 50
centerY = ((boardRow) * 100) + 50
# draw the appropriate piece
if (Piece == 'O'):
pygame.draw.circle (board, (0,0,0), (centerX, centerY), 44, 2)
else:
pygame.draw.line (board, (0,0,0), (centerX - 22, centerY - 22), \
(centerX + 22, centerY + 22), 2)
pygame.draw.line (board, (0,0,0), (centerX + 22, centerY - 22), \
(centerX - 22, centerY + 22), 2)
# mark the space as used
grid [boardRow][boardCol] = Piece
def clickBoard(board):
# determine where the user clicked and if the space is not already
# occupied, draw the appropriate piece there (X or O)
# ---------------------------------------------------------------
# board : the game board surface
global grid
(mouseX, mouseY) = pygame.mouse.get_pos()
(row, col) = boardPos (mouseX, mouseY)
# make sure no one's used this space
if ((grid[row][col] == "X") or (grid[row][col] == "O")):
# this space is in use
return None, None
# draw an X or O
drawMove (board, row, col, "X")
return board, row, col
def gameWon(board):
# determine if anyone has won the game
# ---------------------------------------------------------------
# board : the game board surface
global grid, winner
# check for winning rows
for row in range (0, 3):
if ((grid [row][0] == grid[row][1] == grid[row][2]) and \
(grid [row][0] is not None)):
# this row won
winner = grid[row][0]
pygame.draw.line (board, (250,0,0), (0, (row + 1)*100 - 50), \
(300, (row + 1)*100 - 50), 2)
break
# check for winning columns
for col in range (0, 3):
if (grid[0][col] == grid[1][col] == grid[2][col]) and \
(grid[0][col] is not None):
# this column won
winner = grid[0][col]
pygame.draw.line (board, (250,0,0), ((col + 1)* 100 - 50, 0), \
((col + 1)* 100 - 50, 300), 2)
break
# check for diagonal winners
if (grid[0][0] == grid[1][1] == grid[2][2]) and \
(grid[0][0] is not None):
# game won diagonally left to right
winner = grid[0][0]
pygame.draw.line (board, (250,0,0), (50, 50), (250, 250), 2)
if (grid[0][2] == grid[1][1] == grid[2][0]) and \
(grid[0][2] is not None):
# game won diagonally right to left
winner = grid[0][2]
pygame.draw.line (board, (250,0,0), (250, 50), (50, 250), 2)