当前位置: 首页>>代码示例>>Python>>正文


Python Board.to_notation方法代码示例

本文整理汇总了Python中chess.Board.to_notation方法的典型用法代码示例。如果您正苦于以下问题:Python Board.to_notation方法的具体用法?Python Board.to_notation怎么用?Python Board.to_notation使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在chess.Board的用法示例。


在下文中一共展示了Board.to_notation方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: BoardTest

# 需要导入模块: from chess import Board [as 别名]
# 或者: from chess.Board import to_notation [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.to_notation方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。