This repository has been archived by the owner on Sep 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax_agent.py
42 lines (34 loc) · 1.42 KB
/
max_agent.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
import random
from typing import Optional
import othello
from log_referee import LogReferee
import evaluation
class MaxAgent(othello.Agent):
def __init__(self, play_as: othello.Player, eval_func=evaluation.heuristic_eval_comprehensive) -> None:
super().__init__()
self.play_as = play_as
self.evaluation_function = lambda state: eval_func(state, self.play_as)
def play(self, state: othello.State) -> Optional[othello.Action]:
legal_actions = list(state.get_legal_actions(self.play_as))
if legal_actions == []:
return None
else:
# best_score = 0
# best_action = legal_actions[0]
# for action in legal_actions:
# next_state = state.perform_action(self.play_as, action)
# if next_state.get_score(self.play_as) > best_score:
# best_action = action
option = []
for action in legal_actions:
next_state = state.perform_action(self.play_as, action)
score = self.evaluation_function(next_state)
option.append((action, score))
best_action = max(option ,key=lambda item:item[1])[0]
return best_action
def run_max_agents() -> None:
referee = LogReferee(MaxAgent(othello.Player.DARK),
MaxAgent(othello.Player.LIGHT))
referee.run()
if __name__ == '__main__':
run_max_agents()