def is_valid_move(self, move: Move, rubrics): """given a move and the rubrics, returns True if the move is valid for this piece or throws exception""" # rook can move in straight lines: either up, down, left or right. # it cant jump over other pieces. source_x, source_y = move.from_pos.x, move.from_pos.y dest_x, dest_y = move.to_pos.x, move.to_pos.y if source_x != dest_x and source_y != dest_y: raise InvalidMoveException("rook cant move on both axes at once") # calculate the actual path taken to ensure we're not jumping over other pieces, which is illegal path = [] if dest_x > source_x: # move right for i in range(source_x + 1, dest_x, 1): path.append(Position(i, source_y)) elif dest_x < source_x: # move left for i in range(source_x - 1, dest_x, -1): path.append(Position(i, source_y)) elif dest_y > source_y: # move up for i in range(source_y + 1, dest_y, 1): path.append(Position(dest_x, i)) else: # move down for i in range(source_y - 1, dest_y, -1): path.append(Position(dest_x, i)) # ensure there's nothing in our path: for rubric in path: if rubrics[rubric.x][rubric.y].piece_type != PieceType.PLACEHOLDER: raise InvalidMoveException( "cant jump over piece in position %s,%s" % (rubric.x, rubric.y)) return True
def __init__(self, x, y, theta, std_actuators): self._std_actuators = std_actuators self._position = Position(x, y, theta) self._noisy_position = Position(x, y, theta) self._delta_w = .3 self._delta_v = 1 self._w = 0 self._v = 0
def __init__(self, map_name="level1", spawn_name="spawn1"): pygame.init() self.FPS = 60 self.BG_COLOR = pygame.Color("black") self.GAME_AREA_POS = Position(96, 32) self.GAME_AREA_SIZE = (15, 15) self.GAME_AREA_SIZE_PIXELS = (self.GAME_AREA_SIZE[0] * 16, self.GAME_AREA_SIZE[1] * 16) self.screen = pygame.display.set_mode( (self.GAME_AREA_SIZE_PIXELS[0] + self.GAME_AREA_POS.x, self.GAME_AREA_SIZE_PIXELS[1] + self.GAME_AREA_POS.y), pygame.SCALED) self.clock = pygame.time.Clock() self.hero = Hero() self.wait = False # Wait for something to end, stop character updates self.actions = [] # Map data self.map_surface = pygame.Surface(self.GAME_AREA_SIZE_PIXELS) self.tmx_data = None self.map_data = None self.map_layer = None self.map_sprite_group = None self.spawns = None self.impassables = None self.doors = None self.load_map(map_name, spawn_name)
def list_next_potential_positions(self, rubrics): """returns a list of potential and valid next positions for this piece, ignoring Check-semantics""" valid_positions = [] x = self.position.x y = self.position.y coordinates = [(x + 1, y), (x - 1, y), (x, y - 1), (x, y + 1), (x + 1, y + 1), (x - 1, y + 1), (x + 1, y - 1), (x - 1, y - 1)] positions_on_board = [] # filter out off-board positions (for pieces on the edge of the board) for coordinate in coordinates: try: positions_on_board.append( Position(coordinate[0], coordinate[1])) except Position.InvalidPositionError: pass # filter in vacant positions and positions belonging to the other side for position in positions_on_board: target_piece = rubrics[position.x][position.y] if target_piece.piece_type == PieceType.PLACEHOLDER: valid_positions.append(target_piece.position) elif target_piece.color != self.color: valid_positions.append(target_piece.position) return valid_positions
def list_to_chunks(l): chunks = {} for item in l: pos = Position(item[0], item[1]) chunk = Chunk.from_string(item[2]) chunks[pos] = chunk return chunks
def estimate_position(self): """ Compute the barycenter of all the particles Return its position and the covariance in x and y """ N = self._nb_of_particles x_array = [p._position._x for p in self._particles] y_array = [p._position._y for p in self._particles] covariance = np.cov(x_array,y_array) return(Position(np.sum(x_array)/N,np.sum(y_array)/N,0),covariance)
def is_valid_move(self, move: Move, rubrics): """given a move and the rubrics, returns True if the move is valid for this piece or throws exception""" # bishop can move in one of the four diagonals # it cant jump over other pieces. source_x, source_y = move.from_pos.x, move.from_pos.y dest_x, dest_y = move.to_pos.x, move.to_pos.y if abs(dest_x - source_x) != abs(dest_y - source_y): raise InvalidMoveException("bishop can only move diagonally") # calculate the actual path taken to ensure we're not jumping over other pieces, which is illegal path = [] distance = abs(dest_x - source_x + 1) if dest_x > source_x: # moving right if dest_y > source_y: # moving up for i in range(1, distance): path.append(Position(source_x + i, source_y + i)) else: # moving down for i in range(1, distance): path.append(Position(source_x + i, source_y - i)) else: # moving left if dest_y > source_y: # moving up for i in range(1, distance): path.append(Position(source_x - i, source_y + i)) else: # moving down for i in range(1, distance): path.append(Position(source_x - i, source_y - i)) # ensure there's nothing in our path: for rubric in path: if rubrics[rubric.x][rubric.y].piece_type != PieceType.PLACEHOLDER: raise InvalidMoveException( "cant jump over piece in position %s,%s" % (rubric.x, rubric.y)) return True
def _print_chunks(self): """ Meant for debugging. """ if self._chunks: minx, maxx, miny, maxy = self._get_min_max() sizex, sizey = maxx - minx + 1, maxy - miny + 1 print("┌" + "─"*sizex*2 + "┐") for y in range(miny, maxy + 1): line = [] for x in range(minx, maxx + 1): chunk = self._chunks.get(Position(x, y)) if chunk: if chunk.empty(): line.append("()") else: line.append("[]") else: line.append(" ") line = "".join(line) print("│" + line + "│") print("└" + "─"*sizex*2 + "┘")
def process(self, target, vertices): """ PURPOSE calculate mouse pointer position, on Target monitor, from Target monitor location on `camera` sensor PARAMETERS * vertices : vertices of Target monitor on `camera` sensor (a `Quadrilateral` instance) RETURNS * nothing NOTES * Upon (successful) completion of this procedure, `self.position` ia the `Position` instance the mouse pointer should be (moved to) on the Target monitor. """ self.target = target self.vertices = vertices self.position = None vertices = numpy.float32([ vertices.upperleft, vertices.upperright, vertices.lowerleft, vertices.lowerright ]) # vertices orderd as expected by cv2.getPerspectiveTransform transform = cv2.getPerspectiveTransform(vertices, self.monitor) xy = cv2.perspectiveTransform(self.center[None, None, :], transform) self.position = Position(x=int(xy[0, 0, 0]), y=int(xy[0, 0, 1])) self.log.debug(' computed position is: %s', str(self.position))
def __init__(self, stream): super().__init__() reader = csv.reader(stream) # Each row should have the constellation abbreviation and # then alternating ra,dec columns for each position along # the line. Each row corrosponds to one line. for row in reader: # Add the constellation to the catalog if its not already in # it. if row[0] not in self: self[row[0]] = Constellation(row[0], CONSTELLATION_NAMES[row[0]]) # Create a single line for this row with positions from the # row. line = Line() for ra, dec in itertools.zip_longest(*([iter(row[1:])] * 2)): ra_coord = EquatorialCoordinate(ra, hours=True) dec_coord = EquatorialCoordinate(dec, degrees=True) line.positions.append(Position(ra_coord, dec_coord)) self[row[0]].lines.append(line)