Exemplo n.º 1
0
    def test_pawn_may_not_double_jump_after_moving(self):
        # Arrange
        pawn_start_pos = Vec2(0, 6)

        # Act
        self.chess_board.move(Move(pawn_start_pos, Vec2(0, 3)))
        pawn_move_to_test = self.p1_pawn.can_move(Move(pawn_start_pos, Vec2(5, 3)), self.chess_board)

        # Assert
        self.assertFalse(pawn_move_to_test)
Exemplo n.º 2
0
    def castle(self, move):
        # Utilizing normalization to determine King or Queen side castling
        new_king_pos = move.old + move.direction() * 2
        new_rook_pos = new_king_pos - move.direction()

        # Fetching the rook via Kingly movement
        rook_x = 7 if new_rook_pos.x > move.old.x else 0

        # Moves to do
        king_move = Move(move.old, new_king_pos)
        rook_move = Move(Vec2(rook_x, new_rook_pos.y), new_rook_pos)

        # The actual castling
        self.move(king_move)
        self.move(rook_move)
Exemplo n.º 3
0
    def can_friendlies_uncheck_king(self) -> bool:

        friendlies = self.board.get_all_pieces_on_team(self.highness.team)

        checkers = self.board.get_all_pieces_checking_king(
            self.highness.team, self.board.get_piece_space(self.highness))

        if len(checkers) > 1:
            return False
        elif len(checkers) == 0:
            return True
        else:
            checker = checkers[0]
            if friendlies:
                for friendly in friendlies:
                    move_kill = Move(self.board.get_piece_pos(friendly),
                                     self.board.get_piece_pos(checker))
                    if friendly.can_move(
                            move_kill,
                            self.board) or self.can_friendlies_block_checker(
                                friendlies, checker):
                        print(f"Checker may be captured or blocked")
                        if friendly.can_move(
                                move_kill, self.board
                        ) and self.can_friendlies_block_checker(
                                friendlies, checker):
                            print(
                                f"Checker may be captured via {move_kill}, and blocked via {self.can_friendlies_block_checker(friendlies, checker)}"
                            )
                        return True
            else:
                return False
Exemplo n.º 4
0
    def is_castle_path_clear(self, move: Move, player_team):
        spaces_in_between = move.get_spaces_in_between()

        for space in spaces_in_between:
            if not self.is_space_safe(space, player_team):
                return False

        return True
Exemplo n.º 5
0
    def test_pawn_may_double_jump(self):
        # Arrange
        pawn_start_pos = Vec2(5, 5)

        # Act
        pawn_move = self.pawn.can_move(Move(pawn_start_pos, Vec2(5, 3)), self.chess_board)

        # Assert
        self.assertTrue(pawn_move)
Exemplo n.º 6
0
 def get_attacker_spaces_for_checkmate(self, attacker_piece,
                                       attacker_pos) -> List[Vec2]:
     move_list = []
     for y in range(len(self.spaces)):
         for x in range(len(self.spaces)):
             if attacker_piece.can_move(Move(attacker_pos, Vec2(x, y)),
                                        self):
                 move_list.append(Vec2(x, y))
     return move_list
Exemplo n.º 7
0
 def get_all_pieces_checking_king(self, player_team, king_pos) -> List:
     hit_list = []
     for y in range(len(self.spaces)):
         for x in range(len(self.spaces)):
             attacker = self.get_piece(Vec2(x, y))
             if attacker is not None and attacker.team != player_team:
                 if attacker.can_move((Move(Vec2(x, y), king_pos)), self):
                     hit_list.append(attacker)
     return hit_list
Exemplo n.º 8
0
    def test_new_coordinates_via_pawn_double_jump(self):
        # Arrange
        self.chess_board.set_piece(Vec2(0, 2), self.enemy_pawn)

        # Act
        self.chess_board.move(Move(Vec2(0, 2), Vec2(0, 4)))

        # Assert
        self.assertTrue(self.chess_board.get_piece(Vec2(0, 4)).name == "Pawn")
Exemplo n.º 9
0
 def is_space_safe(self, pos: Vec2, for_team: int) -> bool:
     for y in range(len(self.spaces)):
         for x in range(len(self.spaces)):
             piece = self.get_piece(Vec2(x, y))
             if piece and piece.team != for_team:
                 if piece.can_move(Move(Vec2(x, y), pos), self):
                     # print(f"{piece} can take {pos}")
                     return False
     return True
Exemplo n.º 10
0
    def test_pawn_cannot_move_backwards(self):
        # Arrange
        end_space = self.start_space + Vec2(0, 1)
        move = Move(self.start_space, end_space)

        self.chess_game.board.set_piece(self.start_space, self.pawn)

        # Act & Assert
        self.assertRaises(IllegalMove, self.chess_game.try_player_move, move, self.player_turn)
Exemplo n.º 11
0
    def print_all_moves_for_piece(self, pos):
        a_board = ChessBoard()
        piece = self.get_piece(pos)

        for y in range(len(self.spaces)):
            for x in range(len(self.spaces)):
                if piece.can_move(Move(pos, Vec2(x, y)), self):
                    a_board.spaces[y][x] = "x"

        print(a_board.display())
