def can_an_passant(piece, move, board: ChessBoard): left_neighbor_pos = Vec2(move.old.x - 1, move.old.y) right_neighbor_pos = Vec2(move.old.x + 1, move.old.y) global left_neighbor global right_neighbor if move.old.x != 0: left_neighbor = board.get_piece(left_neighbor_pos) if move.old.x != 7: right_neighbor = board.get_piece(right_neighbor_pos) if piece.team == 1: if move.old.y == 3: if left_neighbor is not None: if left_neighbor.name == "Pawn" and left_neighbor.move_counter == 1 and left_neighbor.team != piece.team: an_passant(move, "left", board) return True elif right_neighbor is not None: if right_neighbor.name == "Pawn" and right_neighbor.move_counter == 1 and right_neighbor.team != piece.team: an_passant(move, "right", board) return True elif piece.team == 2: if move.old.y == 4: if left_neighbor is not None: if left_neighbor.name == "Pawn" and left_neighbor.move_counter == 1 and left_neighbor.team != piece.team: an_passant(move, "left", board) return True elif right_neighbor is not None: if right_neighbor.name == "Pawn" and right_neighbor.move_counter == 1 and right_neighbor.team != piece.team: an_passant(move, "right", board) return True return False
def test_add_two_vectors(self): v1 = Vec2(2, 2) v2 = Vec2(3, 3) actual = v1 + v2 expected = Vec2(5, 5) self.assertEqual(actual, expected)
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
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)
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
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")
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
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)
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)
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)
def is_castling(move, board: ChessBoard): king = board.get_piece(move.old) if king.team == 1: if move.new == Vec2(6, 7) or move.new == Vec2(2, 7): return True else: if move.new == Vec2(6, 0) or move.new == Vec2(2, 0): return True return False
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)
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)
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)
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))
def get_all_pieces_on_team(self, player_team): friendly_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: friendly_list.append(attacker) return friendly_list
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)
def test_piece_may_not_move_out_of_bounds(self): # Arrange knight_end_pos = Vec2(-2, -1) # Act is_knight_moving_inbound = self.chess_board.in_board(knight_end_pos) # Assert self.assertFalse(is_knight_moving_inbound)
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())
def test_get_king(self): # Arrange king_start_pos = Vec2(4, 0) self.chess_game.board.set_piece(king_start_pos, self.king) # Act the_king = self.chess_board.get_king(self.player_turn) # Assert self.assertEqual(self.king, the_king)
def setUp(self) -> None: self.player_turn = 1 self.chess_game = ChessGame() self.chess_board = self.chess_game.board self.chess_game.player_turn = self.player_turn self.start_space = Vec2(5, 5) self.king = King(team=self.player_turn) self.knight = Knight(team=self.player_turn) self.pawn = Pawn(team=self.player_turn) self.enemy_pawn = Pawn(team=2) self.p1_pawn = Pawn(team=1)
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)
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)
def can_castle(move, board: ChessBoard): king = board.get_piece(move.old) king.is_castling_move(move, board) if king.is_castling: if not board.is_path_clear(move): return False if king.has_moved: return False if king.team == 1: if move.new == Vec2(6, 7): if board.get_piece(Vec2(7, 7)) is not None: if board.get_piece(Vec2(7, 7)).has_moved is False: return True elif move.new == Vec2(2, 7): if board.get_piece(Vec2(0, 7)) is not None: if board.get_piece(Vec2(0, 7)).has_moved is False: return True else: if move.new == Vec2(6, 0): if board.get_piece(Vec2(7, 0)) is not None: if board.get_piece(Vec2(7, 0)).has_moved is False: return True elif move.new == Vec2(2, 0): if board.get_piece(Vec2(0, 0)) is not None: if board.get_piece(Vec2(0, 0)).has_moved is False: return True # if board.is_space_safe(move.old, king.team): # return False return False return False
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
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))
def get_king(self, player_team): 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.name == "King" and piece.team == player_team: return piece
def get_piece_space(self, piece_to_find) -> Vec2: for y in range(len(self.spaces)): for x in range(len(self.spaces)): if self.get_piece(Vec2(x, y)) == piece_to_find: return Vec2(x, y)
def setup_pieces(self): # Top team for num in range(0, 8): self.board.set_piece(Vec2(num, 1), pieces.Pawn(team=2)) self.board.set_piece(Vec2(0, 0), pieces.Rook(team=2)) self.board.set_piece(Vec2(7, 0), pieces.Rook(team=2)) self.board.set_piece(Vec2(1, 0), pieces.Knight(team=2)) self.board.set_piece(Vec2(6, 0), pieces.Knight(team=2)) self.board.set_piece(Vec2(2, 0), pieces.Bishop(team=2)) self.board.set_piece(Vec2(5, 0), pieces.Bishop(team=2)) self.p2_king = self.board.set_piece(Vec2(3, 0), pieces.King(team=2)) self.board.set_piece(Vec2(4, 0), pieces.Queen(team=2)) # Bottom Team for num in range(0, 8): self.board.set_piece(Vec2(num, 6), pieces.Pawn(team=1)) self.board.set_piece(Vec2(0, 7), pieces.Rook(team=1)) self.board.set_piece(Vec2(7, 7), pieces.Rook(team=1)) self.board.set_piece(Vec2(1, 7), pieces.Knight(team=1)) self.board.set_piece(Vec2(6, 7), pieces.Knight(team=1)) self.board.set_piece(Vec2(2, 7), pieces.Bishop(team=1)) self.board.set_piece(Vec2(5, 7), pieces.Bishop(team=1)) self.p1_king = self.board.set_piece(Vec2(4, 7), pieces.King(team=1)) self.board.set_piece(Vec2(3, 7), pieces.Queen(team=1)) # cool stuff self.board.print_all_moves_for_piece(Vec2(4, 7))
def get_piece_pos(self, piece) -> Vec2: for y in range(len(self.spaces)): for x in range(len(self.spaces)): if self.spaces[y][x] == piece: return Vec2(x, y)