def test_pawn_moves(self): """Test of pawn_moves() method.""" assert sorted(PieceMoves.pawn_moves(Position(4, 4), self.game)) == [ Move(Position(4, 4), Position(3, 5)), Move(Position(4, 4), Position(4, 5)), ]
def pawn_moves(pos: Position, game: Game) -> List[Move]: """Return list of <game.turn> pawn moves. pawn moves = positions_under_threat + move forward + en_passant """ # Init move list moves = [] # Check if position under threat occupied with opponent pieces or en_passant possible. for pos_under_threat in PositionsUnderThreat.positions_under_pawn_threat( pos, game.turn, game.board): move = Move(pos, pos_under_threat) if PositionsUnderThreat.is_position_enemy( pos_under_threat, game.turn, game.board) or move in PieceMoves.en_passant_moves( pos, game): moves.append(move) # Check forward move. shift_forward_y = 1 if game.turn == Colour.WHITE else -1 pos_forward = Position(pos.x, pos.y + shift_forward_y) if game.board.is_position_empty(pos_forward): move = Move(pos, pos_forward) moves.append(move) # Check double move forward shift_forward_y = 2 if game.turn == Colour.WHITE else -2 pos_d_forward = Position(pos.x, pos.y + shift_forward_y) if (game.board.is_position_empty(pos_forward) and game.board.is_position_empty(pos_d_forward) and not PieceMoves.is_piece_touched(pos, game)): move = Move(pos, pos_d_forward) moves.append(move) return moves
def castling_moves(pos: Position, game: Game) -> List[Move]: """Return list of <game.turn> castling moves. Check is taking into account!!! """ # Init catling list. castling = [] # Retrieve piece at start position. piece_start = game.board.get_piece(pos) # Retrieve positions under threat (important info for castling). pos_under_threat = PositionsUnderThreat.all_positions_under_threat_for_side( game.turn, game.board) # Check if piece piece at start position King with no threat/check. if (piece_start is not None and piece_start.type == PieceType.KING and not PieceMoves.is_piece_touched(pos, game) and pos not in pos_under_threat): # Short castling. _1r_ means 1 pos to the right from white side. is_1r_pos_avail = ( game.board.is_position_empty(Position(pos.x + 1, pos.y)) and Position(pos.x + 1, pos.y) not in pos_under_threat) is_2r_pos_avail = ( game.board.is_position_empty(Position(pos.x + 2, pos.y)) and Position(pos.x + 2, pos.y) not in pos_under_threat) is_3r_pos_rook = ( not game.board.is_position_empty(Position(pos.x + 3, pos.y)) and game.board.get_piece(Position( pos.x + 3, pos.y)).type == PieceType.ROOK) if (is_1r_pos_avail and is_2r_pos_avail and is_3r_pos_rook and not PieceMoves.is_piece_touched( Position(pos.x + 3, pos.y), game)): move = Move(pos, Position(pos.x + 2, pos.y)) castling.append(move) # Long castling. _1l_ means 1 pos to the left from white side. is_1l_pos_avail = ( game.board.is_position_empty(Position(pos.x - 1, pos.y)) and Position(pos.x - 1, pos.y) not in pos_under_threat) is_2l_pos_avail = ( game.board.is_position_empty(Position(pos.x - 2, pos.y)) and Position(pos.x - 2, pos.y) not in pos_under_threat) is_4l_pos_rook = ( not game.board.is_position_empty(Position(pos.x - 4, pos.y)) and game.board.get_piece(Position( pos.x - 4, pos.y)).type == PieceType.ROOK) if (is_1l_pos_avail and is_2l_pos_avail and is_4l_pos_rook and game.board.is_position_empty(Position( pos.x - 3, pos.y)) and not PieceMoves.is_piece_touched( Position(pos.x - 4, pos.y), game)): move = Move(pos, Position(pos.x - 2, pos.y)) castling.append(move) return castling
def test_is_mate(self): """Test of is_check() method.""" assert not GameLogic.is_mate(self.game) # Create dummy board board = Board() board.set_piece(Position(0, 1), Piece(PieceType.PAWN, Colour.WHITE)) board.set_piece(Position(1, 1), Piece(PieceType.PAWN, Colour.WHITE)) board.set_piece(Position(0, 0), Piece(PieceType.KING, Colour.WHITE)) board.set_piece(Position(2, 0), Piece(PieceType.ROOK, Colour.BLACK)) history_moves = [ Move(Position(4, 3), Position(4, 4)), Move(Position(3, 6), Position(3, 4)), ] game = Game(board, Colour.WHITE, history_moves) assert GameLogic.is_mate(game)
def knight_moves(pos: Position, game: Game) -> List[Move]: """Return list of <game.turn> knight moves. knight moves = positions_under_threat """ moves = [ Move(pos, pos_threat) for pos_threat in PositionsUnderThreat. positions_under_knight_threat(pos, game.turn, game.board) ] return moves
def queen_moves(pos: Position, game: Game) -> List[Position]: """Return list of <game.turn> queen moves. queen moves = positions_under_threat """ moves = [ Move(pos, pos_threat) for pos_threat in PositionsUnderThreat. positions_under_queen_threat(pos, game.turn, game.board) ] return moves
def rook_moves(pos: Position, game: Game) -> List[Move]: """Return list of <game.turn> rook moves. rook moves = positions_under_threat """ moves = [ Move(pos, pos_threat) for pos_threat in PositionsUnderThreat.positions_under_rook_threat( pos, game.turn, game.board) ] return moves
def king_moves(pos: Position, game: Game) -> List[Position]: """Return list of <game.turn> king moves. king moves = positions_under_threat + castling """ moves = [ Move(pos, pos_threat) for pos_threat in PositionsUnderThreat.positions_under_king_threat( pos, game.turn, game.board) ] return [*PieceMoves.castling_moves(pos, game), *moves]
def setUp(self) -> None: """Create dummy game.""" # Create dummy board self.board = Board() self.board.set_piece(Position(4, 0), Pieces.WHITE_KING) self.board.set_piece(Position(0, 0), Pieces.WHITE_ROOK) self.board.set_piece(Position(0, 1), Pieces.WHITE_PAWN) self.board.set_piece(Position(7, 0), Pieces.WHITE_ROOK) self.board.set_piece(Position(7, 1), Pieces.WHITE_PAWN) self.board.set_piece(Position(4, 4), Pieces.WHITE_PAWN) self.board.set_piece(Position(2, 7), Pieces.BLACK_ROOK) self.board.set_piece(Position(3, 4), Pieces.BLACK_PAWN) self.board.set_piece(Position(5, 4), Pieces.BLACK_PAWN) # Create game history_moves = [ Move(Position(4, 3), Position(4, 4)), Move(Position(3, 6), Position(3, 4)), ] self.game = Game(self.board, Colour.WHITE, history_moves)
def test_make_move(self): """Test of make_move() method.""" # Check short castling game = GameLogic.make_move(Move(Position(4, 0), Position(6, 0)), self.game) assert game.turn != self.game.turn assert game.history_moves[-1] == Move(Position(4, 0), Position(6, 0)) assert game.board.get_piece(Position(4, 0)) is None assert game.board.get_piece(Position(5, 0)).type == PieceType.ROOK assert game.board.get_piece(Position(6, 0)).type == PieceType.KING assert game.board.get_piece(Position(7, 0)) is None # Check long castling game = GameLogic.make_move(Move(Position(4, 0), Position(2, 0)), self.game) assert game.turn != self.game.turn assert game.history_moves[-1] == Move(Position(4, 0), Position(2, 0)) assert game.board.get_piece(Position(4, 0)) is None assert game.board.get_piece(Position(3, 0)).type == PieceType.ROOK assert game.board.get_piece(Position(2, 0)).type == PieceType.KING assert game.board.get_piece(Position(1, 0)) is None assert game.board.get_piece(Position(0, 0)) is None # Check en_passant game = GameLogic.make_move(Move(Position(4, 4), Position(3, 5)), self.game) assert game.turn != self.game.turn assert game.history_moves[-1] == Move(Position(4, 4), Position(3, 5)) assert game.board.get_piece(Position(4, 4)) is None assert game.board.get_piece(Position(3, 5)).type == PieceType.PAWN assert game.board.get_piece(Position(3, 5)).colour == Colour.WHITE assert game.board.get_piece(Position(3, 4)) is None
def en_passant_moves(pos: Position, game: Game) -> List[Move]: """Return list of <game.turn> en passant moves. en passant: - opponent pawn makes the move at previous turn - opponent pawn jumps over 2 positions """ # Init list of moves. en_passant = [] # Retrieve start piece. piece_start = game.board.get_piece(pos) # Check if piece_start not empty. if piece_start is not None and piece_start.type == PieceType.PAWN: # 2 directions. shifts = [(-1, 0), (1, 0)] for shift_x, shift_y in shifts: # Retrieve not start piece. piece = game.board.get_piece( Position(pos.x + shift_x, pos.y + shift_y)) if piece is not None and piece.type == PieceType.PAWN: # Retrieve last_move. last_move = game.history_moves[-1] # Check if pawn makes the last move and if it jumps over 2 positions. if (Position(pos.x + shift_x, pos.y + shift_y) == last_move.finish and abs(last_move.start.y - last_move.finish.y) == 2): # Add move in dependence of colour. if game.turn == Colour.WHITE: move = Move(pos, Position(pos.x + shift_x, pos.y + 1)) en_passant.append(move) else: move = Move(pos, Position(pos.x + shift_x, pos.y - 1)) en_passant.append(move) return en_passant
def test_king_moves(self): """Test of king_moves() method.""" assert sorted(PieceMoves.king_moves(Position(4, 0), self.game)) == [ Move(Position(4, 0), Position(3, 0)), Move(Position(4, 0), Position(3, 1)), Move(Position(4, 0), Position(4, 1)), Move(Position(4, 0), Position(5, 0)), Move(Position(4, 0), Position(5, 1)), Move(Position(4, 0), Position(6, 0)), ]
def setUp(self) -> None: """Create dummy game.""" # Create dummy board self.board = Board() self.board.set_piece(Position(4, 0), Pieces.WHITE_KING) self.board.set_piece(Position(0, 0), Pieces.WHITE_ROOK) self.board.set_piece(Position(7, 0), Pieces.WHITE_ROOK) self.board.set_piece(Position(4, 4), Pieces.WHITE_PAWN) self.board.set_piece(Position(4, 1), Pieces.WHITE_BISHOP) self.board.set_piece(Position(3, 4), Pieces.BLACK_PAWN) self.board.set_piece(Position(5, 4), Pieces.BLACK_PAWN) self.board.set_piece(Position(4, 3), Pieces.BLACK_ROOK) self.board.set_piece(Position(2, 2), Pieces.BLACK_PAWN) # Create game self.game = Game(self.board, Colour.WHITE, [Move(Position(3, 6), Position(3, 4))])
def test_is_move_possible(self): """Test of is_move_possible() method.""" assert not GameLogic.is_move_possible( self.game, Move(Position(4, 1), Position(3, 2))) assert not GameLogic.is_move_possible( self.game, Move(Position(4, 0), Position(3, 1))) assert not GameLogic.is_move_possible( self.game, Move(Position(4, 0), Position(4, -1))) assert not GameLogic.is_move_possible( self.game, Move(Position(2, 2), Position(2, 1))) assert GameLogic.is_move_possible(self.game, Move(Position(4, 0), Position(5, 1))) assert GameLogic.is_move_possible(self.game, Move(Position(4, 0), Position(2, 0))) assert GameLogic.is_move_possible(self.game, Move(Position(4, 0), Position(6, 0)))
def test_castling_moves(self): """Test of castling_moves() method.""" assert sorted(PieceMoves.castling_moves(Position( 4, 0), self.game)) == [Move(Position(4, 0), Position(6, 0))]
def test_all_moves(self): """Test of all_moves() method.""" assert sorted(PieceMoves.all_moves(self.game)) == [ Move(Position(0, 0), Position(1, 0)), Move(Position(0, 0), Position(2, 0)), Move(Position(0, 0), Position(3, 0)), Move(Position(0, 1), Position(0, 2)), Move(Position(0, 1), Position(0, 3)), *sorted(PieceMoves.king_moves(Position(4, 0), self.game)), Move(Position(4, 4), Position(3, 5)), Move(Position(4, 4), Position(4, 5)), Move(Position(7, 0), Position(5, 0)), Move(Position(7, 0), Position(6, 0)), Move(Position(7, 1), Position(7, 2)), Move(Position(7, 1), Position(7, 3)), ]
def test_en_passant_moves(self): """Test of en_passant_moves() method.""" assert sorted(PieceMoves.en_passant_moves(Position( 4, 4), self.game)) == [Move(Position(4, 4), Position(3, 5))]