-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bike.py
72 lines (64 loc) · 3.06 KB
/
Bike.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
from Bot import *
from Position import *
from Constants import *
class Bike :
""" Represents the bike in arena. """
def __init__ (self, symbol, starting_posn, code_file) :
""" Initializes the bike. """
self.curr_posn = starting_posn
self.prev_posn = Position (starting_posn.x,
starting_posn.y,
starting_posn.max_x,
starting_posn.max_y)
self.symbol = symbol
self.bot = Bot (symbol, code_file)
self.is_dead = False
def __str__ (self) :
""" For pretty printing the object. """
return ("Current Position : " + self.curr_posn.__str__ () + linesep
+ "Previous Position : " + self.prev_posn.__str__ () + linesep
+ "Symbol : " + symbol + linesep)
def put_trail_for_html (self, map) :
""" The function which reports a different symbol for
the trail for the visualizer purposes. """
if self.symbol == BIKE_1_SYMBOL :
trail_symbol = TRAIL1
else :
trail_symbol = TRAIL2
map.log_updates (self.prev_posn.x,
self.prev_posn.y,
map.get_symbol (self.prev_posn),
trail_symbol)
def get_move (self, updates, map, first_move = False) :
""" Gets the move from the bot.
Sets the curr_position accordingly. """
direction = self.bot.get_move (updates, first_move)
self.prev_posn = Position (self.curr_posn.x,
self.curr_posn.y,
self.curr_posn.max_x,
self.curr_posn.max_y)
self.curr_posn.update (direction)
self.put_trail_for_html (map)
def power_up (self, arena, power_symbol) :
""" Take appropriate actions depending on
the Power up symbol. """
if power_symbol == RESET :
# if the map cell is a bot symbol and not the head of the trail
# ie, not the current position of the bike itself, delete all the
map_size = arena.map.size
for i in range (map_size) :
for j in range (map_size) :
position = Position (i, j, map_size, map_size)
if (arena.map.get_symbol (position) in [BIKE_1_SYMBOL,
BIKE_2_SYMBOL]
and (arena.bikes[0].curr_posn != position)
and (arena.bikes[1].curr_posn != position)) :
arena.map.set_symbol (position, EMPTY)
elif power_symbol == TRAVERSER :
self.bot.traverser_left += TRAVERSER_CAPACITY + 1
# The + 1 is necessary because later, during this move,
# it is going to be decreased by 1.
elif power_symbol == NITRO :
self.bot.nitro_left += NITRO_QUANTITY + 1
# The + 1 is necessary because later, during this move,
# it is going to be decreased by 1.