started implementing interface between core game logic and web app
This commit is contained in:
parent
e73c4f8f63
commit
5359dfc4ac
39
app.py
39
app.py
|
@ -47,42 +47,48 @@ 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))
|
||||||
|
|
||||||
# apply the chosen commands to the board which generates a list of movement commands to send to the control program
|
# process the chosen commands and generate a list of robot commands to send to the controller 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 program
|
# send movements to the controller program
|
||||||
for c in current_actions:
|
for c in cmd_list:
|
||||||
if c[0] == 0:
|
self.comm_socket.sendall(c.encode())
|
||||||
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 occured while processing the commands")
|
print("an error occurred 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
|
self.processing_done = True
|
||||||
|
|
||||||
|
players = {}
|
||||||
|
game = Game()
|
||||||
|
|
||||||
class Player:
|
class Player:
|
||||||
MAX_PLAYERS = 3
|
MAX_PLAYERS = 3
|
||||||
player_counter = 0
|
player_counter = 0
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
if Player.player_counter < Player.MAX_PLAYERS:
|
if Player.player_counter < Player.MAX_PLAYERS:
|
||||||
self.id = Player.player_counter
|
self.id = Player.player_counter
|
||||||
|
self.marker_id = 11
|
||||||
Player.player_counter += 1
|
Player.player_counter += 1
|
||||||
|
|
||||||
self.max_cards = 9
|
self.max_cards = 9
|
||||||
|
@ -93,15 +99,14 @@ class Player:
|
||||||
self.action_count = 5
|
self.action_count = 5
|
||||||
self.action_chosen = False
|
self.action_chosen = False
|
||||||
|
|
||||||
|
self.robot = game.board.create_robot(1,1,'>', self.id, self.marker_id)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print("max players reached!")
|
print("max players reached!")
|
||||||
|
|
||||||
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():
|
||||||
|
|
29
roborally.py
29
roborally.py
|
@ -78,11 +78,11 @@ 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, id, board):
|
def __init__(self, x, y, orientation, marker_id, board):
|
||||||
self.x = x
|
self.x = x
|
||||||
self.y = y
|
self.y = y
|
||||||
self.orientation = orientation
|
self.orientation = orientation
|
||||||
self.id = id
|
self.marker_id = marker_id
|
||||||
self.damage = 0
|
self.damage = 0
|
||||||
self.collected_flags = set()
|
self.collected_flags = set()
|
||||||
|
|
||||||
|
@ -146,7 +146,7 @@ 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.id, type)
|
return "{}, {}".format(self.marker_id, type)
|
||||||
|
|
||||||
def move(self, type):
|
def move(self, type):
|
||||||
# move the robot forward or backward
|
# move the robot forward or backward
|
||||||
|
@ -165,7 +165,7 @@ 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.id)
|
return "{}, forward".format(self.marker_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.get_adjecent_tile(opposite_orientation)
|
||||||
|
@ -180,14 +180,14 @@ 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.id)
|
return "{}, backward".format(self.marker_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.id)
|
return "{}, nop".format(self.marker_id)
|
||||||
|
|
||||||
def board_element_processable(self):
|
def board_element_processable(self):
|
||||||
# check if we can directly process the board element for the tile the current robot is located on
|
# check if we can directly process the board element for the tile the current robot is located on
|
||||||
|
@ -210,7 +210,7 @@ class Robot:
|
||||||
self.collected_flags.add(flag)
|
self.collected_flags.add(flag)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.id)
|
return str(self.marker_id)
|
||||||
|
|
||||||
|
|
||||||
class Tile:
|
class Tile:
|
||||||
|
@ -222,6 +222,8 @@ class Tile:
|
||||||
# p : pit (robot takes damage)
|
# p : pit (robot takes damage)
|
||||||
# r : repair station (robot heals damage)
|
# r : repair station (robot heals damage)
|
||||||
# [a,b,c,d] : flag (robot scores)
|
# [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
|
||||||
|
@ -302,10 +304,15 @@ class Board:
|
||||||
# self.board[(2, 1)].modifier = '<'
|
# self.board[(2, 1)].modifier = '<'
|
||||||
|
|
||||||
self.robots = {}
|
self.robots = {}
|
||||||
self.robots[0] = Robot(1, 1, 'v', 0, self.board)
|
#self.robots[0] = Robot(1, 1, 'v', 0, self.board)
|
||||||
#self.robots[1] = Robot(1, 2, 'v', 1, self.board)
|
#self.robots[1] = Robot(1, 2, 'v', 1, self.board)
|
||||||
#self.robots[2] = Robot(2, 1, '>', 2, self.board)
|
#self.robots[2] = Robot(2, 1, '>', 2, self.board)
|
||||||
#self.robots[3] = Robot(2, 2, 'v', 3, 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):
|
||||||
|
self.robots[player_id] = Robot(x, y, orientation, marker_id, self.board)
|
||||||
|
|
||||||
|
|
||||||
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 = []
|
||||||
|
@ -522,9 +529,9 @@ 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 in self.robots.values()):
|
if any((r.x, r.y) == (x,y) for (r_id, r) in self.robots.items()):
|
||||||
r = list(filter(lambda r: (r.x,r.y) == (x,y), self.robots.values()))[0]
|
r = next(filter(lambda r: (r[1].x,r[1].y) == (x,y), self.robots.items()))
|
||||||
output += str(r.id)
|
output += str(r[0])
|
||||||
else:
|
else:
|
||||||
output += str(self.board[(x, y)])
|
output += str(self.board[(x, y)])
|
||||||
output += '\n'
|
output += '\n'
|
||||||
|
|
Loading…
Reference in New Issue
Block a user