def init_board(self): new_b = [] for i in range(64): # white if i == 0 or i == 7: new_b.append(Tile(Rook('W'))) elif i == 1 or i == 6: new_b.append(Tile(Knight('W'))) elif i == 2 or i == 5: new_b.append(Tile(Bishop('W'))) elif i == 3: new_b.append(Tile(Queen('W'))) elif i == 4: new_b.append(Tile(King('W'))) elif i >= 8 and i <= 15: new_b.append(Tile(Pawn('W'))) # black elif i == 56 or i == 63: new_b.append(Tile(Rook('B'))) elif i == 57 or i == 62: new_b.append(Tile(Knight('B'))) elif i == 58 or i == 61: new_b.append(Tile(Bishop('B'))) elif i == 59: new_b.append(Tile(Queen('B'))) elif i == 60: new_b.append(Tile(King('B'))) elif i >= 48 and i <= 55: new_b.append(Tile(Pawn('B'))) # empty else: new_b.append(Tile()) return new_b
def fill_board(self): for key in self.__letter_mapping: self[key + '7'] = Pawn(BLACK, self) self[key + '2'] = Pawn(WHITE, self) self['A1'] = Rook(WHITE, self) self['H1'] = Rook(WHITE, self) self['A8'] = Rook(BLACK, self) self['H8'] = Rook(BLACK, self) self['B1'] = Knight(WHITE, self) self['G1'] = Knight(WHITE, self) self['B8'] = Knight(BLACK, self) self['G8'] = Knight(BLACK, self) self['C1'] = Bishop(WHITE, self) self['F1'] = Bishop(WHITE, self) self['C8'] = Bishop(BLACK, self) self['F8'] = Bishop(BLACK, self) self['D1'] = Queen(WHITE, self) self['D8'] = Queen(BLACK, self) self['E1'] = King(WHITE, self) self['E8'] = King(BLACK, self)
def test_knight_attacks(self): knight1 = Knight(Colour.WHITE, (4, 4)) pawn1 = Pawn(Colour.WHITE, (3, 6)) pawn2 = Pawn(Colour.BLACK, (3, 2)) pawn3 = Pawn(Colour.BLACK, (3, 3)) self.assertEquals(set(knight1.list_attacks([pawn1, pawn2, pawn3])), set([(3, 2)]))
def add_pieces(self): """ Appends the proper pieces and locations to each team list. Team lists are used to get the possible moves and easily check the capture status of a piece. """ self.wp.append(King(4, 7, True)) self.wp.append(Queen(3, 7, True)) self.wp.append(Rook(0, 7, True)) self.wp.append(Rook(7, 7, True)) self.wp.append(Knight(1, 7, True)) self.wp.append(Knight(6, 7, True)) self.wp.append(Bishop(2, 7, True)) self.wp.append(Bishop(5, 7, True)) for i in range(8): self.wp.append(Pawn(i, 6, True)) self.bp.append(King(4, 0, False)) self.bp.append(Queen(3, 0, False)) self.bp.append(Rook(0, 0, False)) self.bp.append(Rook(7, 0, False)) self.bp.append(Knight(1, 0, False)) self.bp.append(Knight(6, 0, False)) self.bp.append(Bishop(2, 0, False)) self.bp.append(Bishop(5, 0, False)) for i in range(8): self.bp.append(Pawn(i, 1, False))
def test_bishop_attacks(self): bishop1 = Bishop(Colour.WHITE, (3, 3)) pawn1 = Pawn(Colour.WHITE, (2, 2)) pawn2 = Pawn(Colour.BLACK, (4, 4)) pawn3 = Pawn(Colour.BLACK, (5, 5)) self.assertEquals(set(bishop1.list_attacks([pawn1, pawn2, pawn3])), set([((4, 4))]))
def __init__(self): self.board = [[None for j in range(8)] for i in range(8)] self.pieces = [] # set white board pieces = [] for i in range(8): pieces.append(Pawn(i, 1, COLOR.WHITE)) pieces.append(Rook(0, 0, COLOR.WHITE)) pieces.append(Rook(7 ,0, COLOR.WHITE)) pieces.append(Knight(1, 0, COLOR.WHITE)) pieces.append(Knight(6, 0, COLOR.WHITE)) pieces.append(Bishop(2, 0, COLOR.WHITE)) pieces.append(Bishop(5, 0, COLOR.WHITE)) pieces.append(King(3, 0, COLOR.WHITE)) pieces.append(Queen(4, 0, COLOR.WHITE)) #set black peices for i in range(8): pieces.append(Pawn(i, 6, COLOR.BLACK)) pieces.append(Rook(0, 7, COLOR.BLACK)) pieces.append(Rook(7 ,7, COLOR.BLACK)) pieces.append(Knight(1, 7, COLOR.BLACK)) pieces.append(Knight(6, 7, COLOR.BLACK)) pieces.append(Bishop(2, 7, COLOR.BLACK)) pieces.append(Bishop(5, 7, COLOR.BLACK)) pieces.append(King(3, 7, COLOR.BLACK)) pieces.append(Queen(4, 7, COLOR.BLACK)) for _piece in pieces: self.pieces.append(_piece) self.board[_piece._x][_piece._y] = _piece
def test_translate_y_should_move_the_pawn_to_the_bottom(self): # Given expected = Pawn(0, 5, Orientation.WEST) # When actual = translate_y(Pawn(0, 4, Orientation.WEST), 1) # Then self.assertEqual(actual, expected, "The pawn should be placed one square on the bottom")
def test_translate_x_should_move_the_pawn_to_the_right(self): # Given expected = Pawn(1, 4, Orientation.WEST) # When actual = translate_x(Pawn(0, 4, Orientation.WEST), 1) # Then self.assertEqual(actual, expected, "The pawn should be placed one square after")
def test_king_moves(self): king1 = King(Colour.WHITE, (4, 4)) pawn1 = Pawn(Colour.WHITE, (4, 5)) pawn2 = Pawn(Colour.WHITE, (4, 3)) pawn3 = Pawn(Colour.WHITE, (3, 3)) pawn4 = Pawn(Colour.WHITE, (5, 5)) self.assertEquals(set(king1.list_moves([pawn1, pawn2, pawn3, pawn4])), set([(3, 5), (5, 3), (5, 4), (3, 4)]))
def test_act_should_move_the_pawn_one_square_to_the_right(self): # Given pawn = Pawn(0, 8, Orientation.WEST) expected = Pawn(2, 8, Orientation.WEST) # When actual = game.act(Action.RIGHT, pawn) # Then self.assertEqual(actual, expected, "The pawn should move one square to the right")
def test_pawn_moves(self): pawn1 = Pawn(Colour.WHITE, (0, 0)) pawn2 = Pawn(Colour.BLACK, (0, 1)) pawn3 = Pawn(Colour.BLACK, (0, 0)) self.assertEquals(pawn1.list_moves([]), [(0, 1), (0, 2)]) pawn1.position = (0, 0) self.assertEquals(pawn1.list_moves([]), [(0, 1)]) self.assertEquals(pawn1.list_moves([pawn2]), []) self.assertEquals(pawn3.list_moves([]), [])
def test_attack_range(self): pawn1 = Pawn(("a", 2), "white") pawn2 = Pawn(("b", 3), "black") pawn3 = Pawn(("d", 5), "white") assert pawn1.attack_range(pawn2) == True assert pawn2.attack_range(pawn1) == True with self.assertRaises(ValueError): pawn1.attack_range(pawn3)
def test_act_should_move_the_pawn_one_square_to_the_right(self): # Given pawn = Pawn(2, 8, Orientation.WEST) fences = [[Direction.NO for i in range(FENCE_SIZE)] for j in range(FENCE_SIZE)] expected = Pawn(0, 8, Orientation.WEST) # When actual = game.act(Action.LEFT, pawn, fences) # Then self.assertEqual(actual, expected, "The pawn should move one square to the left")
def test_attack(self): pawn1 = Pawn(("b", 2), "white") pawn2 = Pawn(("c", 3), "black") pawn3 = Pawn(("a", 3), "white") assert pawn1.attack(pawn2) == None assert pawn1.location == ("c", 3) assert pawn2.location == None assert pawn2.captured == True assert pawn1.captured == False with self.assertRaises(ValueError): pawn1.attack(pawn3)
def test_init_game_should_add_the_pawn_in_the_center_of_the_base_line( self): # Given expected = [ Pawn(0, 8, Orientation.WEST), Pawn(16, 8, Orientation.EAST) ] # When actual = game.init_game() # Then self.assertEqual( actual, expected, "The pawn should be placed in the center of his base line")
def setBoard(self, fen): row, column = 0, 0 for character in fen: if character == 'R': self.addPiece(Rook(column, row, 'w', self.board.board)) column += 1 elif character == 'r': self.addPiece(Rook(column, row, 'b', self.board.board)) column += 1 elif character == 'N': self.addPiece(Knight(column, row, 'w', self.board.board)) column += 1 elif character == 'n': self.addPiece(Knight(column, row, 'b', self.board.board)) column += 1 elif character == 'B': self.addPiece(Bishop(column, row, 'w', self.board.board)) column += 1 elif character == 'b': self.addPiece(Bishop(column, row, 'b', self.board.board)) column += 1 elif character == 'P': self.addPiece(Pawn(column, row, 'w', self.board.board)) column += 1 elif character == 'p': self.addPiece(Pawn(column, row, 'b', self.board.board)) column += 1 elif character == 'K': self.addPiece(King(column, row, 'w', self.board.board)) column += 1 elif character == 'k': self.addPiece(King(column, row, 'b', self.board.board)) column += 1 elif character == 'Q': self.addPiece(Queen(column, row, 'w', self.board.board)) column += 1 elif character == 'q': self.addPiece(Queen(column, row, 'b', self.board.board)) column += 1 elif character == '/': column = 0 row += 1 else: if character >= '1' and character <= '9': column += int(character) elif character == ' ': i = fen.index(character) + 1 self.whitesTurn = True if fen[i] == 'w' else False return
def test_pawn_init(self): loc = ("a", 1) pawn = Pawn(loc, "white") assert pawn.location == ("a", 1) assert pawn.has_moved == False assert pawn.board == None assert pawn.value == 1
def test_get_board_should_add_the_pawn_in_the_top_right_of_the_board(self): # Given pawns = [Pawn(16, 0, Orientation.WEST)] fences = [[Direction.NO for i in range(FENCE_SIZE)] for j in range(FENCE_SIZE)] expected = [[2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 3], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2]] # When actual = get_board(pawns, fences) # Then self.assertEqual( actual, expected, "In the board, the pawn should be placed in the top right of the board" )
def test_get_board_should_add_the_pawn_in_the_center_of_the_base_line( self): # Given pawns = [Pawn(0, 8, Orientation.WEST)] fences = [[Direction.NO for i in range(FENCE_SIZE)] for j in range(FENCE_SIZE)] expected = [[2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [3, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2]] # When actual = get_board(pawns, fences) # Then self.assertEqual( actual, expected, "In the board, the pawn should be placed in the center of his base line" )
def putBlackPawns(self,black_pawns_list): for i in range(8): sq = self.squares[6][i] new_spot = self.chess_coord[sq] new_pawn = Pawn(new_spot, False) new_pawn.ficha.setpos(new_pawn.spot) black_pawns_list.append(new_pawn)
def putWhitePawns(self,white_pawns_list): for i in range(8): sq = self.squares[1][i] new_spot = self.chess_coord[sq] new_pawn = Pawn(new_spot, True) new_pawn.ficha.setpos(new_pawn.spot) white_pawns_list.append(new_pawn)
def test_valid_moves_seventh_row_white(self): board = Board() board.set_config(flat=EMPTY * 64) # it doesn't matter much pawn = Pawn(board, Square(1, 0), WHITE) expected = [Square(0, 0)] result = pawn.valid_moves() self.assertEqual(expected, result)
def _init_pieces(self): if not self._positions: king = King(color=self._color) self._positions.update({str(king.position): king}) queen = Queen(color=self._color) self._positions.update({str(queen.position): queen}) for i in range(1, 9): pawn = Pawn(self._color, col=i) self._positions.update({str(pawn.position): pawn}) knight = Knight(self._color, col=2) self._positions.update({str(knight.position): knight}) knight = Knight(self._color, col=7) self._positions.update({str(knight.position): knight}) rook = Rook(self._color, col=1) self._positions.update({str(rook.position): rook}) rook = Rook(self._color, col=8) self._positions.update({str(rook.position): rook}) bishop = Bishop(self._color, col=3) self._positions.update({str(bishop.position): bishop}) bishop = Bishop(self._color, col=6) self._positions.update({str(bishop.position): bishop})
def test_place_pawn(self, test_board): """ Explicitly tests the place() method without relying on the test_pawn fixture. """ # Check that the intended Space is on the Board with the expected attributes. expected_file = random.choice(FILES) expected_rank = random.choice(RANKS) expected_name = expected_file + str(expected_rank) target_space = test_board.get_space(expected_file, expected_rank) assert target_space assert target_space.file == expected_file assert target_space.rank == expected_rank assert target_space.name == expected_name # Place a Pawn on the intended Space expected_color = PieceColor.WHITE test_pawn = Pawn(expected_color) # Check that the Pawn was created assert test_pawn test_pawn.place(target_space) # Check that the Pawn is on the intended Space assert test_pawn.current_space is target_space assert test_pawn.moved is False
def test_valid_moves_second_row_black(self): board = Board() board.set_config(flat=EMPTY * 64) # it doesn't matter much pawn = Pawn(board, Square(6, 0), BLACK) expected = [Square(7, 0)] result = pawn.valid_moves() self.assertEqual(expected, result)
def test_white_capture_right(self, test_board, white_test_pawn): assert test_board assert white_test_pawn assert white_test_pawn.current_space.rank < MAX_RANK opposing_pawn = Pawn(-(white_test_pawn.color.value)) assert opposing_pawn # Place the opposing Pawn one space in front of and one space to the right of the test Pawn target_file = chr(ord(white_test_pawn.current_space.file) + 1) target_rank = white_test_pawn.current_space.rank + 1 target_space = test_board.get_space(target_file, target_rank) opposing_pawn.place(target_space) # Capture the opposing Pawn with the test Pawn white_test_pawn.capture(target_space) # Check that the test Pawn is on the opposing Pawn's previous Space assert white_test_pawn.current_space is target_space # Check that the opposing Pawn has been removed from the board assert opposing_pawn.current_space is None # Check that the test Pawn is its Space's current piece assert target_space.current_piece is white_test_pawn
def test_get_board_should_add_the_pawn_in_the_bottom_right_of_the_board( self): # Given pawns = [Pawn(16, 16, Orientation.WEST)] fences = [[1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 2, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]] expected = [[2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2], [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 1, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2], [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 1, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 3]] # When actual = get_board(pawns, fences) # Then self.assertEqual( actual, expected, "In the board, the pawn should be placed in the bottom right of the board" )
def test_is_a_victory_should_not_be_ok_the_pawn_is_on_the_board(self): # Given pawn = Pawn(10, 8, Orientation.WEST) # When actual = game.is_a_victory(pawn) # Then self.assertFalse(actual, "The game should continue, the pawn is on the board")
def test_act_should_raise_the_exception_when_moving_out_of_the_board(self): # Given pawn = Pawn(0, 8, Orientation.WEST) fences = [[Direction.NO for i in range(FENCE_SIZE)] for j in range(FENCE_SIZE)] # When Then self.assertRaises(OutOfBoardException, game.act, Action.LEFT, pawn, fences)
def test_act_should_raise_exception_with_unknown_action(self): # Given pawn = Pawn(2, 8, Orientation.WEST) fences = [[Direction.NO for i in range(FENCE_SIZE)] for j in range(FENCE_SIZE)] # When Then self.assertRaises(UnknownActionException, game.act, Action.UNKNOWN, pawn, fences)