如何对CPU进行编程以随机按按钮?

发布于 2025-01-19 08:44:23 字数 206 浏览 1 评论 0原文

我正在Matlab应用程序设计师中创建一个游戏,玩家在该游戏中与计算机对手进行比赛。我想编码CPU在轮到它时随机按下按钮。例如,在Tictactoe中,玩家对另一个玩家进行比赛,但在这种情况下,对手是CPU。 CPU能够随机单击按钮,例如,如果有9个按钮,则会按随机的9个按钮,只要尚未按下它。我不确定如何对此进行编程。

我尝试使用回调函数,但不知道如何对CPU进行编程以随机按按钮。

I am creating a game in matlab app designer in which a player plays against the computer opponent. I want to code the CPU to press a button at random when it is its turn. For example, in TicTacToe the player plays against another player but in this case, the opponent is the CPU. The CPU is able to click buttons at random for example if there are 9 buttons it will press on any of those 9 randomly providing it has not been pressed already. I am not sure how to program this any help would be highly appreciated.

I have tried to use the callback function but do not know how to program the cpu to randomly press buttons.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世态炎凉 2025-01-26 08:44:23

第二个思考,这不像触发回调功能那样简单,因为您必须让AI知道何时“按”按钮。因此,有关通知AI的零件也应在回调函数中:

function ButtonPushed(app, event)
    % Get the button that triggered the callback function
    btn = event.Source;

    btn.Text = 'X';
    btn.Enable = 'off';

    % Now, notify the AI to make its move
    AI_btn = app.Button_(randi(9));

    % Obviously there are better ways to do this
    % I will leave it for your own improvisation
    while ~isequal(AI_btn.Enable, 'on')
        % If the button is already pressed...
        AI_btn = app.Button_(randi(9));
    end

    AI_btn.Text = 'X';
    AI_btn.Enable = 'off';
end

On second thought, this is not as simple as triggering the callback function, because you have to let the AI know when to "press" the button also. So, the part about notifying the AI should be in the callback function as well:

function ButtonPushed(app, event)
    % Get the button that triggered the callback function
    btn = event.Source;

    btn.Text = 'X';
    btn.Enable = 'off';

    % Now, notify the AI to make its move
    AI_btn = app.Button_(randi(9));

    % Obviously there are better ways to do this
    % I will leave it for your own improvisation
    while ~isequal(AI_btn.Enable, 'on')
        % If the button is already pressed...
        AI_btn = app.Button_(randi(9));
    end

    AI_btn.Text = 'X';
    AI_btn.Enable = 'off';
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文