started implementing core game logic

master
Simon Pirkelmann 2020-09-15 18:05:33 +02:00
parent e74cf124ce
commit a4857c720a
1 changed files with 97 additions and 13 deletions

110
app.py
View File

@ -8,16 +8,88 @@ app.secret_key = b'RoboRallyRolling'
random.seed(0)
moves = ['forward', 'backward', 'turn left', 'turn right', 'turn around']
moves = ['forward', 'forward x2', 'forward x3', 'backward', 'turn left', 'turn right', 'turn around']
probabilities = [0.21428571428571427, 0.14285714285714285, 0.07142857142857142, 0.07142857142857142, 0.21428571428571427, 0.21428571428571427, 0.07142857142857142]
class Robot:
def __init__(self, x, y, orientation, id):
self.x = x
self.y = y
self.orientation = orientation
self.id = id
def move(self, type):
pass
def __str__(self):
return str(self.id)
class Tile:
# possible modifiers:
# conveyors: <, >, ^, v
# repair station: r
# flag: f<number>
def __init__(self, x, y, modifier=None):
self.modifier = modifier
self.occupant = None
self.x = x
self.y = y
def is_empty(self):
return self.occupant is None
def __str__(self):
if self.is_empty():
if self.modifier is None:
return ' '
else:
return self.modifier
else:
return str(self.occupant)
class Board:
x_dims = 12 # number of tiles in x direction
y_dims = 6 # number of tiles in y direction
def __init__(self):
self.board = {}
for x in range(Board.x_dims):
for y in range(Board.y_dims):
if x == 0 and (y >= 1) and (y <= 4):
self.board[(x,y)] = Tile(x,y,'v')
else:
self.board[(x, y)] = Tile(x, y)
self.board[(0,0)].occupant = Robot(0,0,'>',1)
self.board[(2,0)].occupant = Robot(2,0,'v',2)
def apply_actions(self, actions):
# apply the actions to the board and generate a list of movement commands
# sort actions by priority
sorted_actions = sorted(actions, key=lambda a: a[1].priority)
pass
def __str__(self):
output = '#' * (Board.x_dims + 2) + '\n'
for y in range(Board.y_dims):
output += '#'
for x in range(Board.x_dims):
output += str(self.board[(x,y)])
output += '#\n'
output += '#' * (Board.x_dims + 2)
return output
class Game:
def __init__(self):
self.action_stack = {}
self.processing_done = False # indicates whether all commands in the current round have been processed
self.board = Board()
self.comm_socket = socket.socket() # socket for communicating with the program controlling the robots
try:
self.comm_socket.connect(('192.168.1.213', 1337))
self.comm_socket.connect(('192.168.1.222', 1337))
except socket.error:
print("could not connect to robot control socket!")
@ -42,15 +114,29 @@ class Game:
current_actions = []
for p in self.action_stack.keys():
current_actions += [(p, self.action_stack[p][i])]
for c in current_actions:
print("current actions = ", current_actions)
# generate list of movement commands to send to the control program
self.board.apply_actions(current_actions)
if False:
# send movements to the program
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)
if data == b'OK\n':
print("an error occured while processing the commands")
self.processing_done = True
self.action_stack = {}
return
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(3)
time.sleep(0.5)
#self.comm_socket.send()
@ -73,7 +159,7 @@ class Card:
return "Card No. " + str(self.number) + " " + self.action + " " + str(self.priority)
class CardDeck:
def __init__(self, n=20):
def __init__(self, n=84):
self.deck = {}
# generate cards
for i in range(0,n):
@ -126,8 +212,6 @@ class Player:
print("current hand: ", [str(c) for c in self.player_hand])
self.action_count = 5
self.action_chosen = False
else: