-
Notifications
You must be signed in to change notification settings - Fork 36
/
base_agent.py
executable file
·48 lines (36 loc) · 1.27 KB
/
base_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
43
44
45
46
47
48
#!/usr/bin/env python
import random
from gym_tictactoe.env import TicTacToeEnv, agent_by_mark, check_game_status,\
after_action_state, tomark, next_mark
class BaseAgent(object):
def __init__(self, mark):
self.mark = mark
def act(self, state, ava_actions):
for action in ava_actions:
nstate = after_action_state(state, action)
gstatus = check_game_status(nstate[0])
if gstatus > 0:
if tomark(gstatus) == self.mark:
return action
return random.choice(ava_actions)
def play(max_episode=10):
start_mark = 'O'
env = TicTacToeEnv()
agents = [BaseAgent('O'),
BaseAgent('X')]
for _ in range(max_episode):
env.set_start_mark(start_mark)
state = env.reset()
while not env.done:
_, mark = state
env.show_turn(True, mark)
agent = agent_by_mark(agents, mark)
ava_actions = env.available_actions()
action = agent.act(state, ava_actions)
state, reward, done, info = env.step(action)
env.render()
env.show_result(True, mark, reward)
# rotate start
start_mark = next_mark(start_mark)
if __name__ == '__main__':
play()