-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
282 lines (189 loc) · 6.34 KB
/
game.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# game.py
# Two Player Game
# Emerson Grabke
# Programmed using Python 3.3.2
def play():
'''(None) -> None
Initiate two-player game'''
global grid_size
global grid
global grid_length
global current_player
global watching_player
false_grid_size = True
while false_grid_size:
grid_size = input('Please input grid size: ')
if grid_size in ['4', '9', '16']:
grid_size = int(grid_size)
false_grid_size = False
print('You have chosen a {} by {} grid'.format(
int(grid_size), int(grid_size)))
else:
print('You have entered an invalid grid size! Size must be any of 4, 9 or 16')
grid = [[0]*grid_size for element in [0]*grid_size]
grid_length = int(grid_size**0.5)
show()
current_player = 'B'
watching_player = 'A'
win_value = 0
while win_value == 0:
current_player, watching_player = watching_player, current_player
move_return = 's'
while move_return in ['s', 'l']:
error = 1
while(error):
try:
move_return = move()
error = 0
except:
print('Move not valid. Please enter move as: row,column,value')
error = 1
if move_return == 'q':
print('Goodbye')
return
show()
win_value = check_win()
if win_value == 1:
print('Congratulations Player ' + current_player + '. You have won!')
else:
print('The matrix is full. It is a tie.')
def move():
'''(None) -> str
Begins move sequence.
Return one character string based on the move input.
This string is to be interpreted in play()'''
global grid
global current_player
move_not_legal = True
while move_not_legal:
raw_move = input('Player ' + current_player + ' enter a move: ')
if raw_move == 's':
save()
return 's'
elif raw_move == 'l':
load()
return 'l'
elif raw_move == 'q':
return 'q'
row, column, value = raw_move.split(',')
row, column, value = (
int(row.strip()), int(column.strip()), int(value.strip()))
move_not_legal = not check_move(row, column, value)
if move_not_legal:
print('You have entered an invalid move. Try again')
grid[row][column] = value
return 'y'
def check_move(row, col, value):
'''(int, int, int) -> boolean
Checks if a move is legal or not, where the move takes the form of:
Row# (row), Column# (col), and value (value).
Return True if move is legal, False if not.'''
global grid
global grid_size
global grid_length
if (row >= grid_size) or (col >= grid_size):
return False # Checks for range
elif (grid[row][col] != 0) or (value in grid[row]) or (
value not in range(1, grid_size+1)):
return False # Checks for row and value specified
row_cluster = row//(grid_length)
col_cluster = col//(grid_length)
for row_element in range(grid_length):
for col_element in range(grid_length):
if value == grid[row_cluster*grid_length + row_element][(
col_cluster*grid_length + col_element)]:
return False # Checks cluster
for row_number in range(grid_size):
if value == grid[row_number][col]:
return False # Checks column
return True
def check_win():
'''(None) -> int
Decides on the status of the game.
Return 0 if game can continue.
Return 1 if a player wins.
Return 2 if game concludes in a tie.'''
global grid
global grid_size
end_counter = 0
for row in range(grid_size):
if 0 not in grid[row]:
end_counter += 1
for column in range(grid_size):
for value in range(1, grid_size+1):
if check_move(row, column, value):
return 0
if end_counter == grid_size:
return 2 # Tie game case
return 1
def save():
'''(None) -> None
Saves the game to an external file.'''
global grid
global grid_size
write_file = input('Please input a file to save to: ')
file_object = open(write_file, 'w')
string_grid = ''
for row in range(grid_size):
for column in range(grid_size):
string_grid += str(grid[row][column]) + ','
string_grid = string_grid.strip(',') + '\n'
string_grid.strip()
file_object.write(string_grid)
file_object.close()
print('Game Saved')
return
def load():
'''(None) -> None
Loads the game from an external file.'''
global grid
global grid_size
global grid_length
global current_player
global watching_player
read_file = input('Please input a file to load from: ')
file_object = open(read_file)
grid_size = 0
grid = []
for line in file_object:
grid_size += 1
current_line = line.strip().split(',')
grid.append(current_line)
turn_count = 0
for row in range(grid_size):
for column in range(grid_size):
grid[row][column] = int(grid[row][column])
if grid[row][column] == 0:
turn_count += 1
turns_elapsed = grid_size**2 - turn_count
if turns_elapsed//2 == turns_elapsed/2:
current_player = 'A'
watching_player = 'B'
else:
current_player = 'B'
watching_player = 'A'
grid_length = int(grid_size**0.5)
print('Game Loaded')
show()
file_object.close()
return
def show():
global grid
global grid_size
global grid_length
grid_string = ''
for row in range(grid_size):
for column in range(grid_size):
if (column != 0) and ((column+1)//grid_length == (
(column+1)/grid_length) and (column != grid_size-1)):
grid_string += str(grid[row][column]) + '|'
else:
grid_string += str(grid[row][column]) + ' '
grid_string += '\n'
if (row != 0) and ((row+1)//grid_length == ((
row+1)/grid_length) and (row != grid_size-1)):
grid_string += str('-'*2*grid_size) + '\n'
grid_string += 'l: load, s: save, q: quit'
print(grid_string)
if __name__ == '__main__':
play()