Exemplo n.º 12
0
    def test_king_cannot_endanger_himself(self):
        # Arrange
        king_start_pos = Vec2(4, 7)
        self.chess_game.board.set_piece(king_start_pos, self.king)
        self.chess_game.board.set_piece(Vec2(5, 5), self.enemy_pawn)

        # Act
        king_move = self.king.can_move(Move(king_start_pos, Vec2(4, 2)), self.chess_board)

        # Assert
        self.assertFalse(king_move)
Exemplo n.º 13
0
    def test_can_king_free_himself_if_checked(self):
        # Arrange
        king_start_pos = Vec2(4,7)
        self.chess_game.board.set_piece(king_start_pos, self.king)
        self.chess_game.board.set_piece(Vec2(5, 6), self.enemy_pawn)

        # Act
        king_move = self.king.can_move(Move(king_start_pos, Vec2(3, 7)), self.chess_board)

        # Assert
        self.assertTrue(king_move)
Exemplo n.º 14
0
    def test_king_cannot_over_move_For_castle_right(self):
        # Arrange
        king_start_pos = Vec2(4, 7)
        rook_start_pos = Vec2(7, 7)
        self.chess_game.board.set_piece(king_start_pos, self.king)
        self.chess_game.board.set_piece(rook_start_pos, Rook(team=self.player_turn))

        # Act
        castle_move = self.king.can_move(Move(king_start_pos, Vec2(7, 7)), self.chess_board)

        # Assert
        self.assertFalse(castle_move)
Exemplo n.º 15
0
    def test_pawn_can_move_one_space_forward(self):
        # Arrange
        end_space = self.start_space + Vec2(0, -1)
        move = Move(self.start_space, end_space)

        self.chess_game.board.set_piece(self.start_space, self.pawn)

        # Act
        self.chess_game.try_player_move(move, self.player_turn)

        # Assert
        self.assertEqual(self.chess_game.board.get_piece(end_space), self.pawn)
Exemplo n.º 16
0
    def test_king_may_castle_left(self):
        # Arrange
        king_start_pos = Vec2(4, 7)
        rook_start_pos = Vec2(0, 7)
        self.chess_game.board.set_piece(king_start_pos, self.king)
        self.chess_game.board.set_piece(rook_start_pos, Rook(team=self.player_turn))

        # Act
        castle_move = self.king.can_move(Move(king_start_pos, Vec2(2, 7)), self.chess_board)

        # Assert
        self.assertTrue(castle_move)
Exemplo n.º 17
0
    def test_horse_may_jump(self):
        # Arrange
        knight_start_pos = Vec2(5, 5)
        self.chess_game.board.set_piece(knight_start_pos, self.knight)
        self.chess_game.board.set_piece(Vec2(5, 4), self.pawn)
        self.chess_game.board.set_piece(Vec2(4, 4), self.king)

        # Act
        knight_jump = self.knight.can_move(Move(knight_start_pos, Vec2(3, 4)), self.chess_board)

        # Assert
        self.assertTrue(knight_jump)
Exemplo n.º 18
0
    def test_knight_border_portal(self):
        # Arrange
        knight = Knight(team=1)

        knight_starting_space = Vec2(1, 0)
        knight_destination_space = Vec2(0, 2)

        knight_move = Move(knight_starting_space, knight_destination_space)

        self.chess_game.board.set_piece(knight_starting_space, knight)

        # Act & Assert
        self.assertTrue(knight.can_move(knight_move, self.chess_board))
Exemplo n.º 19
0
    def is_path_clear(self, move: Move):
        # Using move's normalization to determine direction ((-1 to 1), (-1 to 1))
        spaces_in_between = move.get_spaces_in_between()
        # print([str(x) for x in spaces_in_between])

        # Checking intermediate spaces via normalized Vec2
        for space in spaces_in_between:
            if not self.is_unoccupied(space):
                # Blocked
                return False

        # Not blocked
        return True
Exemplo n.º 20
0
 def can_friendlies_block_checker(self, friendlies, checker) -> bool:
     checker_pos = self.board.get_piece_pos(checker)
     checking_path = self.board.get_attacker_spaces_for_checkmate(
         checker, checker_pos)
     if len(checking_path) > 0:
         for friendly in friendlies:
             friendly_pos = self.board.get_piece_pos(friendly)
             for pos in checking_path:
                 move = Move(friendly_pos, pos)
                 if friendly.can_move(move, self.board):
                     if not self.board.will_king_check(
                             move, self.highness.team,
                             self.board.get_piece_pos(self.highness)):
                         if friendly.name != "King":
                             print(f"Checker may be blocked via {move}")
                             return True
     else:
         print("No check path (Knight?)")
         return False
Exemplo n.º 21
0
def move_from_string(string):
    cur_x, cur_y, new_x, new_y = [
        int(move_string_dict[x.upper()]) for x in string
    ]
    return Move(Vec2(cur_x, cur_y), Vec2(new_x, new_y))
Exemplo n.º 22
0
 def can_piece_move(self, piece, piece_pos):
     for y in range(len(self.spaces)):
         for x in range(len(self.spaces)):
             if piece.can_move((Move(piece_pos, Vec2(x, y))), self):
                 return True
     return False