-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReversi.py
619 lines (537 loc) · 24.9 KB
/
Reversi.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
## This is the code for the Reversi game
## One player can play this against the computer
## For more information about Reversi,
## see https://en.wikipedia.org/wiki/Reversi
import random
import datetime
import re
## Function to store the player's name
def ask_name():
print(40*"-", "\nWelcome to Reversi!\n", 40*"-",sep="")
name = input("Please type your name: ")
print("Hello ", name, "! Let's play a game!\n", sep="")
return name
## Class for bricks
class Brick():
def __init__(self, row, col, state):
self.row = row
self.col = col
self.state = state # either "x", "o" or " "
def change_state(self, new_state):
while True:
try:
self.state = new_state
if new_state not in ["x", "o", " "]:
raise ValueError
except (ValueError):
print("The new state is not valid.")
else:
return self.state
## Defining the size of the board
def choose_size():
print("How big is the board supposed to be?\n\
Please choose an even number between 4 and 14: ")
# check if the number is valid
while True:
try:
size = int(input())
if size < 4 or size > 14 or size%2 != 0:
raise ValueError
except (ValueError):
print("That's not a valid number. Please choose an even number between 6 and 40: ")
else:
return size
## Creating a list with all bricks according to the board size
def create_list(size):
# storing all bricks in a list
bricks = []
for i in range(1, size+1):
for j in range(1,size+1):
# assign bricks to Brick class
brick = Brick(i, j, " ")
bricks.append(brick)
return bricks
## Let the player choose his colour
def choose_colour():
print("Do you want to be x or o?")
while True:
try:
players_colour = input()
if players_colour == "x":
#set the colour of the opponent
PC_colour = "o"
else:
PC_colour = "x"
if players_colour not in ["x","o"]:
raise ValueError
except (ValueError):
print("That's not a valid option. Please type either 'x' or 'o'.")
else:
return players_colour, PC_colour
## Print gameboard
def print_board(bricks, size):
print("\n ", end="")
# ______1___2___3___4...
for i in range(1, size+1):
print("{:^4}".format(i), end="")
# ____+---+---+---+---+...
print("\n ","+---"*size, "+\n", sep="", end="")
for i in range (size): #i = row
print("{:<3}".format(i+1), "|", sep="", end="") # 1 |
for brick in bricks:
if brick.row == i+1:
print("{:^3}".format(brick.state), "|", sep="", end="")
print("\n ", "+---"*size, "+", sep="")
## Starting position of the first four bricks in the middle of the board
def start_position(bricks, size):
# setting the state of the initial bricks
for brick in bricks:
if (brick.row == size/2 and brick.col == size/2)\
or (brick.row == size/2+1 and brick.col == size/2+1):
brick.change_state("x")
if (brick.row == size/2 and brick.col == size/2+1)\
or (brick.row == size/2+1 and brick.col == size/2):
brick.change_state("o")
## Players turn
def players_turn(bricks, players_colour, PC_colour, size, name):
print("Please enter the number of row and column you want to play, seperated by a comma.\n\
If you need some help, type 'hint'. ")
available_bricks = []
for brick in bricks:
if brick.state == " ":
available_bricks.append(brick)
while True:
try:
choice = input()
# check if choice have to be in the format <digit><,><digit>
pattern = r"\d,\d"
# give the player a hint, if he asks for it
if choice == "hint":
give_hint(bricks, players_colour, PC_colour, size)
# continue if the input is according to the pattern
elif re.match(pattern, choice):
pass
else:
print("You have to provide the information in the format <row>,<column>")
raise ValueError
row = int(choice.split(",")[0])
col = int(choice.split(",")[1])
for brick in bricks:
# brick can't already belong to one colour / can't already have a state
if brick.row == row and brick.col == col:
if brick not in available_bricks:
print("This brick is not available anymore.")
raise ValueError
# 2. rule: brick has to touch another one of the oppponents colour:
if first_rule(bricks, row, col, PC_colour) == False:
print("This brick doesn't touch another one of your opponent's colour.")
raise ValueError
# 2. rule: there has to be another brick of the players colour in the same line
elif second_rule(bricks, row, col, players_colour, PC_colour, size) == False:
print("The brick has to be in direct line with one of your bricks.")
raise ValueError
except ValueError:
if choice == "hint":
print("Then let's go!")
else:
print("Try again!")
else:
#change state of the bricks according to the played move
bricks_to_flip = second_rule(bricks, row, col, players_colour, PC_colour, size)
for brick in bricks_to_flip:
brick.change_state(players_colour)
#change colour of the played brick
for brick in bricks:
if brick.col == col and brick.row == row:
brick.change_state(players_colour)
break
print_board(bricks, size)
if check_game_finished(bricks, PC_colour, players_colour, players_colour, PC_colour, size, name) == "Skip":
print("There is no possible move for the computer. It's your turn again.\n")
players_turn(bricks, players_colour, PC_colour, size, name)
else:
input("Press Enter to let the computer play")
computers_turn(bricks, PC_colour, players_colour, size, name)
def computers_turn(bricks, PC_colour, players_colour, size, name):
print("\nNow your big opponent, the COMPUTER plays!\n")
#create list with all available bricks
available_bricks = []
for brick in bricks:
if brick.state == " ":
available_bricks.append(brick)
while True:
try:
if len(available_bricks)==0:
print("There are no moves left")
check_game_finished(bricks, players_colour, PC_colour, players_colour, PC_colour, size, name)
break
# else let the computer play one of the possible moves
# find out which move is possible by testing each of the available bricks
choosen_brick = random.choice(available_bricks)
available_bricks.remove(choosen_brick)
#for each move the first and the second rule has to be fulfilled, otherwise raise ValueError
if first_rule(bricks, choosen_brick.row, choosen_brick.col, players_colour) == False:
raise ValueError
elif second_rule(bricks, choosen_brick.row, choosen_brick.col, PC_colour, players_colour, size) == False:
raise ValueError
except ValueError:
pass
else:
bricks_to_flip = second_rule(bricks, choosen_brick.row, choosen_brick.col, PC_colour, players_colour, size)
for brick in bricks_to_flip:
brick.change_state(PC_colour)
for brick in bricks:
if brick.col == choosen_brick.col and brick.row==choosen_brick.row:
brick.change_state(PC_colour)
print("The computer played the brick", choosen_brick.row, "|", choosen_brick.col)
break
print_board(bricks, size)
if check_game_finished(bricks, players_colour, PC_colour, players_colour, PC_colour, size, name) == "Skip":
print("There is no possible move for you. It's the computer's turn again.")
input("Press Enter to let the computer play")
computers_turn(bricks, PC_colour, players_colour, size, name)
else:
players_turn(bricks, players_colour, PC_colour, size, name)
## First rule: Check if the brick touches another brick of the opponent's colour
def first_rule(bricks, row, col, opp_colour):
# create a list directions with the directions of the touched bricks (not of the new brick!)
# diagional/vertical left/right up/down
directions = []
for brick in bricks:
if brick.state == opp_colour:
if brick.row == row-1:
if brick.col == col-1:
directions.append("diagonal left up")
if brick.col == col:
directions.append("vertical up")
if brick.col == col+1:
directions.append("diagonal right up")
if brick.row == row:
if brick.col == col-1:
directions.append("horizontal left")
if brick.col == col+1:
directions.append("horizontal right")
if brick.row == row+1:
if brick.col == col-1:
directions.append("diagonal left down")
if brick.col == col:
directions.append("vertical down")
if brick.col == col+1:
directions.append("diagonal right down")
if len(directions) == 0:
return False #brick doesn't touch a brick of the opponent's colour
else:
return directions
## List with temporary bricks to flip (needed for the second rule)
def create_temp(direction, directions, brick, temp_bricks, colour):
done = False
temp_bricks.append(brick)
# count the own bricks of the player's colour
own_bricks = 0
for brick in temp_bricks:
if brick.state == colour:
own_bricks += 1
# check if there is an empty brick before the next brick of the player's colour
if brick.state == " " and own_bricks == 0: #invalid move
directions.remove(direction)
done = True
return done, temp_bricks
## Second rule: Find another brick of the player's colour in the same direction as of the touched brick
## function returns a list with the bricks that have to be flipped
def second_rule(bricks, row, col, colour, opp_colour, size):
directions = first_rule(bricks, row, col, opp_colour)
bricks_to_flip = []
temp_bricks = []
#check for every directions if there are bricks to flip and if so add them to bricks_to_flip list
if "diagonal left up" in directions:
done = False
for brick in reversed(bricks):
# add all the bricks in the according direction to the list bricks_to_flip
# and break if an empty brick is found
for i in range(row-1,1,-1):
for j in range(col-1,1,-1):
if brick.row == i and brick.col == j\
and row - brick.row == col - brick.col:
# add brick to the list of temporary bricks to flip
done, temp_bricks = create_temp("diagonal left up", directions, brick, temp_bricks, colour)
if done == True:
break
# check if a brick of the player's colour is found in the same direction and if so break the loop
if brick.row < row and brick.col < col \
and row - brick.row == col - brick.col \
and brick.state == colour: #valid move
bricks_to_flip.extend(temp_bricks)
done = True
break
# check if end of board is reached and still there is no other brick of the player's colour
elif ((brick.row == 1 and brick.col == col - row + 1)\
or (brick.col == 1 and brick.row == row - col + 1))\
and brick.state !=colour: #invalid move
directions.remove("diagonal left up")
done = True
break
if done == True:
break
temp_bricks = [] #empty temporary list
if "vertical up" in directions:
done = False
for brick in reversed(bricks):
for i in range(row-1,1,-1):
if brick.row ==i and brick.col == col:
done, temp_bricks = create_temp("vertical up", directions, brick, temp_bricks, colour)
if done == True:
break
if brick.row < row and brick.col == col and brick.state == colour: #valid move
bricks_to_flip.extend(temp_bricks)
done = True
break
elif brick.row == 1 and brick.col == col and brick.state !=colour: #invalid move
directions.remove("vertical up")
done = True
break
if done == True:
break
temp_bricks = [] #empty temporary list
if "diagonal right up" in directions:
done = False
for brick in reversed(bricks):
for i in range(row-1,1,-1):
for j in range(col+1,size):
if brick.row == i and brick.col == j\
and row - brick.row == brick.col - col:
done, temp_bricks = create_temp("diagonal right up", directions, brick, temp_bricks, colour)
if done == True:
break
if brick.row < row and brick.col > col\
and row - brick.row == brick.col - col \
and brick.state == colour: #valid move
bricks_to_flip.extend(temp_bricks)
done = True
break
elif ((brick.row == 1 and brick.col == row + col - 1)\
or (brick.col == size and brick.row == row + col - size))\
and brick.state !=colour: #invalid move
directions.remove("diagonal right up")
done = True
break
if done == True:
break
temp_bricks = [] #empty temporary list
if "horizontal left" in directions:
done = False
for brick in reversed(bricks):
for i in range(col-1,1,-1):
if brick.row ==row and brick.col == i:
done, temp_bricks = create_temp("horizontal left", directions, brick, temp_bricks, colour)
if done == True:
break
if brick.row == row and brick.col < col and brick.state == colour: #valid move
bricks_to_flip.extend(temp_bricks)
done = True
break
elif brick.row == row and brick. col == 1 and brick.state != colour: #invalid move
directions.remove("horizontal left")
done = True
break
if done == True:
break
temp_bricks = [] #empty temporary list
if "horizontal right" in directions:
done = False
for brick in bricks:
for i in range(col+1,size):
if brick.row ==row and brick.col == i:
done, temp_bricks = create_temp("horizontal right", directions, brick, temp_bricks, colour)
if done == True:
break
if brick.row == row and brick.col > col and brick.state == colour: #valid move
bricks_to_flip.extend(temp_bricks)
done = True
break
elif brick.row == row and brick.col == size and brick.state != colour: #invalid move
directions.remove("horizontal right")
done = True
break
if done == True:
break
temp_bricks = [] #empty temporary list
if "diagonal left down" in directions:
done = False
for brick in bricks:
for i in range(row+1,size):
for j in range(col-1,1,-1):
if brick.row == i and brick.col == j\
and brick.row - row == col - brick.col:
done, temp_bricks = create_temp("diagonal left down", directions, brick, temp_bricks, colour)
if done == True:
break
if done == True:
break
if brick.row > row and brick.col < col\
and brick.row - row == col - brick.col\
and brick.state == colour: #valid move
bricks_to_flip.extend(temp_bricks)
done = True
break
elif ((brick.row == size and brick.col == row + col - size)\
or (brick.col == 1 and brick.row == row + col - 1))\
and brick.state !=colour: #invalid move
directions.remove("diagonal left down")
done = True
break
if done == True:
break
temp_bricks = [] #empty temporary list
if "vertical down" in directions:
done = False
for brick in bricks:
for i in range(row+1,size):
if brick.row ==i and brick.col == col:
done, temp_bricks = create_temp("vertical down", directions, brick, temp_bricks, colour)
if done == True:
break
if brick.state == colour and brick.col == col and brick.row > row: #valid move
bricks_to_flip.extend(temp_bricks)
done = True
break
elif (brick.row == size and brick.col == col) and brick.state !=colour: #invalid move
directions.remove("vertical down")
done = True
break
if done == True:
break
temp_bricks = [] #empty temporary list
if "diagonal right down" in directions:
done = False
for brick in bricks:
for i in range(row+1,size):
for j in range(col+1,size):
if brick.row == i and brick.col == j\
and brick.row - row == brick.col - col:
done, temp_bricks = create_temp("diagonal right down", directions, brick, temp_bricks, colour)
if done == True:
break
if done == True:
break
if brick.row > row and brick.col > col\
and brick.row - row == brick.col - col\
and brick.state == colour: #valid move
bricks_to_flip.extend(temp_bricks)
done = True
break
elif ((brick.row == size and brick.col == col - row + size)\
or (brick.col == size and brick.row == row - col + size))\
and brick.state !=colour: #invalid move
directions.remove("diagonal right down")
done = True
break
if done == True:
break
temp_bricks = [] #empty temporary list
if len(directions)==0:
return False
else:
return bricks_to_flip
## function for checking the points of the computer and the player
def check_points(bricks, players_colour, PC_colour):
players_points = 0
computers_points = 0
for brick in bricks:
if brick.state == players_colour:
players_points +=1
elif brick.state == PC_colour:
computers_points +=1
print("PC points:", computers_points, " \
Your points:", players_points)
return players_points, computers_points
## check if there are more possible moves
## if not the game is finished
## if only not for the current player, then he has to skip the turn
def check_game_finished(bricks, colour, opp_colour, players_colour, PC_colour, size, name):
players_points, computers_points = check_points(bricks, players_colour, PC_colour)
available_bricks = []
for brick in bricks:
if brick.state == " ":
available_bricks.append(brick)
# check if there are anymore moves possible for either of the player
possible_moves = 0 #possible moves for the current player
opp_possible_moves = 0 #possible moves for the other player
for brick in available_bricks:
if first_rule(bricks, brick.row, brick.col, opp_colour) != False:
if second_rule(bricks, brick.row, brick.col, colour, opp_colour, size)!= False:
possible_moves +=1 # possible moves for the current players
if first_rule(bricks, brick.row, brick.col, colour) != False:
if second_rule(bricks, brick.row, brick.col, opp_colour, colour, size)!= False:
opp_possible_moves +=1 # possible moves for the other player
if possible_moves == 0 and opp_possible_moves == 0:
print("The game is finished")
if players_points > computers_points:
print("Congratulations, you won!")
write_file(players_points, name)
elif computers_points > players_points:
print("Loser! The computer beat you!")
elif players_points == computers_points:
print("Draw!")
while True:
try:
choice = input("You wanna play again? Answer 'yes' or 'no'\n")
if choice in ("yes", "Yes", "y", "Y"):
start_game(name)
elif choice in ("no", "No", "n", "N"):
print("Goodbye and have a nice day")
quit()
elif choice not in ("yes", "Yes", "y", "Y", "no", "No", "n", "N"):
raise ValueError
except ValueError:
print("What do you want?")
else:
break
# if the current player can't play, but there are possible moves for the opponent
elif possible_moves == 0 and opp_possible_moves > 0:
return "Skip"
## Write new highscore to a textfile
def write_file(highscore, name):
try:
with open("Highscore.txt", "r") as text:
content = text.readlines()
old_highscore = 0
for i, word in enumerate(content[-1].split()):
if word == "scored":
old_highscore= content[-1].split()[i+1]
except IOError:
old_highscore = 0
if highscore > int(old_highscore):
print("Even more awesome, you reached a new highscore!")
timestamp = (datetime.datetime.now().strftime("%d %b %Y, %H:%M"))
new_entry = [50*"*" + "\nReversi Highscore\n" + 50*"*" + \
str("\n" + name + " scored " + str(highscore) + " points " + timestamp)]
with open("Highscore.txt","w") as text:
text.writelines(new_entry)
## Give hint of possible moves for the player
def give_hint(bricks, colour, opp_colour, size):
available_bricks = []
for brick in bricks:
if brick.state == " ":
available_bricks.append(brick)
possible_moves = []
for brick in available_bricks:
if first_rule(bricks, brick.row, brick.col, opp_colour) != False:
if second_rule(bricks, brick.row, brick.col, colour, opp_colour, size)!= False:
possible_moves.append(brick)
no_possible_moves = len(possible_moves)
#1. Hint
print("There are", no_possible_moves, "possible bricks you could choose.")
hint = random.choice(possible_moves)
print("Try row" , hint.row)
choice = input("If you need another hint, answer 'yes' or else type 'no'\n")
if choice in ("yes", "Yes", "y", "Y"):
print("Check out column:" , hint.col)
def start_game(name):
size = choose_size()
bricks = create_list(size)
players_colour, PC_colour = choose_colour()
start_position(bricks, size)
print_board(bricks, size)
players_turn(bricks, players_colour, PC_colour, size, name)
name = ask_name()
start_game(name)