-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.py
137 lines (106 loc) · 3.58 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
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
"""Checkers implementation in Python, with AI."""
import time
from ai import best_move
from model import Configuration, Player
from ui import GUI, TUI
def main():
max_depth = None
while max_depth is None:
print("Choose difficulty")
print("\t1 = Easy")
print("\t2 = Medium")
print("\t3 = Hard")
difficulty = int(input())
if difficulty == 1:
max_depth = 3
elif difficulty == 2:
max_depth = 4
elif difficulty == 3:
max_depth = 5
else:
print("Invalid difficulty")
prune = None
while prune is None:
print("Choose algorithm:")
print("\t1 = minimax")
print("\t2 = minimax with alpha-beta pruning")
algorithm_choice = int(input())
if algorithm_choice == 1:
prune = False
elif algorithm_choice == 2:
prune = True
else:
print("Invalid algorithm")
human_player = None
while human_player is None:
print("Choose player:")
print("\t1 = Black")
print("\t2 = White")
player_choice = int(input())
if player_choice == 1:
human_player = Player.BLACK
elif player_choice == 2:
human_player = Player.WHITE
else:
print("Invalid player")
computer_player = human_player.opposite()
print("Human plays as", human_player)
print("Computer AI plays as", computer_player)
ui = None
while ui is None:
print("Choose user interface type:")
print("1 = Text")
print("2 = Graphical")
ui_choice = int(input())
if ui_choice == 1:
ui = TUI()
elif ui_choice == 2:
ui = GUI()
else:
print("Invalid user interface")
winner = None
current_config = Configuration.initial()
current_player = Player.BLACK
move_count = 0
game_start_time = time.time()
while True:
move_count += 1
print("Black's score:", current_config.score())
ui.render(current_config)
if not current_config.has_pieces(computer_player):
# the other player won
winner = human_player
break
if not current_config.has_pieces(human_player):
# the other player won
winner = computer_player
break
possible_moves = current_config.possible_moves(current_player)
if not possible_moves:
# leave winner as `None`, indicating a draw
break
if current_player == human_player:
move = ui.get_next_move(current_config, current_player, possible_moves)
# provide an option to quit the game
if move is None:
break
current_config = current_config.apply_move(move)
else:
print("Computer is thinking...")
start_time = time.perf_counter()
current_config = best_move(current_config, computer_player, max_depth, prune)
end_time = time.perf_counter()
thinking_time = end_time - start_time
print(f"Computer spent {thinking_time:.2f} seconds thinking")
if not current_config.capture_chain:
current_player = current_player.opposite()
game_end_time = time.time()
game_duration = game_end_time - game_start_time
if winner is None:
print("The game is a draw")
else:
print("Winner:", winner)
print(f"Game finished after {move_count} moves")
print(f"Took {int(game_duration)} seconds")
if __name__ == '__main__':
main()