def get_neighbours(self, coord: Coord): pos_list = list() # type: List[Coord] for adj in Coord.adjacency(): pos = coord + adj if pos in self.cells: pos_list.append(pos) return pos_list
def generate(self, grid: Grid, rand: random.Random): width = grid.width // 2 + 1 height = grid.height // 2 + 1 pieces = dict() # type: Dict[Coord, TetrisPiece] block_origin = dict() # type: Dict[Coord, Coord] occupied = set() # type: Set[Coord] for y in range(height): for x in range(width): pos = Coord(x, y) if pos not in occupied: rand.shuffle(self.pieces) opts = [ p for p in self.pieces if self.piece_fits(p, occupied, pos) ] if len(opts) > 1: self.place_piece(pieces, block_origin, occupied, pos, opts[1]) for y in range(1, height): for x in range(1, width): pos = Coord(x, y) origin = block_origin.get(pos, None) grid_pos = Coord(x, y) * 2 - 1 if origin is not None: piece = pieces.get(origin) block = pos - origin for delta in Coord.adjacency(): adj = block + delta if adj not in piece.blocks: for i in range(3): if delta.x == 0: cell_pos = grid_pos + Coord(i - 1, delta.y) else: cell_pos = grid_pos + Coord(delta.x, i - 1) if cell_pos in grid.cells: cell = grid.get(cell_pos) cell.celltype = Cell.CellType.FLOOR
def get_turn(self, action): adj = Coord.adjacency() if action == 0 or action == 3: return adj[self.direction.value], self.direction if self.direction == Snake.Direction.UP: if action == 2: return adj[Snake.Direction.LEFT.value], Snake.Direction.LEFT else: return adj[Snake.Direction.RIGHT.value], Snake.Direction.RIGHT elif self.direction == Snake.Direction.DOWN: if action == 2: return adj[Snake.Direction.RIGHT.value], Snake.Direction.RIGHT else: return adj[Snake.Direction.LEFT.value], Snake.Direction.LEFT elif self.direction == Snake.Direction.RIGHT: if action == 2: return adj[Snake.Direction.UP.value], Snake.Direction.UP else: return adj[Snake.Direction.DOWN.value], Snake.Direction.DOWN elif self.direction == Snake.Direction.LEFT: if action == 2: return adj[Snake.Direction.DOWN.value], Snake.Direction.DOWN else: return adj[Snake.Direction.UP.value], Snake.Direction.UP
def get_neighbourds(self, coord): return [ coord + adj for adj in Coord.adjacency() if coord + adj in self.cells ]