简体   繁体   English

模拟AI在Android Studio中的点击

[英]Simulate AI clicking in Android Studio

I made a game of tic tac toe in Android Studios. 我在Android Studio中制作了井字游戏。 I also made an AI that the player can play against, however the AI doesn't need to "click" because it can just set the values of the specific row and column to its value (X or 0). 我还制作了一个可以供玩家使用的AI,但是AI不需要“单击”,因为它可以将特定行和列的值设置为其值(X或0)。 Is there a way to simulate a click from the AI? 有没有一种方法可以模拟来自AI的点击?

<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>

My layout is pretty simple and in my player vs player mode, the clicks just look like: 我的布局非常简单,在播放器与播放器模式下,点击看起来像:

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

You can call Button.performClick() 您可以调用Button.performClick()

Call this view's OnClickListener, if it is defined. 调用此视图的OnClickListener(如果已定义)。 Performs all normal actions associated with clicking: reporting accessibility event, playing a sound, etc. 执行与单击相关的所有常规操作:报告可访问性事件,播放声音等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM