-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·65 lines (56 loc) · 1.93 KB
/
main.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
#!/usr/bin/env python3
import random
from GameState import GameState
from Node import Node
from Move import Move
DEPTH = 5
player_color = None
while player_color is None:
try:
player_color_str = input("Play as light or dark? ")[0].lower()
if player_color_str == "b" or player_color_str == "d":
player_color = 1
elif player_color_str == "w" or player_color_str == "l":
player_color = -1
else:
raise RuntimeError
except (IndexError, RuntimeError):
print('Invalid color, please enter "dark" or "light"')
game_state = GameState()
print(game_state)
print()
while True:
if not game_state.get_possible_moves():
print("Game over. Score: " + str(game_state.get_score()))
break
if game_state.to_move == player_color:
player_move = None
possible_moves = game_state.get_possible_moves()
if len(possible_moves) == 1 and next(iter(possible_moves)).is_pass():
player_move = next(iter(possible_moves))
print("The only possible move is a pass.")
else:
while player_move is None:
#player_move = random.choice(list(possible_moves))
#print("Randomly selected move: " + player_move.get_algebraic_string())
player_move_str = input("Your move? ").lower()
try:
potential_move = Move.from_algebraic_string(player_color, player_move_str)
except ValueError:
print("Invalid move string.")
else:
if potential_move not in possible_moves:
print("That move is not possible.")
else:
player_move = potential_move
game_state = game_state.move(player_move)
else:
node = Node(game_state)
node.evaluate(DEPTH)
print("Current value: " + str(node.value))
best_move = node.best_child.prev_move
print("Computer move: " + best_move.get_algebraic_string())
game_state = node.best_child.game_state
print(game_state)
print("Current score: " + str(game_state.get_score()))
print()