generated from Ctree35/Project-COMP424-2022-Winter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
random_agent.py
51 lines (43 loc) · 1.43 KB
/
random_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
49
50
51
import numpy as np
from copy import deepcopy
from agents.agent import Agent
from store import register_agent
# Important: you should register your agent with a name
@register_agent("random_agent")
class RandomAgent(Agent):
"""
Example of an agent which takes random decisions
"""
def __init__(self):
super(RandomAgent, self).__init__()
self.name = "RandomAgent"
self.autoplay = True
def step(self, chess_board, my_pos, adv_pos, max_step):
# Moves (Up, Right, Down, Left)
ori_pos = deepcopy(my_pos)
moves = ((-1, 0), (0, 1), (1, 0), (0, -1))
steps = np.random.randint(0, max_step + 1)
# Random Walk
for _ in range(steps):
r, c = my_pos
dir = np.random.randint(0, 4)
m_r, m_c = moves[dir]
my_pos = (r + m_r, c + m_c)
# Special Case enclosed by Adversary
k = 0
while chess_board[r, c, dir] or my_pos == adv_pos:
k += 1
if k > 300:
break
dir = np.random.randint(0, 4)
m_r, m_c = moves[dir]
my_pos = (r + m_r, c + m_c)
if k > 300:
my_pos = ori_pos
break
# Put Barrier
dir = np.random.randint(0, 4)
r, c = my_pos
while chess_board[r, c, dir]:
dir = np.random.randint(0, 4)
return my_pos, dir