def get_possible_moves(self): moves = generate_diagonal_moves(self, self.game.board, False) + generate_straight_moves( self, self.game.board, False) if not self.has_moved(): h_rook = self.game.find_piece_at(8, self.y) if h_rook and not h_rook.has_moved(): f_piece = self.game.find_piece_at(6, self.y) g_piece = self.game.find_piece_at(7, self.y) if not f_piece and not g_piece: moves.append( PieceMove(self, 7, self.y, None, PieceMove(h_rook, 6, self.y))) a_rook = self.game.find_piece_at(1, self.y) if a_rook and not a_rook.has_moved(): b_piece = self.game.find_piece_at(2, self.y) c_piece = self.game.find_piece_at(3, self.y) d_piece = self.game.find_piece_at(4, self.y) if not b_piece and not c_piece and not d_piece: moves.append( PieceMove(self, 3, self.y, None, PieceMove(a_rook, 4, self.y))) return moves
def generate_offset_moves( piece: BasePiece, all_pieces: Iterable[BasePiece], offsets: Iterable[Coord], repeat: bool = False, ): moves = [] for offset in offsets: x = piece.x y = piece.y encountered_piece = None while 1 <= x <= 8 and 1 <= y <= 8 and not encountered_piece: x += offset.x y += offset.y for maybe_encountered in all_pieces: if is_piece_at(maybe_encountered, x, y): encountered_piece = maybe_encountered if not encountered_piece: moves.append(PieceMove(piece, x, y)) if not repeat: break if encountered_piece and encountered_piece.color != piece.color: moves.append(PieceMove(piece, x, y, encountered_piece)) return moves
def get_right_take(self): y = self.y + 1 * self.direction x = self.x - 1 piece = self.game.find_piece_at(x, y) if piece and self.is_enemy(piece): return PieceMove(self, x, y, piece)
def get_two_forward(self): if self.has_moved(): return y1 = self.y + 1 * self.direction y2 = self.y + 2 * self.direction piece1 = self.game.find_piece_at(self.x, y1) piece2 = self.game.find_piece_at(self.x, y2) if not piece1 and not piece2: return PieceMove(self, self.x, y2)
def get_right_en_passant(self): en_passant_y = 5 if self.color == PlayerColor.WHITE else 4 if self.y != en_passant_y: return y = self.y + 1 * self.direction x = self.x - 1 piece = self.game.find_piece_at(x, self.y) if (piece and piece.move_count == 1 and piece.is_enemy(self) and piece.type == BasePiece.Type.PAWN): return PieceMove(self, x, y, piece)
def receive(self, action_tuple): if len(action_tuple) != 2: logger.error("Invalid message, skipping") return action = ClientAction.get_value(action_tuple[0]) data = action_tuple[1] if action == ClientAction.IDENTIFY: user_id = data.get("id") self.game.identify(self.websocket, user_id) if action == ClientAction.SETTING: total_length = int(data.get("total_length", 5)) per_move = int(data.get("per_move", 3)) self.game.set_mode(total_length, per_move) if action == ClientAction.CONNECT: user_id = data.get("id") self.game.connect(self.websocket, user_id) if action == ClientAction.MOVE: move = PieceMove.from_dict(data, self.game) self.game.move(self.websocket, move)
def get_one_forward(self): y = self.y + 1 * self.direction piece = self.game.find_piece_at(self.x, y) if not piece: return PieceMove(self, self.x, y)