-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.py
executable file
·126 lines (112 loc) · 4.46 KB
/
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
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
from PySide.QtGui import *
import random
from astar import AStar
from utils import neighbours
class Agent():
def __init__(self, position, qp, parent, is_scout=False):
(self.x, self.y) = position
self.step_num = 1
self.path = [position]
self.is_scout = is_scout
self.parent = parent
self.color = QColor(0, 96, 109, 255) if self.is_scout else QColor(0, 168, 109, 255)
self.draw_path = False
self.bomb_location = False
self.path_to_bomb = []
self.exchanges = 0
def move(self):
self.step_num += 1
# if bomb in the neigbourhood
if self.parent.target in neighbours((self.x, self.y), True):
# The agent found the bomb
self.bomb_location = self.parent.target
if self.is_scout:
# bomb deactivated
self.parent.bomb_found = True
return True
else:
self.color = QColor(200, 96, 109, 255)
# the path is being reinitialized in
# order to be show to the scout
self.bomb_location = self.parent.target
# if agent is scout and scout knows the bomb
if self.bomb_location and self.is_scout:
if self.path_to_bomb:
(self.x, self.y) = self.path_to_bomb.pop()
return True
# create a shortcut
target = self.bomb_location
known_region = self.path
start = (self.x, self.y)
print 'calling astar'
shortest_path = AStar()
print 'will find the shortest path'
self.path_to_bomb = shortest_path.find_path(start, target, known_region)
return True
# select to move randomly avoiding
# to go to the last position
order_list = ['u', 'r', 'd', 'l']
random.shuffle(order_list)
# get the available moves
moves = []
for order in order_list:
if order == 'u':
moves = self.up(moves)
elif order == 'r':
moves = self.right(moves)
elif order == 'd':
moves = self.down(moves)
elif order == 'l':
moves = self.left(moves)
# if moves available
if len(moves) != 0:
# check if agents meet each other
for agent in self.parent.agents:
if (agent.x, agent.y) in neighbours((self.x, self.y)):
self.exchanges += 1
agent.path += self.path
self.path += agent.path
self.path = list(set(self.path))
agent.path = list(set(agent.path))
if (agent.is_scout) and (self.bomb_location) and (not agent.bomb_location):
agent.bomb_location = self.bomb_location
return True
else:
if self.bomb_location and not agent.bomb_location:
agent.bomb_location = self.bomb_location
agent.color = QColor(200, 96, 109, 255)
for m in moves:
# The agent prefers to go to a
# square he has not visited
if m in self.path:
#we put this move at the end
moves.remove(m)
moves.append(m)
(self.x, self.y) = moves[0]
if moves[0] in self.path:
self.path.remove((self.x, self.y))
self.path.append((self.x, self.y))
else:
print 'cannot move'
return False
return True
def up(self, moves):
# try step up
if self.y > 0 and not self.parent.is_wall(self.x, self.y - 1):
moves.append((self.x, self.y - 1))
return moves
def right(self, moves):
# try step right
if self.x < self.parent.maze_width - 1 and not self.parent.is_wall(self.x + 1, self.y):
moves.append((self.x + 1, self.y))
return moves
def down(self, moves):
# try step down
if self.y < self.parent.maze_height - 1 and not self.parent.is_wall(self.x, self.y + 1):
moves.append((self.x, self.y + 1))
return moves
def left(self, moves):
# try step left
if self.x > 0 and not self.parent.is_wall(self.x - 1, self.y):
moves.append((self.x - 1, self.y))
return moves