def test_BlindBoard_changes(): '''Test BlindBoard editing methods''' board = BlindBoard.from_dict({chess.A1: WHITE, chess.B2: WHITE}) board.move_piece(chess.A1, chess.D5) board.change_color_at(chess.B2) nose.tools.eq_(board, BlindBoard.from_dict({chess.D5: WHITE, chess.B2: BLACK}))
def test_BlindBoardDiff(): '''Test the __eq__() method of BlindBoard.Diff''' diff1 = BlindBoard.Diff(chess.BB_A1, chess.BB_B1 | chess.BB_C1, chess.BB_VOID) diff2 = BlindBoard.Diff(chess.BB_A1, chess.BB_C1 | chess.BB_B1, chess.BB_VOID) nose.tools.ok_(diff1 == diff2)
def test_BlindBoard_changes(): '''Test BlindBoard editing methods''' board = BlindBoard.from_dict({chess.A1: WHITE, chess.B2: WHITE}) board.move_piece(chess.A1, chess.D5) board.change_color_at(chess.B2) nose.tools.eq_(board, BlindBoard.from_dict({ chess.D5: WHITE, chess.B2: BLACK }))
def test_take_move_diff(self): '''Test take move deduction''' board_1 = BlindBoard.from_dict( { chess.E2: WHITE, chess.F2: BLACK, }) board_2 = BlindBoard.from_dict( { chess.F2: WHITE, }) diff = BlindBoard.diff_board(board_2, board_1) move = core.diffreader.read(diff) expected_move = Move(chess.E2, chess.F2) nose.tools.eq_(move, expected_move)
def test_BlindBoard_identical(): '''Diff 2 identical BlindBoards and check the result''' board_from = BlindBoard.from_dict({chess.A1: WHITE, chess.A7: BLACK, chess.A8: BLACK, chess.E2: WHITE}) board_to = BlindBoard.from_dict({chess.A1: WHITE, chess.A7: BLACK, chess.A8: BLACK, chess.E2: WHITE}) diff = board_to.diff(board_from) nose.tools.ok_(not diff.emptied) nose.tools.ok_(not diff.filled) nose.tools.ok_(not diff.changed)
def test_take_move_diff(self): '''Test take move deduction''' board_1 = BlindBoard.from_dict({ chess.E2: WHITE, chess.F2: BLACK, }) board_2 = BlindBoard.from_dict({ chess.F2: WHITE, }) diff = BlindBoard.diff_board(board_2, board_1) move = core.diffreader.read(diff) expected_move = Move(chess.E2, chess.F2) nose.tools.eq_(move, expected_move)
def test_queen_castling_move_diff(self): '''Test queen castling move deduction''' board_1 = BlindBoard.from_dict({ chess.E8: BLACK, chess.A8: BLACK, }) board_2 = BlindBoard.from_dict({ chess.D8: BLACK, chess.C8: BLACK, }) diff = BlindBoard.diff_board(board_2, board_1) move = core.diffreader.read(diff) expected_move = Move(chess.E8, chess.C8) nose.tools.eq_(move, expected_move)
def test_king_castling_move_diff(self): '''Test king castling move deduction''' board_1 = BlindBoard.from_dict( { chess.H1: WHITE, chess.E1: WHITE, }) board_2 = BlindBoard.from_dict( { chess.F1: WHITE, chess.G1: WHITE, }) diff = BlindBoard.diff_board(board_2, board_1) move = core.diffreader.read(diff) expected_move = Move(chess.E1, chess.G1) nose.tools.eq_(move, expected_move)
def test_king_castling_move_diff(self): '''Test king castling move deduction''' board_1 = BlindBoard.from_dict({ chess.H1: WHITE, chess.E1: WHITE, }) board_2 = BlindBoard.from_dict({ chess.F1: WHITE, chess.G1: WHITE, }) diff = BlindBoard.diff_board(board_2, board_1) move = core.diffreader.read(diff) expected_move = Move(chess.E1, chess.G1) nose.tools.eq_(move, expected_move)
def test_BlindBoard_moves(): '''Diff 2 BlinBoards and check the result''' board_from = BlindBoard.from_dict({chess.A1: WHITE, chess.A7: BLACK, chess.A8: BLACK, chess.E2: WHITE}) board_to = BlindBoard.from_dict({chess.A1: WHITE, chess.A7: BLACK, chess.A8: WHITE, chess.E4: WHITE}) diff = board_to.diff(board_from) nose.tools.eq_(diff.emptied, chess.BB_E2) nose.tools.eq_(diff.filled, chess.BB_E4) nose.tools.eq_(diff.changed, chess.BB_A8) nose.tools.eq_(diff.__str__(), "emptied:{'e2'} filled:{'e4'} changed:{'a8'}")
def test_BlindBoard_identical(): '''Diff 2 identical BlindBoards and check the result''' board_from = BlindBoard.from_dict({ chess.A1: WHITE, chess.A7: BLACK, chess.A8: BLACK, chess.E2: WHITE }) board_to = BlindBoard.from_dict({ chess.A1: WHITE, chess.A7: BLACK, chess.A8: BLACK, chess.E2: WHITE }) diff = board_to.diff(board_from) nose.tools.ok_(not diff.emptied) nose.tools.ok_(not diff.filled) nose.tools.ok_(not diff.changed)
def test_BlindBoard_moves(): '''Diff 2 BlinBoards and check the result''' board_from = BlindBoard.from_dict({ chess.A1: WHITE, chess.A7: BLACK, chess.A8: BLACK, chess.E2: WHITE }) board_to = BlindBoard.from_dict({ chess.A1: WHITE, chess.A7: BLACK, chess.A8: WHITE, chess.E4: WHITE }) diff = board_to.diff(board_from) nose.tools.eq_(diff.emptied, chess.BB_E2) nose.tools.eq_(diff.filled, chess.BB_E4) nose.tools.eq_(diff.changed, chess.BB_A8) nose.tools.eq_(diff.__str__(), "emptied:{'e2'} filled:{'e4'} changed:{'a8'}")
def test_BlindBoard_print(): '''Pretty-printing of a blind board''' blindboard = BlindBoard(chess.STARTING_BOARD_FEN) expected = '''b b b b b b b b b b b b b b b b . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . W W W W W W W W W W W W W W W W''' eq_(str(blindboard), expected)
def get_blindboard(self): """ Converts the numpy array `_blindboard_matrix` into a BlindBoard object """ if self._blindboard_matrix is None: raise ImageProcessorException( "The `.process` method has not been called on this object yet") occupied_squares = {} for (i, j), entry in np.ndenumerate(self._blindboard_matrix): col = j ; row = 7-i if entry is not None: occupied_squares[chess.square(col, row)] = bool(entry) return BlindBoard.from_dict(occupied_squares)
def get_blindboard(self): """ Converts the numpy array `_blindboard_matrix` into a BlindBoard object """ if self._blindboard_matrix is None: raise ImageProcessorException( "The `.process` method has not been called on this object yet") occupied_squares = {} for (i, j), entry in np.ndenumerate(self._blindboard_matrix): col = j row = 7 - i if entry is not None: occupied_squares[chess.square(col, row)] = bool(entry) return BlindBoard.from_dict(occupied_squares)
def test_imgage_processor(): '''Test image processor (multiple tests)''' expected_board = BlindBoard.get_starting_board() expected_board.remove_piece_at(chess.E2) expected_board.set_piece_at(chess.E4, Piece(PAWN, WHITE)) # retrieve all the images paths and sort images = collect_test_images('tests/pictures/game000') debug("Calibrating image processor...") processor = ImageProcessor(images[0], images[1]) for img, expected_board in zip(images[2:], expected_boards()): debug("Processing `{}`...".format(os.path.basename(img))) processor.process(img) board = processor.get_blindboard() yield compare_blindboards, expected_board, board, img
def expected_boards(): '''This generator returns all the BlindBoards corresponding to the images inside ./pictures (starting with board-2.jpg)''' b = BlindBoard.get_starting_board() b.move_piece(chess.E2, chess.E4) yield b # board-2.jpg b.move_piece(chess.E7, chess.E5) yield b b.move_piece(chess.G1, chess.F3) yield b b.move_piece(chess.B8, chess.C6) yield b b.move_piece(chess.F1, chess.B5) yield b b.move_piece(chess.G8, chess.F6) yield b b.move_piece(chess.E1, chess.F1) b.move_piece(chess.H1, chess.G1) yield b b.remove_piece_at(chess.F6) b.change_color_at(chess.E4) yield b b.move_piece(chess.D1, chess.E2) yield b b.move_piece(chess.D7, chess.D5) yield b b.remove_piece_at(chess.F3) b.change_color_at(chess.E5) yield b b.move_piece(chess.C8, chess.D7) yield b b.remove_piece_at(chess.E5) b.change_color_at(chess.C6) yield b b.remove_piece_at(chess.B7) b.change_color_at(chess.C6) yield b b.move_piece(chess.B5, chess.D3) yield b
def setUp(self): self.bb_start = BlindBoard.get_starting_board()