finished board element logic

This commit is contained in:
Simon Pirkelmann 2020-09-23 22:50:31 +02:00
parent f33513644f
commit e73c4f8f63

View File

@ -83,6 +83,8 @@ class Robot:
self.y = y
self.orientation = orientation
self.id = id
self.damage = 0
self.collected_flags = set()
self.board = board
@ -191,24 +193,35 @@ class Robot:
# check if we can directly process the board element for the tile the current robot is located on
tile = self.get_tile()
if tile.modifier is None:
return True
elif tile.modifier in ['^', '>', 'v', '<']:
if tile.modifier in ['^', '>', 'v', '<']:
direction = tile.modifier
neighbor_tile = self.get_adjecent_tile(direction)
return neighbor_tile.occupant is None # if the adjacent tile the robot will be pushed into is empty
# we can execute the push
return True
def take_damage(self, count):
self.damage = min(self.damage + count, 10)
def heal_damage(self, count):
self.damage = max(self.damage - count, 0)
def pick_up_flag(self, flag):
self.collected_flags.add(flag)
def __str__(self):
return str(self.id)
class Tile:
# possible modifiers:
# conveyors: <, >, ^, v
# repair station: r
# flag: f<number>
# # : wall (robot is blocked from moving there)
# [<, >, ^, v] : conveyors (robot is pushed to the next tile)
# + : rotation in positive direction (robot is rotated ccw)
# - : rotation in negative direction (robot is rotated cw)
# p : pit (robot takes damage)
# r : repair station (robot heals damage)
# [a,b,c,d] : flag (robot scores)
def __init__(self, x, y, modifier=None):
self.modifier = modifier
self.occupant = None
@ -260,6 +273,8 @@ class Board:
if (x == 0) or (x == Board.x_dims + 1) or (y == 0) or (y == Board.y_dims + 1):
# place walls around the board
self.board[(x, y)] = Tile(x, y, '#')
elif y > 2 and y < 6 and x == 7:
self.board[(x, y)] = Tile(x, y, '#')
elif x == 1 and (y >= 1) and (y < 4):
self.board[(x, y)] = Tile(x, y, 'v')
elif y == 4:
@ -271,16 +286,26 @@ class Board:
else:
self.board[(x,y)] = Tile(x,y)
self.board[(1, 1)].modifier = 'v'
self.board[(1, 2)].modifier = '>'
self.board[(2, 2)].modifier = '^'
self.board[(2, 1)].modifier = '<'
self.board[(5, 1)].modifier = '+'
self.board[(5, 4)].modifier = '-'
self.board[(2, 2)].modifier = 'p'
self.board[(3, 3)].modifier = 'r'
# place flags near the corners of the board
self.board[(2,2)].modifier = 'a'
self.board[(Board.x_dims-1, 2)].modifier = 'b'
self.board[(Board.x_dims-1, Board.y_dims-1)].modifier = 'c'
self.board[(2, Board.y_dims-1)].modifier = 'd'
# self.board[(2, 2)].modifier = '^'
# self.board[(2, 1)].modifier = '<'
self.robots = {}
self.robots[0] = Robot(1, 1, '>', 0, self.board)
self.robots[1] = Robot(1, 2, 'v', 1, self.board)
self.robots[2] = Robot(2, 1, '>', 2, self.board)
self.robots[3] = Robot(2, 2, 'v', 3, self.board)
self.robots[0] = Robot(1, 1, 'v', 0, self.board)
#self.robots[1] = Robot(1, 2, 'v', 1, self.board)
#self.robots[2] = Robot(2, 1, '>', 2, self.board)
#self.robots[3] = Robot(2, 2, 'v', 3, self.board)
def handle_push(self, direction, pushed_robot, forward=True, pushing_robot=None):
cmd_list = []
@ -399,12 +424,24 @@ class Board:
def handle_board_element(self, robot):
cmd_list = []
tile = self.board[(robot.x, robot.y)]
if tile.modifier in ['^', '>', 'v', '<']:
if tile.modifier is None:
return cmd_list
elif tile.modifier in ['^', '>', 'v', '<']:
# board element pushes the robot to next tile
if robot.is_pushable(tile.modifier):
cmd_list += self.handle_push(direction=tile.modifier, pushed_robot=robot, forward=True)
else:
cmd_list.append(robot.nop())
elif tile.modifier == '+':
cmd_list.append(robot.turn('turn left'))
elif tile.modifier == '-':
cmd_list.append(robot.turn('turn right'))
elif tile.modifier == 'p':
robot.take_damage(1)
elif tile.modifier == 'r':
robot.heal_damage(1)
elif tile.modifier in 'abcd':
robot.pick_up_flag(tile.modifier)
return cmd_list
def apply_actions(self, cards):
@ -430,6 +467,8 @@ class Board:
# apply the actions caused by board elements at the end of the phase
self.apply_board_element_actions()
print(self)
return cmd_list
def apply_board_element_actions(self):
@ -490,30 +529,34 @@ class Board:
output += str(self.board[(x, y)])
output += '\n'
#output += '#' * (Board.x_dims + 2)
for r_id, r in self.robots.items():
output += "Robot {}: {}\n".format(r_id, r.orientation)
return output
if __name__ == "__main__":
n = 5
deck = CardDeck()
deck = CardDeck(n=1000)
player_1_cards = random.sample(list(filter(lambda c: 'turn around' in c.action, deck.deck.values())), n)
player_2_cards = random.sample(list(filter(lambda c: 'turn around' in c.action, deck.deck.values())), n)
#player_1_cards = deck.draw_cards(40)
#player_2_cards = deck.draw_cards(40)
player_1_cards = deck.draw_cards(200)
#player_2_cards = deck.draw_cards(200)
#player_3_cards = deck.draw_cards(200)
#player_4_cards = deck.draw_cards(200)
#player_1_cards = random.sample(list(filter(lambda c: 'turn around' in c.action, deck.deck.values())), n)
#player_2_cards = random.sample(list(filter(lambda c: 'turn around' in c.action, deck.deck.values())), n)
#player_3_cards = random.sample(list(filter(lambda c: 'turn around' in c.action, deck.deck.values())), n)
#player_4_cards = random.sample(list(filter(lambda c: 'turn around' in c.action, deck.deck.values())), n)
cards_1 = [(0, c) for c in player_1_cards]
cards_2 = [(1, c) for c in player_2_cards]
#cards_2 = [(1, c) for c in player_2_cards]
#cards_3 = [(2, c) for c in player_3_cards]
#cards_4 = [(3, c) for c in player_4_cards]
player_3_cards = random.sample(list(filter(lambda c: 'turn around' in c.action, deck.deck.values())), n)
player_4_cards = random.sample(list(filter(lambda c: 'turn around' in c.action, deck.deck.values())), n)
#player_1_cards = deck.draw_cards(40)
#player_2_cards = deck.draw_cards(40)
cards_3 = [(0, c) for c in player_3_cards]
cards_4 = [(1, c) for c in player_4_cards]
chosen_cards = list(zip(cards_1, cards_2, cards_3, cards_4))
#chosen_cards = list(zip(cards_1, cards_2, cards_3, cards_4))
chosen_cards = list(zip(cards_1))
b = Board()
print(b)