-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrategy.py
209 lines (160 loc) · 6.96 KB
/
strategy.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
"""
A module for strategies.
"""
from typing import Any
import random
from classes import Tree, Stack
def interactive_strategy(game: 'Game') -> Any:
"""
Return a move for game through interactively asking the user for input.
"""
move = input("Enter a move: ")
return game.str_to_move(move)
def rough_outcome_strategy(game: 'Game') -> Any:
"""
Return a move for game by picking a move which results in a state with
the lowest rough_outcome() for the opponent.
NOTE: game.rough_outcome() should do the following:
- For a state that's over, it returns the score for the current
player of that state.
- For a state that's not over:
- If there is a move that results in the current player winning,
return 1.
- If all moves result in states where the other player can
immediately win, return -1.
- Otherwise; return a number between -1 and 1 corresponding to how
'likely' the current player will win from the current state.
In essence: rough_outcome() will only look 1 or 2 states ahead to
'guess' the outcome of the game, but no further. It's better than
random, but worse than minimax.
"""
current_state = game.current_state
best_move = None
best_outcome = -2 # Temporarily -- just so we can replace this easily later
# Get the move that results in the lowest rough_outcome for the opponent
for move in current_state.get_possible_moves():
new_state = current_state.make_move(move)
# We multiply the below by -1 since a state that's bad for the opponent
# is good for us.
guessed_score = new_state.rough_outcome() * -1
if guessed_score > best_outcome:
best_outcome = guessed_score
best_move = move
# Return the move that resulted in the best rough_outcome
return best_move
def iterative_minimax(game: 'Game') -> Any:
"""
Returns the best score available for a certain game using an iterative
approach.
@param game 'Game': the Game object that represents the game being played
@rtype: Any
"""
tree_list = []
tree_list.append(Tree((game.current_state, [], None, None)))
s = Stack()
s.add(tree_list[0])
a_lst = []
while not s.is_empty():
p = s.remove()
if (p.children == []) and p.value.get_possible_moves() != []:
moves = [p]
for value in p.value.get_possible_moves():
q = Tree((p.value.make_move(value), [], None, value))
p.children.append(q)
tree_list.append(q)
moves.append(q)
a_lst.append(moves)
for node in tree_list:
s.add(node)
elif (p.children == []) and p.value.get_possible_moves() == []:
old_state = game.current_state
game.current_state = p.value
if not game.is_winner('p1') and not game.is_winner('p2'):
p.score = 0
game.current_state = old_state
else:
p.score = -1
game.current_state = old_state
else:
lst_scores = []
for node in p.children:
lst_scores.append(node.score * -1)
p.score = max(lst_scores)
for outer_list in a_lst:
if game.current_state.__repr__() == outer_list[0].value.__repr__() \
and outer_list[0].children != []:
lst = []
for node in outer_list[0].children:
if node.value.get_possible_moves() == []:
lst.append(node.move_made)
if len(lst) == 1:
return lst[0]
elif len(lst) >= 1:
return random.choice(lst)
m = [(node.move_made, node.score) for node in outer_list[1:]]
lst = []
for item in m:
if item[1] == outer_list[0].score * -1:
lst.append(item[0])
return random.choice(lst)
return -1
def recursive_helper(t: 'Tree', game: 'Game') -> Any:
"""
A recursive minimax helper function that returns the best available move
that can be played by a player for a certain game.
@param t 'Tree': a Tree onject that is a representation of a specific state
within the game
@param game 'Game': a Game object that represents the current game being
played
@rtype: Any
"""
if t.value.get_possible_moves() == []:
old_state = game.current_state
game.current_state = t.value
if not game.is_winner('p1') and not game.is_winner('p2'):
t.score = 0
game.current_state = old_state
else:
t.score = -1
game.current_state = old_state
return t.score
elif t.value.get_possible_moves() != [] and \
t.value.__repr__() != game.current_state.__repr__():
for move in t.value.get_possible_moves():
q = Tree((t.value.make_move(move), [], None, move))
if q.value.get_possible_moves() == []:
q.score = recursive_helper(q, game)
else:
q.score = max([-1 * \
recursive_helper(Tree((q.value.make_move(x), \
[], None, x,)), game)\
for x in q.value.get_possible_moves()])
t.children.append(q)
t.score = max([x.score * -1 for x in t.children])
return t.score
else:
for value in game.current_state.get_possible_moves():
node = Tree((t.value.make_move(value), [], None, value))
t.children.append(node)
for node in t.children:
if node.value.get_possible_moves() == []:
return node.move_made
lst = []
t.score = max([-1 * recursive_helper(x, game) for x in t.children])
for node in t.children:
if node.score == t.score * -1:
lst.append(node.move_made)
return random.choice(lst)
def recursive_minimax(game: 'Game') -> Any:
"""
A recursive minimax implementation that returns the best score that the
a player can make for a certain game.
@param game 'Game': a Game object that represents the current game being
played
@rtype: Any
"""
t = Tree((game.current_state, [], None, None))
return recursive_helper(t, game)
if __name__ == "__main__":
from python_ta import check_all
check_all(config="a2_pyta.txt")