-
Notifications
You must be signed in to change notification settings - Fork 0
/
five.py
213 lines (163 loc) · 5.42 KB
/
five.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from random import randint
class Board(object):
def __init__(self):
self.board = []
self.flipboard = []
for x in range(0,9):
self.board.append(['-', '-', '-', '-', '-', '-', '-', '-', '-'])
self.flipboard.append(['-', '-', '-', '-', '-', '-', '-', '-', '-'])
## Constructs a board reflected across the diagonal to test for horizontal win
def flip(self):
for x in range(0,9):
for y in range(0,9):
self.flipboard[x][y] = self.board[y][x]
def printout(self):
for x in range(9):
if x == 0:
print "_____________________________________ ", "\n"
for y in range(9):
if y == 0:
print "| ",
print self.board[x][y] + " |",
print "\n", "_____________________________________ "
def has_player_won(self, last_row, last_column):
if self.check_hor_and_vert():
return True
elif self.check_diags(last_row, last_column):
return True
else:
return False
def check_hor_and_vert(self):
if "OOOOO" in self.board or "XXXXX" in self.board:
return True
elif "OOOOO" in self.flipboard or "XXXXX" in self.flipboard:
return True
else:
return False
# checks the diagonals for a possible win
def check_diags(self, last_row, last_column):
# check the symbol to be tested
mark = self.board[last_row][last_column]
# lists for the diagonals surrounding input point
temp1 = []
temp2 = []
# booleans for determining list direction
NE = True
NW = True
SW = True
SE = True
# checks for a SW win
for x in range(0,5):
new_row = last_row + x
new_column = last_column - x
SW = self.dirSW(new_row, new_column)
if board.board[new_row][new_column] == mark:
temp1.append("X")
if not SW:
break
# checks for a NE win
for x in range(0,5):
new_row = last_row - x
new_column = last_column + x
NE = self.dirNE(new_row, new_column)
if board.board[new_row][new_column] == mark and x != 0:
temp1.append("X")
if not NE:
break
# checks for a NW win
for x in range(0,5):
new_row = last_row - x
new_column = last_column - x
NW = self.dirNW(new_row, new_column)
if board.board[new_row][new_column] == mark:
temp2.append("X")
if not NW:
break
# checks for a SE win
for x in range(0,5):
new_row = last_row + x
new_column = last_column + x
SE = self.dirSE(new_row, new_column)
if board.board[new_row][new_column] == mark and x != 0:
temp2.append("X")
if not SE:
break
if "XXXXX" in "".join(temp1) or "XXXXX" in "".join(temp2):
return True
else:
return False
# takes a point as an input and decides which directions it could
# possibly have a win in
def dirNE(self, row, column):
if row == 0 or column ==8:
return False
else:
return True
def dirNW(self, row, column):
if row == 0 or column == 0:
return False
else:
return True
def dirSE(self, row, column):
if row == 8 or column == 8:
return False
else:
return True
def dirSW(self, row, column):
if row == 8 or column == 0:
return False
else:
return True
class Computer(object):
def __init__(self, symbol):
self.symbol = symbol
def take_turn(self, board):
while True:
row = randint(0,8)
column = randint(0,8)
if board.board[row][column] == '-':
return (row, column)
break
###################Main Body#########################
print "Welcome to five in a row!"
# determine whether the user wants x or o
while True:
print "X goes first."
print "Would you like to be X or O?"
symbol = raw_input("???").upper()
if symbol == "O":
comp_symbol = "X"
compturn = True
break
elif symbol == "X":
comp_symbol = "O"
compturn = False
break
else:
print "Please enter either X or O"
comp = Computer(comp_symbol)
board = Board()
board.printout()
# play the game
while True:
if compturn:
print "Here is the computer's move."
row, column = comp.take_turn(board)
board.board[row][column] = comp_symbol
compturn = False
else:
print "In which row would you like to place your %s?" % symbol
row = int(raw_input("???"))
print "In which column would you like to place your %s?" % symbol
column = int(raw_input("???"))
board.board[int(row)][int(column)] = "%s" % symbol
compturn = True
board.flip()
if board.has_player_won(row, column):
print "We have a win!"
board.printout()
break
for a in board.board:
if "-" not in a:
print "There are no spaces to play in left!"
board.printout()