Compare commits

..

No commits in common. "845c6b1cf165c93032d4947b79584cff6fb05560" and "003113cb89041252cef05e20bc9aa1511e52bfc2" have entirely different histories.

2 changed files with 51 additions and 204 deletions

53
app.py
View File

@ -47,40 +47,35 @@ class Game:
l.append(zip([p] * len(self.action_stack[p]), self.action_stack[p])) l.append(zip([p] * len(self.action_stack[p]), self.action_stack[p]))
chosen_cards = list(zip(*l)) chosen_cards = list(zip(*l))
# process the chosen commands and generate a list of robot commands to send to the controller program # apply the chosen commands to the board which generates a list of movement commands to send to the control program
cmd_list = self.board.apply_actions(chosen_cards) cmd_list = self.board.apply_actions(chosen_cards)
if False: if False:
# send movements to the controller program # send movements to the program
for c in cmd_list: for c in current_actions:
self.comm_socket.sendall(c.encode()) if c[0] == 0:
print("{}, {}\n".format(c[1].action, 11))
self.comm_socket.sendall("{}, {}\n".format(c[1].action, 11).encode())
elif c[0] == 1:
print("{}, {}\n".format(c[1].action, 14))
self.comm_socket.sendall("{}, {}\n".format(c[1].action, 14).encode())
data = self.comm_socket.recv(32) data = self.comm_socket.recv(32)
if data != b'OK\n': if data == b'OK\n':
print("an error occurred while processing the commands") print("an error occured while processing the commands")
self.processing_done = True self.processing_done = True
self.action_stack = {} self.action_stack = {}
return return
# for c in current_actions:
# if c[0] == 0:
# print("{}, {}\n".format(c[1].action, 11))
# self.comm_socket.sendall("{}, {}\n".format(c[1].action, 11).encode())
# elif c[0] == 1:
# print("{}, {}\n".format(c[1].action, 14))
# self.comm_socket.sendall("{}, {}\n".format(c[1].action, 14).encode())
# data = self.comm_socket.recv(32)
#
time.sleep(0.5) time.sleep(0.5)
#self.comm_socket.send()
# clear the action stack for the next round # clear the action stack for the next round
self.action_stack = {} self.action_stack = {}
self.processing_done = True
players = {} self.processing_done = True
game = Game()
class Player: class Player:
MAX_PLAYERS = 3 MAX_PLAYERS = 3
@ -98,17 +93,15 @@ class Player:
self.action_count = 5 self.action_count = 5
self.action_chosen = False self.action_chosen = False
self.robot = None
else: else:
print("max players reached!") print("max players reached!")
def initialize_robot(self, x, y, orientation, marker_id):
self.robot = game.board.create_robot(x, y, orientation, self.id, marker_id)
def draw_new_cards(self): def draw_new_cards(self):
self.player_hand += deck.draw_cards(self.max_cards - len(self.player_hand)) self.player_hand += deck.draw_cards(self.max_cards - len(self.player_hand))
players = {}
game = Game()
@app.route('/send_cmds', methods=['POST', 'GET']) @app.route('/send_cmds', methods=['POST', 'GET'])
def send_cmds(): def send_cmds():
@ -117,11 +110,6 @@ def send_cmds():
player_id = session['player_id'] player_id = session['player_id']
p = players[player_id] p = players[player_id]
if p.robot is None:
x = int(request.form.get('x'))
y = int(request.form.get('y'))
p.initialize_robot(x, y, '>', 11)
if game.register_actions(p.id, p.player_hand[0:p.action_count]): if game.register_actions(p.id, p.player_hand[0:p.action_count]):
p.player_hand = p.player_hand[p.action_count:] # discard used cards p.player_hand = p.player_hand[p.action_count:] # discard used cards
p.draw_new_cards() p.draw_new_cards()
@ -161,12 +149,7 @@ def hello_world():
if request.method == 'GET': if request.method == 'GET':
robot = players[player_id].robot return render_template('drag_example.html', cmds=player_hand, player_id=player_id)
if robot is not None:
robot_pos = (robot.x, robot.y)
else:
robot_pos = None
return render_template('drag_example.html', cmds=player_hand, player_id=player_id, robot_pos=robot_pos)
elif request.method == 'POST': elif request.method == 'POST':
#print(request.form) #print(request.form)

