This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathia.py
98 lines (89 loc) · 3.25 KB
/
ia.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
from structure.client import *
from structure.utils import Coordinate
class IA(Client):
"""
The Class of the AI
"""
def __init__(self, host: str, port: int):
super().__init__(host, port)
self.turn = 0
self.bikers = None
self.biker = None
def play(self) -> bool:
"""
Method to start the game and initialize bikers , deliveries and such
:return:
If the game is playing or not
"""
self.turn += 1
board = self.map
if self.bikers is None:
self.bikers = self.get_bikers(self.id_team)
deliveries = self.deliveries
# others_bikers = [self.get_bikers(id_) for id_ in range(self.teams) if id_ != self.id_team]
actions = ACTION_POINTS
while actions > 0:
if self.biker is None or len(self.biker.carrying) == 0:
# We find the closest delivery to take from the deliveries list
biker1 = self.bikers[0]
biker2 = self.bikers[1]
pos1 = biker1.pos
pos2 = biker2.pos
path1, delivery1 = board.nearest_delivery_to_take(deliveries, pos1)
path2, delivery2 = board.nearest_delivery_to_take(deliveries, pos2)
if path2 is None or (path1 is not None and len(path1) <= len(path2)):
self.biker, path, delivery = biker1, path1, delivery1
elif path1 is None or (path2 is not None and len(path2) <= len(path1)):
self.biker, path, delivery = biker2, path2, delivery2
else:
return self.end_and_wait_next_turn()
shop = True
elif len(self.biker.carrying) < 3:
# We find the closest delivery to take from the deliveries list OR to deliver from the biker's backpack
path_take, delivery_take = board.nearest_delivery_to_take(deliveries, self.biker.pos)
path_depose, delivery_depose = board.nearest_delivery_to_depose(self.biker.carrying, self.biker.pos)
if path_take is None or (path_depose is not None and len(path_depose) < len(path_take)):
path, delivery = path_depose, delivery_depose
shop = False
elif path_depose is None or (path_take is not None and len(path_take) < len(path_depose)):
path, delivery = path_take, delivery_take
shop = True
else:
return self.end_and_wait_next_turn()
else:
# We find the closest delivery to deliver from the biker's backpack
path, delivery = board.nearest_delivery_to_depose(self.biker.carrying, self.biker.pos)
if path is None:
return self.end_and_wait_next_turn()
shop = False
if path:
dir_ = path[0]
self.move(self.biker.nu, dir_)
self.biker.pos = self.biker.pos.next_in_direction(dir_)
elif shop:
self.take(self.biker.nu, delivery.code)
self.biker.carrying.add(delivery)
deliveries.remove(delivery)
# for delivery in deliveries:
# if delivery.coord_restaurant == next \
# and delivery not in self.biker.carrying \
# and len(self.biker.carrying) < 3 \
# and actions > 1:
# self.take(self.biker.nu, delivery.code)
# self.biker.carrying.append(delivery)
# actions -= 1
else:
self.deliver(self.biker.nu, delivery.code)
self.biker.carrying.remove(delivery)
actions -= 1
return self.end_and_wait_next_turn()
def get_route_neighbor(self, coord: Coordinate):
"""
Find a route near to coord
:param coord:
:return: Coordinate of a road location
"""
for adj in coord.adjacent():
if self.map.route(adj.x, adj.y):
return coord
return None