我在 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"); }
最佳答案
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/