def setUp(self): super().setUp() self.chess_game = ChessGame() self.coords = namedtuple('Coords', 'x y') self.start_coords = self.coords(x=6, y=3) self.black_pawn = Pawn(color='black') self.white_pawn = Pawn(color='white')
def _pawn_two_space_first_move(self): if (self.playing_piece == Pawn(Color.WHITE) and self.from_coords.y == 1 and self.to_coords.y == 3): return True if (self.playing_piece == Pawn(Color.BLACK) and self.from_coords.y == 6 and self.to_coords.y == 4): return True return False
def _is_en_passant(self): if (self.playing_piece.type == 'Pawn' and self.playing_piece.valid_capture() and self.to_board_positon is None): if self.playing_piece.color == Color.WHITE and self.to_coords.y == 5: return self.board[self.to_coords.x][self.to_coords.y - 1] == Pawn(Color.BLACK) if self.playing_piece.color == Color.BLACK and self.to_coords.y == 2: return self.board[self.to_coords.x][self.to_coords.y + 1] == Pawn(Color.WHITE) return False
def _legal_en_passant(self, to_coords): if not self.last_move_pawn: return False if self.playing_piece == Pawn(Color.WHITE) and to_coords.y == 5: return Coords(to_coords.x, to_coords.y - 1) == self.last_move_pawn.coords if self.playing_piece == Pawn(Color.BLACK) and to_coords.y == 2: return Coords(to_coords.x, to_coords.y + 1) == self.last_move_pawn.coords return False
def setUp(self): super().setUp() self.pawn = Pawn(color='black') self.chess_game = ChessGame() self.coords = namedtuple('Coords', 'x y') self.move = self.chess_game.move # Pretend Pawns are generic pieces for testing 'piece blocking move' raises error self.piece = Pawn(color='black') self.blocking_piece = Pawn(color='black')
def chess_pieces(color, *, y_idxs=None): """Helper function for new Chess setup. Return list of [start_coords, pieces] for given color """ coords = [f'{x_idx}{y_idx}' for y_idx in y_idxs for x_idx in range(8)] pieces = [ Rook(color), Knight(color), Bishop(color), Queen(color), King(color), Bishop(color), Knight(color), Rook(color), Pawn(color), Pawn(color), Pawn(color), Pawn(color), Pawn(color), Pawn(color), Pawn(color), Pawn(color) ] return list(zip(coords, pieces))
def test_child_class_inherits_properties(): pawn = Pawn(Color.BLACK) assert pawn.name == 'Pawn' assert pawn.color == Color.BLACK assert pawn.coords is None assert repr(pawn) == "Pawn('Black')"
def _en_passant(self): if (self.playing_piece == Pawn(self.playing_color) and self.playing_piece.legal_capture(self.to_coords) and self.board[self.to_coords.x][self.to_coords.y] is None and self._potential_en_passant_capture_piece()): return True return False
def setUp(self): super().setUp() self.pawn = Pawn(color='black') self.chess_game = ChessGame() self.coords = namedtuple('Coords', 'x y') self.start_coords = self.coords(x=4, y=4) self.chess_game.add(self.pawn, self.start_coords)
def test_that_child_class_inherits_properties(self): pawn = Pawn(color='black') test_repr = "Pawn('black')" test_str = 'Black Pawn: x_coord = None, y_coord = None' assert pawn.type == 'Pawn' assert repr(pawn) == test_repr assert str(pawn) == test_str
def test_that_avoids_duplicate_positioning(self): second_pawn = Pawn(color='black') coords = self.coords(x=6, y=3) self.chess_game.add(self.pawn, coords) self.chess_game.add(second_pawn, coords) assert self.pawn.x_coord == 6 assert self.pawn.y_coord == 3 assert not second_pawn.x_coord assert not second_pawn.y_coord
def test_captured_piece_removed_from_board(self): white_pawn1 = Pawn(color='white') white_pawn2 = Pawn(color='white') color = white_pawn2.color type_ = white_pawn2.type self.chess_game.add(self.pawn, self.coords(x=6, y=6)) self.chess_game.add(white_pawn1, self.coords(x=5, y=5)) self.chess_game.add(white_pawn2, self.coords(x=4, y=4)) assert white_pawn1.x_coord == 5 assert white_pawn1.y_coord == 5 assert self.chess_game.pieces[color][type_] == 2 # Attack white_pawn1 self.chess_game.move(self.coords(x=6, y=6), self.coords(x=5, y=5)) # Previous position empty assert self.chess_game.board[6][6] is None # Captured piece removed and replaced by attacking piece assert self.chess_game.board[5][5] == self.pawn assert self.pawn.x_coord == 5 assert self.pawn.y_coord == 5 # Captured piece no longer on board assert not white_pawn1.x_coord assert not white_pawn1.x_coord assert self.chess_game.pieces[color][type_] == 1 assert white_pawn2.x_coord == 4 assert white_pawn2.y_coord == 4 # Attack white_pawn2 self.chess_game.move(self.coords(x=5, y=5), self.coords(x=4, y=4)) # Previous position empty assert self.chess_game.board[5][5] is None # Captured piece removed and replaced by attacking piece assert self.chess_game.board[4][4] == self.pawn assert self.pawn.x_coord == 4 assert self.pawn.y_coord == 4 # Captured piece no longer on board assert not white_pawn2.x_coord assert not white_pawn2.x_coord assert self.chess_game.pieces[color][type_] == 0
def test_limits_the_number_of_pawns(self): for count in range(10): pawn = Pawn(color='black') row = count / self.chess_game.MAX_BOARD_WIDTH x_coord = count y_coord = count % self.chess_game.MAX_BOARD_WIDTH self.chess_game.add(pawn, self.coords(x=x_coord, y=y_coord)) if row < 1: assert pawn.x_coord == count assert pawn.y_coord == count % self.chess_game.MAX_BOARD_WIDTH else: assert not pawn.x_coord assert not pawn.y_coord
def chess_pieces(color, *, y_idxs=None): """Helper method for new_chess_setup.""" coords = [f'{x_idx}{y_idx}' for y_idx in y_idxs for x_idx in range(8)] pieces = [ Rook(color), Knight(color), Bishop(color), Queen(color), King(color), Bishop(color), Knight(color), Rook(color), Pawn(color), Pawn(color), Pawn(color), Pawn(color), Pawn(color), Pawn(color), Pawn(color), Pawn(color) ] return list(zip(coords, pieces))
def test_illegal_game_color_throws_error(): with pytest.raises(AttributeError): Pawn(Color.GREY) with pytest.raises(ValueError): Pawn('white')
def black_pawn(): """Setup Pawn start coords. Return Pawn""" pawn = Pawn(Color.BLACK) pawn.coords = Coords(x=1, y=6) return pawn
def white_pawn(): """Setup Pawn start coords. Return Pawn""" pawn = Pawn(Color.WHITE) pawn.coords = Coords(x=1, y=1) return pawn
def _prawn_promotion(self): return (self.playing_piece == Pawn(Color.WHITE) and self._black_king_row() or self.playing_piece == Pawn(Color.BLACK) and self._white_king_row())
def test_attacking_piece_of_own_color_raises_exception(game): game.add(Pawn(Color.WHITE), Coords(x=1, y=1)) with pytest.raises(IllegalMoveError, match=game.OWN_PIECE_ATTACK): # White king can't attack white Pawn game.move(Coords(x=0, y=0), Coords(x=1, y=1))
def _potential_en_passant_capture_piece(self): y_coord = self.to_coords.y - 1 if self.playing_color == Color.WHITE else self.to_coords.y + 1 if self.board[self.to_coords.x][y_coord] == Pawn(self.opponent_color): return True return False