java - 在Android Studio中模拟AI点击

标签 java android android-studio tic-tac-toe

我在 Android Studio 中制作了一款井字棋游戏。我还制作了一个玩家可以对抗的 AI,但是 AI 不需要“单击”,因为它只需将特定行和列的值设置为其值(X 或 0)。有没有办法模拟人工智能的点击?

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <Button
            android:id="@+id/button_00"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="60sp"/>

        <Button
            android:id="@+id/button_01"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="60sp"/>

        <Button
            android:id="@+id/button_02"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="60sp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <Button
            android:id="@+id/button_10"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="60sp"/>

        <Button
            android:id="@+id/button_11"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="60sp"/>

        <Button
            android:id="@+id/button_12"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="60sp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <Button
            android:id="@+id/button_20"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="60sp"/>

        <Button
            android:id="@+id/button_21"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="60sp"/>

        <Button
            android:id="@+id/button_22"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="60sp"/>
    </LinearLayout>

我的布局非常简单,在我的玩家对玩家模式下,点击看起来像:

if(!((Button) v).getText().toString().equals("")){return;}
if(player1turn){ ((Button) v).setText("X"); 
        }else{ ((Button) v).setText("O"); }

最佳答案

您可以调用Button.performClick()

Call this view's OnClickListener, if it is defined. Performs all normal actions associated with clicking: reporting accessibility event, playing a sound, etc.

关于java - 在Android Studio中模拟AI点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53402900/

相关文章:

android - Android中展开recyclerView时如何旋转图像

android-studio - 如何在我的 gradle 构建脚本中访问 sdk/ndk 路径?

android - 没有可调试的进程并将调试符号分配给 .so 文件

java - jre安装目录奇怪

java - 对于 native 对等对象生命周期管理,是否真的应该避免 Java 终结器?

java - “找不到或加载主类”是什么意思?

android - 谷歌素材库和设计支持库的区别

java - 仅更改鼠标悬停时字母的颜色

Android 通知 : add text after app's name, 之前

android - 错误 :Failed to open zip file. Gradle 依赖项缓存可能已损坏(这有时会在网络连接超时后发生。)