View File

@ -78,38 +78,27 @@ class Robot:
# dictionary mapping an orientation to its opposite # dictionary mapping an orientation to its opposite
opposites = {'^': 'v', '>': '<', 'v': '^', '<': '>'} opposites = {'^': 'v', '>': '<', 'v': '^', '<': '>'}
def __init__(self, x, y, orientation, marker_id, board): def __init__(self, x, y, orientation, id, board):
self.x = x self.x = x
self.y = y self.y = y
self.orientation = orientation self.orientation = orientation
self.marker_id = marker_id self.id = id
self.damage = 0
self.collected_flags = set()
self.board = board self.board = board
# mark the tile on the board as occupied # mark the tile on the board as occupied
self.board[(x,y)].occupant = self self.board[(x,y)].occupant = self
def get_tile(self):
# return the tile the robot is standing on
return self.board[(self.x, self.y)]
def get_adjecent_tile(self, direction):
# get the tile adjecent to the robot in the given direction
current_tile = self.get_tile()
return self.board[current_tile.get_neighbor_coordinates(direction)]
def get_accessed_tiles(self, count, forward=True): def get_accessed_tiles(self, count, forward=True):
# create a list of all tiles the robot would enter if it drives <count> steps forward # create a list of all tiles the robot would enter if it drives <count> steps forward
tiles = [] tiles = []
current_tile = self.get_tile() current_tile = self.board[(self.x, self.y)]
for i in range(1, count + 1): for i in range(1, count + 1):
if forward: if forward:
current_tile = self.board.get(current_tile.get_neighbor_coordinates(self.orientation)) current_tile = self.board.get(current_tile.get_neighbor_coordinates(self.orientation))
else: else:
current_tile = self.board.get(current_tile.get_neighbor_coordinates(Robot.opposites[self.orientation])) current_tile = self.board.get(current_tile.get_neighbor_coordinates(Robot.opposites[self.orientation]))
if current_tile is None: if current_tile is None:
return tiles return tiles
else: else:
@ -119,7 +108,7 @@ class Robot:
def is_pushable(self, direction): def is_pushable(self, direction):
# check if the robot can be pushed in the given direction # check if the robot can be pushed in the given direction
# this is the case if there is a non-blocking tile next to the robot or if there is another robot that is pushable # this is the case if there is a non-blocking tile next to the robot or if there is another robot that is pushable
robot_tile = self.get_tile() robot_tile = self.board[(self.x, self.y)]
neighbor_tile = self.board.get(robot_tile.get_neighbor_coordinates(direction)) neighbor_tile = self.board.get(robot_tile.get_neighbor_coordinates(direction))
if neighbor_tile is None: # neighbor tile could not be found -> robot would be pushed out of the board if neighbor_tile is None: # neighbor tile could not be found -> robot would be pushed out of the board
return False return False
@ -146,14 +135,14 @@ class Robot:
# change the orientation of the robot # change the orientation of the robot
self.orientation = Robot.resulting_orientation[self.orientation][type] self.orientation = Robot.resulting_orientation[self.orientation][type]
return "{}, {}".format(self.marker_id, type) return "{}, {}".format(self.id, type)
def move(self, type): def move(self, type):
# move the robot forward or backward # move the robot forward or backward
# this involves # this involves
tile = self.get_tile() tile = self.board[(self.x, self.y)]
if type == 'forward': if type == 'forward':
target_tile = self.get_adjecent_tile(self.orientation) target_tile = self.board[tile.get_neighbor_coordinates(self.orientation)]
if target_tile.occupant is not None: if target_tile.occupant is not None:
print("error: target tile is not empty") print("error: target tile is not empty")
@ -165,10 +154,10 @@ class Robot:
self.y = target_tile.y self.y = target_tile.y
# return the move for sending to the controller # return the move for sending to the controller
return "{}, forward".format(self.marker_id) return "{}, forward".format(self.id)
elif type == 'backward': elif type == 'backward':
opposite_orientation = self.get_opposite_orientation() opposite_orientation = self.get_opposite_orientation()
target_tile = self.get_adjecent_tile(opposite_orientation) target_tile = self.board[tile.get_neighbor_coordinates(opposite_orientation)]
if target_tile.occupant is not None: if target_tile.occupant is not None:
print("error: target tile is not empty") print("error: target tile is not empty")
@ -180,50 +169,24 @@ class Robot:
self.y = target_tile.y self.y = target_tile.y
# return the move for sending to the controller # return the move for sending to the controller
return "{}, backward".format(self.marker_id) return "{}, backward".format(self.id)
else: else:
print("error: invalid move") print("error: invalid move")
sys.exit(1) sys.exit(1)
def nop(self): def nop(self):
# do nothing command # do nothing command
return "{}, nop".format(self.marker_id) return "{}, nop".format(self.id)
def board_element_processable(self):
# 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 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): def __str__(self):
return str(self.marker_id) return str(self.id)
class Tile: class Tile:
# possible modifiers: # possible modifiers:
# # : wall (robot is blocked from moving there) # conveyors: <, >, ^, v
# [<, >, ^, v] : conveyors (robot is pushed to the next tile) # repair station: r
# + : rotation in positive direction (robot is rotated ccw) # flag: f<number>
# - : 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)
#
# occupant: Robot that is standing on the tile
def __init__(self, x, y, modifier=None): def __init__(self, x, y, modifier=None):
self.modifier = modifier self.modifier = modifier
self.occupant = None self.occupant = None
@ -275,46 +238,16 @@ class Board:
if (x == 0) or (x == Board.x_dims + 1) or (y == 0) or (y == Board.y_dims + 1): if (x == 0) or (x == Board.x_dims + 1) or (y == 0) or (y == Board.y_dims + 1):
# place walls around the board # place walls around the board
self.board[(x, y)] = Tile(x, y, '#') 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): elif x == 1 and (y >= 1) and (y < 4):
self.board[(x, y)] = Tile(x, y, 'v') self.board[(x, y)] = Tile(x, y, 'v')
elif y == 4: elif y == 4:
self.board[(x, y)] = Tile(x, y, '>') self.board[(x, y)] = Tile(x, y, '>')
elif y == 1 and (x >= 2) and (x < 5):
self.board[(x, y)] = Tile(x, y, '>')
elif y == 1 and (x >= 6) and (x <= 8):
self.board[(x, y)] = Tile(x, y, '<')
else: else:
self.board[(x,y)] = Tile(x,y) self.board[(x,y)] = Tile(x,y)
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 = {}
#self.robots[0] = Robot(1, 1, 'v', 0, self.board) self.robots[0] = Robot(3, 1, '>', 0, self.board)
#self.robots[1] = Robot(1, 2, 'v', 1, self.board) self.robots[1] = Robot(2, 1, 'v', 1, self.board)
#self.robots[2] = Robot(2, 1, '>', 2, self.board)
#self.robots[3] = Robot(2, 2, 'v', 3, self.board)
#self.create_robot(1,1,'>', 7, 11)
def create_robot(self, x, y, orientation, player_id, marker_id):
new_robot = Robot(x, y, orientation, marker_id, self.board)
self.robots[player_id] = new_robot
return new_robot
def handle_push(self, direction, pushed_robot, forward=True, pushing_robot=None): def handle_push(self, direction, pushed_robot, forward=True, pushing_robot=None):
cmd_list = [] cmd_list = []
@ -433,24 +366,12 @@ class Board:
def handle_board_element(self, robot): def handle_board_element(self, robot):
cmd_list = [] cmd_list = []
tile = self.board[(robot.x, robot.y)] tile = self.board[(robot.x, robot.y)]
if tile.modifier is None: if tile.modifier in ['^', '>', 'v', '<']:
return cmd_list
elif tile.modifier in ['^', '>', 'v', '<']:
# board element pushes the robot to next tile # board element pushes the robot to next tile
if robot.is_pushable(tile.modifier): if robot.is_pushable(tile.modifier):
cmd_list += self.handle_push(direction=tile.modifier, pushed_robot=robot, forward=True) cmd_list += self.handle_push(direction=tile.modifier, pushed_robot=robot, forward=True)
else: else:
cmd_list.append(robot.nop()) 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 return cmd_list
def apply_actions(self, cards): def apply_actions(self, cards):
@ -471,59 +392,14 @@ class Board:
cmd_list += self.handle_single_action(action, robot) cmd_list += self.handle_single_action(action, robot)
print(self)
# apply the actions caused by board elements at the end of the phase # apply the actions caused by board elements at the end of the phase
self.apply_board_element_actions() for robot_id in self.robots:
robot = self.robots[robot_id]
cmd_list += self.handle_board_element(robot)
print(cmd_list)
print(self) print(self)
pass
return cmd_list
def apply_board_element_actions(self):
cmd_list = []
remaining_robots = set(self.robots.values())
processed_robots = set()
# first we compute all tiles the robots would enter as a result of board game elements
target_tiles = {} # get target tiles for each robot
for r in remaining_robots:
tile = r.get_tile()
if tile.modifier in ['^', '>', 'v', '<']: # tile would push the robot around
direction = tile.modifier
target_tiles[r] = r.get_adjecent_tile(direction) # save tile the robot would be pushed to
# now we check if there are any conflicts
conflicting_tiles = set([x for x in target_tiles.values() if list(target_tiles.values()).count(x) > 1])
if len(conflicting_tiles) > 0: # check if any robots would be pushed to the same tile
# there is a conflict -> skip the board element execution and mark those robots as processed
conflicting_robots = set(filter(lambda r: target_tiles[r] in conflicting_tiles, target_tiles.keys()))
processed_robots = processed_robots.union(conflicting_robots)
# Now we process the board game elements for the robots which have no conflicts.
# We have to pay attention to the order of the execution in order to avoid robots pushing other robots
# during this phase.
# This is done in a loop because we don't know yet which robot goes first. For instance, it may happen that
# multiple robots are queued on a conveyor belt. Then we first have to move the robot which is furthest down the
# line, then the second one and so on
# By doing this in a loop we can automatically determine the correct order by checking for each robot if it can
# move and then processing the robot such that the next robot can move
while len(processed_robots) < len(self.robots):
# update remaining robots to process
remaining_robots = set(self.robots.values()) - processed_robots
# check which robots can be moved around
processable_robots = list(filter(lambda r: r.board_element_processable(), remaining_robots))
if len(processable_robots) > 0:
# handle the board game elements for robots that can move
for current_robot in processable_robots:
cmd_list += self.handle_board_element(current_robot)
processed_robots.add(current_robot)
else:
# this happens if there is a deadlock that cannot be resolved (e.g. caused by a cyclical conveyor belt)
break
return cmd_list return cmd_list
def __str__(self): def __str__(self):
@ -531,41 +407,29 @@ class Board:
output = '' output = ''
for y in range(Board.y_dims+2): for y in range(Board.y_dims+2):
for x in range(Board.x_dims+2): for x in range(Board.x_dims+2):
if any((r.x, r.y) == (x,y) for (r_id, r) in self.robots.items()): if any((r.x, r.y) == (x,y) for r in self.robots.values()):
r = next(filter(lambda r: (r[1].x,r[1].y) == (x,y), self.robots.items())) r = list(filter(lambda r: (r.x,r.y) == (x,y), self.robots.values()))[0]
output += str(r[0]) output += str(r.id)
else: else:
output += str(self.board[(x, y)]) output += str(self.board[(x, y)])
output += '\n' output += '\n'
#output += '#' * (Board.x_dims + 2) #output += '#' * (Board.x_dims + 2)
for r_id, r in self.robots.items():
output += "Robot {}: {}\n".format(r_id, r.orientation)
return output return output
if __name__ == "__main__": if __name__ == "__main__":
n = 5 n = 5
deck = CardDeck(n=1000) deck = CardDeck()
player_1_cards = deck.draw_cards(200) player_1_cards = random.sample(list(filter(lambda c: 'backward' in c.action, deck.deck.values())), n)
#player_2_cards = deck.draw_cards(200) player_2_cards = random.sample(list(filter(lambda c: 'turn around' in c.action, deck.deck.values())), n)
#player_3_cards = deck.draw_cards(200) #player_1_cards = deck.draw_cards(40)
#player_4_cards = deck.draw_cards(200) #player_2_cards = deck.draw_cards(40)
#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_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]
chosen_cards = list(zip(cards_1, cards_2))
#chosen_cards = list(zip(cards_1, cards_2, cards_3, cards_4))
chosen_cards = list(zip(cards_1))
b = Board() b = Board()
print(b) print(b)