當前位置: 首頁>>代碼示例>>Python>>正文


Python Board.empty方法代碼示例

本文整理匯總了Python中chess.Board.empty方法的典型用法代碼示例。如果您正苦於以下問題:Python Board.empty方法的具體用法?Python Board.empty怎麽用?Python Board.empty使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在chess.Board的用法示例。


在下文中一共展示了Board.empty方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: BoardTest

# 需要導入模塊: from chess import Board [as 別名]
# 或者: from chess.Board import empty [as 別名]
class BoardTest(unittest.TestCase):

    def setUp(self):
        self.notation = [["WR", "WN", "WB", "WQ", "WK", "WB", "WN", "WR"],
                         ["WP", "WP", "WP", "WP",   "", "WP", "WP", "WP"], 
                         [  "",   "",   "",   "",   "",   "",   "",   ""],
                         [  "",   "",   "",   "", "WP",   "",   "",   ""],
                         [  "",   "",   "",   "", "BP",   "",   "",   ""],
                         [  "",   "",   "",   "",   "",   "",   "",   ""],
                         ["BP", "BP", "BP", "BP",   "", "BP", "BP", "BP"], 
                         ["BR", "BN", "BB", "BQ", "BK", "BB", "BN", "BR"]]
        self.flat_notation = list(itertools.chain(*self.notation))
        self.num_pieces = len([square for square in self.flat_notation if square != ""])
        self.empty_board = Board()
        self.board = Board.from_notation(self.notation)

    def test_from_notation(self):
        for row, notation_row in enumerate(self.notation):
            for col, square in enumerate(notation_row):
                board_piece = self.board[row][col]
                if square == "":
                    self.assertEqual(board_piece, None)
                else:
                    self.assertEqual(Position(row, col), board_piece.position)
                    self.assertEqual(Color.from_notation(square[0]), board_piece.color)
                    self.assertEqual(Piece.from_notation(square[1]), type(board_piece))

    def test_to_notation(self):
        self.assertListEqual(self.notation, self.board.to_notation())
        self.assertListEqual([[""] * 8] * 8, self.empty_board.to_notation())

    def test_positions(self):
        # 64 distinct + in bounds must be all squares in the 8x8 board
        self.assertEqual(64, len(set(self.board.positions())))
        self.assertTrue(all(position.in_bounds for position in self.board.positions()))

    def test_squares(self):
        # 64 squares in an 8x8 board
        self.assertEqual(64, len(list(self.board.squares())))
        self.assertSetEqual(set(self.board.pieces()).union({None}), set(self.board.squares()))

    def test_pieces(self):
        self.assertEqual(self.num_pieces, len(list(self.board.pieces())))
        for piece in self.board.pieces():
            self.assertEqual(piece.position.row, piece.row)
            self.assertEqual(piece.position.col, piece.col)

    def test_white_pieces(self):
        white_pieces = set(self.board.white_pieces())
        self.assertEqual(16, len(white_pieces))

        for piece in white_pieces:
            self.assertTrue(piece.is_white)

        for piece in self.board.pieces():
            if piece.is_white:
                self.assertIn(piece, white_pieces)

    def test_black_pieces(self):
        black_pieces = set(self.board.black_pieces())
        self.assertEqual(16, len(black_pieces))

        for piece in black_pieces:
            self.assertTrue(piece.is_black)

        for piece in self.board.pieces():
            if piece.is_black:
                self.assertIn(piece, black_pieces)

    def test_add(self):
        board = Board()
        self.assertTrue(all(square == None for square in board.squares()))
        board.add(Color.white, Pawn, Position(3, 4))
        board.add(Color.black, Pawn, Position(6, 6))
        self.assertEqual(Color.white, board.at(Position(3, 4)).color)
        self.assertEqual(Pawn, type(board.at(Position(3, 4))))
        self.assertEqual(Color.black, board.at(Position(6, 6)).color)
        self.assertEqual(Pawn, type(board.at(Position(6, 6))))
        self.assertEqual(2, len(list(board.pieces())))
        for position in board.positions():
            if position not in (Position(3, 4), Position(6, 6)):
                self.assertIsNone(board.at(position))

    def test_at(self):
        for position in self.board.positions():
            self.assertIs(self.board[position.row][position.col], self.board.at(position))
        for piece in self.board.pieces():
            self.assertIs(piece, self.board.at(piece.position))

    def test_empty(self):
        self.assertTrue(all(self.empty_board.empty((row, col)) for row in range(8) for col in range(8)))
開發者ID:kbuzsaki,項目名稱:restchess,代碼行數:93,代碼來源:board_test.py


注:本文中的chess.Board.empty方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。