def generate_board(max_width: int, max_height: int, wall_chance: float = 0.2) \ -> Board: board = Board(max_width, max_height) xoffset = random.randint(-max_width + 1, 0) yoffset = random.randint(-max_height + 1, 0) yrange = range(0 + yoffset, board.max_y + 1 + yoffset) xrange = range(0 + xoffset, board.max_x + 1 + xoffset) for y in yrange: for x in xrange: cell = board[Position(x, y)] cell.explored = True cell.set_wall( Direction.North, Wall.Yes if y == yrange.start else randwall(wall_chance)) cell.set_wall( Direction.South, Wall.Yes if y == yrange.stop-1 else randwall(wall_chance)) cell.set_wall( Direction.East, Wall.Yes if x == xrange.stop-1 else randwall(wall_chance)) cell.set_wall( Direction.West, Wall.Yes if x == xrange.start else randwall(wall_chance)) return board
def place_dest(): ctx = get_ctx() x = int(request.args.get('x')) y = int(request.args.get('y')) botname = request.args.get('bot') ctx.destbot = [b for b in ctx.bots if b.name == botname][0] ctx.destination = Position(x, y) return _render()
def _find_empty_cell(self): for y in range(self.board.min_y, self.board.max_y + 1): for x in range(self.board.min_x, self.board.max_x + 1): pos = Position(x, y) cell = self.board[pos] if cell.explored or any([bot.pos == pos for bot in self.bots]): continue return cell
def prepare_game(self): #self.red.emergency_stop(False) for y in range(self.board.min_y, self.board.max_y + 1): for x in range(self.board.min_x, self.board.max_x + 1): self.board[Position(x, y)].explored_by = None self.green = ProxyBot(self.get_green()) self.blue = ProxyBot(self.get_blue()) self.bots = [self.red, self.green, self.blue] self.green.pos = self._find_empty_cell().pos self.blue.pos = self._find_empty_cell().pos
def render(self): board = self.board out = StringIO() out.write('<table class="robotgrid">') for y in range(board.min_y, board.max_y + 1): out.write('<tr>') for x in range(board.min_x, board.max_x + 1): cell = board[Position(x, y)] classes = [] content = '' bgcolor = None # styles = [] if y != board.min_y: classes.append(self.HorizontalWalls[cell.wall( Direction.North)]) if x != board.min_x: classes.append(self.VerticalWalls[cell.wall( Direction.West)]) if cell.explored: if cell.explored_by is not None: bgcolor = [*cell.explored_by.color, 0.2] else: classes.append('c-unexp') content = '?' if 'bgcolor' in cell.data: bgcolor = cell.data['bgcolor'] # type: List[int] if len(bgcolor) == 3: bgcolor.append(1) if None not in (self.destination, self.destbot): if x == self.destination.x and y == self.destination.y: content = '★' bgcolor = [*self.destbot.color, 1] for bot in self.bots: if x == bot.pos.x and y == bot.pos.y: content = { Direction.North: '↑', Direction.East: '→', Direction.South: '↓', Direction.West: '←', Direction.Unknown: '¿' }[bot.dir] bgcolor = [*bot.color, 1] out.write('<td class="') out.write(' '.join(classes)) out.write('" style="') if bgcolor: out.write( 'background-color:rgba({},{},{},{})'.format(*bgcolor)) # out.write(';'.join(styles)) out.write('" data-x="{}" data-y="{}">'.format(x, y)) out.write(content) out.write('</td>') out.write('</tr>') out.write('</table>') return out.getvalue()
def get_candidates() -> List[Board.Cell]: ret = [] for y in range(board.min_y, board.max_y + 1): for x in range(board.min_x, board.max_x + 1): cell = board[Position(x, y)] if cell.explored: continue if Wall.No in (cell.wall(Direction.North), cell.wall(Direction.East), cell.wall(Direction.South), cell.wall(Direction.West)): ret.append(cell) return ret
def place_bot(): ctx = get_ctx() x = int(request.args.get('x')) y = int(request.args.get('y')) botname = request.args.get('bot') bot = [b for b in ctx.bots if b.name == botname][0] newpos = Position(x, y) if bot.dir == Direction.Unknown: bot.dir = Direction.North if bot.pos == newpos: bot.dir = bot.dir.apply_relative(RelativeDirection.Right) else: bot.pos = newpos return _render()
def export(self): token = idx(self.board, self.destination) robots = [idx(self.board, bot.pos) for bot in self.bots] grid = [0] * (16 * 16) for y in range(self.board.min_y, self.board.max_y + 1): for x in range(self.board.min_x, self.board.max_x + 1): pos = Position(x, y) cell = self.board[pos] gidx = idx(self.board, pos) grid[gidx] = to_mask(cell) for bot in self.bots: if bot.pos == pos: grid[gidx] |= M_ROBOT robot = self.bots.index(self.destbot) return { 'grid': grid, 'robot': robot, 'token': token, 'robots': robots, }