我有一个由纽扣制成的tic tac toe board。 每个按钮都链接到启动逻辑的方法。 我遇到的问题是,当人类玩家(noughts)轮到他们时,逻辑正在跳过那个回合,运行AI转向然后只回到人类玩家,这次他们正在按预期工作
点击该按钮时,该问题应该调用setText
方法并标记为“' O'对于人类玩家,或者' X'对于AI。
在此之后,按钮被设置为无效,跟踪电路板的阵列会发生变化。
我正在打印日志,从我可以看到,人类用户点击是触发AI移动的内容,而不是自动完成。但我无法说明为什么会这样。 我认为我的逻辑存在缺陷,但我无法找出问题所在。 编辑:它适用于第一次人工点击,之后就是那个问题。
代码:
package com.example.richardcurteis.connect3;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import java.util.Random;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
boolean noughtsTurn;
ArrayList board;
Players player;
int aiPickedButton;
int buttonPressed;
int targetIndex;
public void receiveClick(View view) {
String takeButton = String.valueOf(view.getTag());
buttonPressed = Integer.parseInt(takeButton);
playNow(view);
//checkForWin; Check winning conditions. Not yet implemented
}
public void playNow(View view) {
if (noughtsTurn) {
System.out.println("Player picked: " + buttonPressed);
playerClick(view);
} else {
if (aiValidPick()) {
playerClick(view);
} else {
playNow(view);
}
}
}
public void setTurn(boolean trueOrFalse) {
if (trueOrFalse) {
noughtsTurn = false;
} else {
noughtsTurn = true;
}
}
public void playerClick(View view) {
Button B;
int boardSetIndex;
int boardSetValue;
if (view instanceof Button) {
B = (Button) view;
if ( noughtsTurn ) {
B.setText(player.noughtsPlayer());
} else {
B = aiPlayerPick();
System.out.println("AI picked: " + targetIndex);
B.setText(player.crossesPlayer());
}
board.set(boardSetIndex(), boardSetValue());
System.out.println("Board: " + board);
B.setEnabled(false);
setTurn(noughtsTurn);
}
}
public int boardSetIndex() {
int index;
if (noughtsTurn) {
index = buttonPressed;
} else {
index = targetIndex;
}
return index;
}
public int boardSetValue() {
int value;
if (noughtsTurn) {
value = player.humanTurnValue();
} else {
value = player.aiTurnValue();
}
return value;
}
public Integer randomButtonPick() {
Random randomNumber = new Random();
int randomInt = randomNumber.nextInt(board.size());
return randomInt;
}
public boolean aiValidPick() {
aiPickedButton = randomButtonPick();
if (board.get(aiPickedButton).equals(player.boardArrayDefaultValue())
&& !board.get(aiPickedButton).equals(player.humanTurnValue())
&& !board.get(aiPickedButton).equals(player.aiTurnValue())){
return true;
} else {
return false;
}
}
public Button aiPlayerPick() {
Button btn = null;
TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);
for (int rowIndex = 0; rowIndex < tableLayout.getChildCount(); rowIndex++) {
View tableLayoutChild = tableLayout.getChildAt(rowIndex);
if (tableLayoutChild instanceof TableRow) {
for (int i = 0; i < ((ViewGroup) tableLayoutChild).getChildCount(); i++) {
View view = ((ViewGroup) tableLayoutChild).getChildAt(i);
String targetButton = String.valueOf(aiPickedButton);
if (view instanceof Button && view.getTag().equals(targetButton) ) {
targetIndex = Integer.parseInt(targetButton);
View btn_v = view.findViewWithTag(targetButton);
btn = (Button) btn_v;
break;
}
}
}
}
return btn;
}
public class Players {
public String noughtsPlayer() { return "O"; }
public String crossesPlayer() { return "X"; }
//public String blankButton() { return ""; }
public int humanTurnValue() { return 0;}
public int aiTurnValue() { return 1;}
public int boardArrayDefaultValue() { return 2;}
}
public int getBoardSize() {
int buttonCount = 0;
TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);
for (int rowIndex = 0; rowIndex < tableLayout.getChildCount(); rowIndex++) {
View tableLayoutChild = tableLayout.getChildAt(rowIndex);
if (tableLayoutChild instanceof TableRow) {
for (int i = 0; i < ((ViewGroup) tableLayoutChild).getChildCount(); i++) {
View view = ((ViewGroup) tableLayoutChild).getChildAt(i);
if (view instanceof Button) {
buttonCount++;
}
}
}
}
return buttonCount;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
noughtsTurn = true;
player = new Players();
board = new ArrayList();
int boardSize = getBoardSize();
for (int boardIndex = 0; boardIndex < boardSize; boardIndex++) {
board.add(player.boardArrayDefaultValue());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.richardcurteis.connect3.MainActivity"
tools:showIn="@layout/activity_main"
android:background="#070000">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="false"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false"
android:layout_centerInParent="true"
android:id="@+id/tableLayout"
android:background="#000000">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/gridButton1"
android:layout_column="4"
android:onClick="receiveClick"
android:tag="0" />
android:nestedScrollingEnabled="false" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/gridButton2"
android:layout_column="12"
android:onClick="receiveClick"
android:tag="1" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/gridButton3"
android:layout_column="19"
android:onClick="receiveClick"
android:tag="2" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/gridButton4"
android:layout_column="4"
android:onClick="receiveClick"
android:tag="3"/>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/gridButton5"
android:layout_column="12"
android:onClick="receiveClick"
android:tag="4"/>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/gridButton6"
android:layout_column="19"
android:onClick="receiveClick"
android:tag="5"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/gridButton7"
android:layout_column="4"
android:onClick="receiveClick"
android:tag="6"/>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/gridButton8"
android:layout_column="12"
android:onClick="receiveClick"
android:tag="7"/>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/gridButton9"
android:layout_column="19"
android:onClick="receiveClick"
android:tag="8" />
</TableRow>
</TableLayout>
<Button
android:layout_width="200dp"
android:layout_height="120dp"
android:text="New Game"
android:id="@+id/newGameButton"
android:layout_below="@+id/tableLayout"
android:layout_centerHorizontal="true"
android:layout_marginTop="61dp" />
</RelativeLayout>
答案 0 :(得分:1)
问题不在于你的逻辑。问题在于如何实施。
您目前拥有一个逐步逻辑系统,其中逻辑中的每个步骤都由receiveClick(View)方法触发。问题是没有自动触发AI转弯。
你需要在playerClick(View)的末尾添加某种逻辑,这样如果玩家轮到它,就会重新运行AI转向的逻辑。
示例:
public void playerClick(View view) {
Button B;
int boardSetIndex;
int boardSetValue;
if (view instanceof Button) {
B = (Button) view;
if ( noughtsTurn ) {
B.setText(player.noughtsPlayer());
} else {
B = aiPlayerPick();
System.out.println("AI picked: " + targetIndex);
B.setText(player.crossesPlayer());
}
board.set(boardSetIndex(), boardSetValue());
System.out.println("Board: " + board);
B.setEnabled(false);
//
if(setTurn(noughtsTurn) == false){
playNow(findViewByTag(randomButtonPick()))
}
//
}
}
在上面的例子中,有一些关键的东西丢失,例如检查randomButtonPick()是否已被占用以及setTurn(boolean)返回void而不是布尔值的事实。你需要某种方法来检查玩家是否转了一圈,如果是的话,让AI转一圈。
答案 1 :(得分:0)
好的,所以这是我为这个问题找到的最终解决方案。
我保持了相同的逻辑,但将人类和AI分离成单独的方法,AI仍然依赖于X = [[ 1, 2], [3, 4], [5, 6], [7, 8]]
y = [0, 0, 1, 1]
k_fold = 2
for X_train, y_train, X_valid, y_valid in k_fold_generator(X, y, k_fold):
# Train using X_train and y_train
# Evaluate using X_valid and y_valid
方法。
代码:
noughtsTurn