-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTester.py
207 lines (174 loc) · 6.63 KB
/
Tester.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
# Connects Board and Solver to run Obstruction
import sys
import console as console
from Board import State, get_mapping_to_index
# Represents the participants of the game Obstruction
from Solver import Minimax, AlphaBetaPruning
# representation of the players that participate in the game
class Player:
def __init__(self, player: int, is_ai: bool):
self.player_num = player
self.player_id = assign_player_id(player)
self.is_ai = is_ai
# represents entire game and any state that will be coupled Player 1: MAX, 2: MIN
class Game:
def __init__(self, player1: Player, player2: Player, search_method):
self.player1 = player1
self.player2 = player2
self.board_state = State()
self.is_game_over = False
self.ai_search_method = search_method
self.total_nodes_expanded = 0
# assigns the character that player will use to occupy space on the game board, X is Max and O is Min
def assign_player_id(player: int):
if player == 1:
output = 'X'
elif player == 2:
output = 'O'
else:
console.error("Player ID was not assigned to X or O.")
return 'err'
return output
# Verify human input is correct
def verify_human_input(human_input: list, game: Game):
if len(human_input) > 2:
return False
check = ''
for i in human_input:
if int(i) > 5 or int(i) < 0:
return False
check += i
# x = game.board_state.mapping_to_index[check]
x = get_mapping_to_index()[check]
index = x.pop()
if index not in game.board_state.available_indexes:
return False
return True
# Conducts player move depending on if human or AI
def conduct_move(game: Game, player: Player):
# human move
if not player.is_ai:
# Get human player input
raw_input = input("Input format row/column ex: 3/4: ")
move_coordinates = raw_input.split('/')
correct_input = verify_human_input(move_coordinates, game)
while not correct_input:
raw_input = input("Input format row/column ex: 3/4: ")
move_coordinates = raw_input.split('/')
correct_input = verify_human_input(move_coordinates, game)
move_location = ''
for i in move_coordinates:
move_location += i
game.board_state.place_symbol_and_update_state(move_location, player.player_id)
game_over_flag = len(game.board_state.available_indexes)
if game_over_flag == 0:
return True
else:
return False
else:
if game.ai_search_method == 'MM':
# make algo object
plan_move = Minimax(game.board_state, player.player_num, 'MM')
game.board_state.place_symbol_and_update_state(plan_move.the_move_chosen, player.player_id)
game.total_nodes_expanded += plan_move.total_expanded
elif game.ai_search_method == 'AB':
plan_move = AlphaBetaPruning(game.board_state, player.player_num, 'AB')
game.board_state.place_symbol_and_update_state(plan_move.the_move_chosen, player.player_id)
game.total_nodes_expanded += plan_move.total_expanded
game_over_flag = len(game.board_state.available_indexes)
if game_over_flag == 0:
return True
else:
return False
def run():
if len(sys.argv) == 3:
if sys.argv[1].isdigit():
ai_player_id = int(sys.argv[1])
search_method = sys.argv[2]
else:
ai_player_id = None
search_method = None
print("Improper command line input")
exit(0)
else:
print("Need arguments or improper format.")
exit(0)
if ai_player_id == 1 or ai_player_id == 2 and search_method == "MM" or search_method == 'AB':
supported = True
else:
supported = False
if not supported:
print("Arguments currently not supported.")
exit(0)
else:
print("AI Player: ", ai_player_id, ", ", "Search: ", search_method)
if ai_player_id == 1:
print("AI is player 1")
player1 = Player(1, True)
player2 = Player(2, False)
else:
print("AI is player 2")
player1 = Player(1, False)
player2 = Player(2, True)
game = Game(player1, player2, search_method)
player1_won = False
while not game.is_game_over:
# First player move AKA MAX
print("Player 1's turn")
game.board_state.display_current_state()
game.is_game_over = conduct_move(game, player1)
game.board_state.display_current_state()
# check to see if player 1 has won the game
if game.is_game_over:
player1_won = True
continue
print("Player 2's turn")
# Second player move AKA MIN
game.is_game_over = conduct_move(game, player2)
game.board_state.display_current_state()
if player1_won:
winner = "Player 1 Wins!"
print(winner)
else:
winner = "Player 2 Wins!"
print(winner)
# print to Readme.txt
file = open("Readme.txt", "a")
file.writelines(["\n", winner, " \n", "Algo: ", game.ai_search_method,
"\n", "Expanded: ", str(game.total_nodes_expanded), "\n"])
file.close()
# debugging point of entry for the game
def debug_run():
search_method = 'AB'
player1 = Player(1, False) # False means not AI
player2 = Player(2, True)
# TODO code below will be in run() once the program is complete
game = Game(player1, player2, search_method)
player1_won = False
while not game.is_game_over:
# First player move AKA MAX
print("Player 1's turn")
game.board_state.display_current_state()
game.is_game_over = conduct_move(game, player1)
game.board_state.display_current_state()
# check to see if player 1 has won the game
if game.is_game_over:
player1_won = True
continue
print("Player 2's turn")
# Second player move AKA MIN
game.is_game_over = conduct_move(game, player2)
game.board_state.display_current_state()
if player1_won:
winner = "Player 1 Wins!"
print(winner)
else:
winner = "Player 2 Wins!"
print(winner)
# print to Readme.txt
file = open("Readme.txt", "a")
file.writelines(["\n", winner, " \n", "Algo: ", game.ai_search_method,
"\n", "Expanded: ", str(game.total_nodes_expanded), "\n"])
file.close()
run()
# debug_run()