generated from Ctree35/Project-COMP424-2022-Winter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
student_agent.py
79 lines (68 loc) · 2.37 KB
/
student_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
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
# Student agent: Add your own agent here
from agents.agent import Agent
from store import register_agent
import sys
from agents.MCNode import MCNode
from agents.MCTree import MCTree
import numpy as np
from copy import deepcopy
@register_agent("student_agent")
class StudentAgent(Agent):
"""
A dummy class for your implementation. Feel free to use this class to
add any helper functionalities needed for your agent.
"""
def __init__(self):
super(StudentAgent, self).__init__()
self.name = "StudentAgent"
self.autoplay = True
#self.step_number = 0
self.tree = None
self.node = None
self.dir_map = {
"u": 0,
"r": 1,
"d": 2,
"l": 3,
}
def step(self, chess_board, my_pos, adv_pos, max_step):
"""
Implement the step function of your agent here.
You can use the following variables to access the chess board:
- chess_board: a numpy array of shape (x_max, y_max, 4)
- my_pos: a tuple of (x, y)
- adv_pos: a tuple of (x, y)
- max_step: an integer
You should return a tuple of ((x, y), dir),
where (x, y) is the next position of your agent and dir is the direction of the wall
you want to put on.
Please check the sample implementation in agents/random_agent.py or agents/human_agent.py for more details.
"""
# 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