class Agent(object):
    def __init__(self):
        self._chess_camera = ChessCamera()
        self._motion_coordinator = None
        self._logger = logging.getLogger(__name__)
        self._player = StockfishPlayer()

    def __enter__(self):
        self._motion_coordinator = Coordinator()
        self._motion_coordinator.reset()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self._motion_coordinator.close()
        self._motion_coordinator = None
        return False

    def _candidate_move(self):
        cbm = self._chess_camera.current_colored_board_mask()
        b = game.get_board()
        moves = possible_moves_for_board(b, cbm)
        return moves[0] if moves else None

    def _next_move(self):
        candidates = []
        for i in range(NUM_REQUIRED_MATCHING_CANDIDATES):
            candidates.append(self._candidate_move())
            time.sleep(2)
        return candidates[0] if len(set(candidates)) == 1 else None

    def _write_status(self):
        b = game.get_board()
        cbm = self._chess_camera.current_colored_board_mask()
        cbm_board = pawn_board_from_colored_board_mask(cbm)
        pgn = game.pgn()
        formatted_datetime = time.strftime("%x %X")
        text = unicode("\n\n").join(
            [formatted_datetime,
             unicode(cbm_board),
             unicode(b), pgn])
        with io.open(lib_path('status.txt'), 'w', encoding='utf8') as f:
            f.write(text)

    def perception_action_sequence(self):
        b = game.get_board()
        if b.is_game_over():
            self._logger.info("Game has ended, result: {}".format(b.result()))
            game.start_new_game()
        elif b.turn == chess.WHITE:
            m = self._next_move()
            if m is not None:
                game.apply_move(m)
        else:
            m = self._player.select_move(b)
            self._motion_coordinator.move_piece(m, b)  # 执行arm.py里的move方法
            game.apply_move(m)
        self._write_status()
def main():
    args = _get_args()
    setup_console_logging()
    rb = RandomBoard(seed=args.seed)
    _create_collection_folder(args.base_path, rb)
    end = False

    cam = ChessCamera()

    while not end:
        cbf = cam.current_chessboard_frame()
        img = cbf.img.copy()

        for i in range(1, 8):
            mx_pt = int((i / 8.0) * BOARD_SIZE)
            cv2.line(img, (0, mx_pt), (BOARD_SIZE, mx_pt), (255, 255, 255))
            cv2.line(img, (mx_pt, 0), (mx_pt, BOARD_SIZE), (255, 255, 255))

        for i in range(64):
            piece = rb.pieces[i]
            x = (i % 8) * SQUARE_SIZE
            y = BOARD_SIZE - (i / 8) * SQUARE_SIZE - SQUARE_SIZE
            if piece is not None:
                text = str(piece)
                cv2.putText(img, text, (x + 10, y + 20),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1,
                            cv2.LINE_AA)

        cv2.imshow(os.path.relpath(__file__), img)
        key = cv2.waitKey(1) & 0xFF
        if key == ord('r'):
            rb.increment()
            _create_collection_folder(args.base_path, rb)
        elif key == ord('c'):
            _capture_chessboard(args.base_path, rb, cbf.img)
        elif key == ord('q'):
            end = True

    cv2.destroyAllWindows()
Esempio n. 3
0
 def __init__(self):
     self._chess_camera = ChessCamera()
     self._motion_coordinator = None
     self._logger = logging.getLogger(__name__)
     self._player = StockfishPlayer()