我正在制作'Tic Tac Toe'游戏。 3x3字段,基本。现在我做了一些逻辑,加上随机计算机 选择。但有时当我点击该字段时,计算机不会移动它。我认为 问题在于arraylist。查看我的xml和java文件:
MY XML FILE:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.tictactoe.ThirdActivity"
android:background="@color/background"
android:padding="16dp"
android:orientation="vertical">
<LinearLayout
android:id="@+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
android:elevation="1dp"
android:orientation="horizontal">
<Button
android:id="@+id/buttonOne"
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_weight="1"
android:background="@color/buttonColor"
android:onClick="onClickButton"
android:layout_margin="5dp"
android:text=" "/>
<Button
android:id="@+id/buttonTwo"
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_weight="1.00"
android:background="@color/buttonColor"
android:onClick="onClickButton"
android:layout_margin="5dp"
android:text=" "/>
<Button
android:id="@+id/buttonThree"
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_weight="1"
android:background="@color/buttonColor"
android:onClick="onClickButton"
android:layout_margin="5dp"
android:text=" "/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/secondLayout"
android:layout_weight="4">
<Button
android:id="@+id/buttonFour"
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_weight="1"
android:background="@color/buttonColor"
android:onClick="onClickButton"
android:layout_margin="5dp"
android:text=" "/>
<Button
android:id="@+id/buttonFive"
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_weight="1"
android:background="@color/buttonColor"
android:onClick="onClickButton"
android:layout_margin="5dp"
android:text=" "/>
<Button
android:id="@+id/buttonSix"
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_weight="1"
android:background="@color/buttonColor"
android:onClick="onClickButton"
android:layout_margin="5dp"
android:text=" "/>
</LinearLayout>
<LinearLayout
android:id="@+id/thirdLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
android:orientation="horizontal">
<Button
android:id="@+id/buttonSeven"
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_weight="1"
android:background="@color/buttonColor"
android:onClick="onClickButton"
android:layout_margin="5dp"
android:text=" "/>
<Button
android:id="@+id/buttonEight"
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_weight="1"
android:background="@color/buttonColor"
android:onClick="onClickButton"
android:layout_margin="5dp"
android:text=" "/>
<Button
android:id="@+id/buttonNine"
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_weight="1"
android:background="@color/buttonColor"
android:onClick="onClickButton"
android:layout_margin="5dp"
android:text=" "/>
</LinearLayout>
<LinearLayout
android:id="@+id/fourthLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
android:orientation="horizontal">
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="@string/restart"
android:textColor="@color/text"/>
</LinearLayout>
</LinearLayout>
MY JAVA FILE:
package com.tictactoe;
import android.app.ActionBar;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Random;
public class ThirdActivity extends Activity {
Button buttonOne;
Button buttonTwo;
Button buttonThree;
Button buttonFour;
Button buttonFive;
Button buttonSix;
Button buttonSeven;
Button buttonEight;
Button buttonNine;
ArrayList<Button> myList = new ArrayList<Button>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.BLACK));
actionBar.setIcon(R.mipmap.tictactoe);
actionBar.setTitle("Play Game");
buttonOne = (Button) findViewById(R.id.buttonOne);
buttonTwo = (Button) findViewById(R.id.buttonTwo);
buttonThree = (Button) findViewById(R.id.buttonThree);
buttonFour = (Button) findViewById(R.id.buttonFour);
buttonFive = (Button) findViewById(R.id.buttonFive);
buttonSix = (Button) findViewById(R.id.buttonSix);
buttonSeven = (Button) findViewById(R.id.buttonSeven);
buttonEight = (Button) findViewById(R.id.buttonEight);
buttonNine = (Button) findViewById(R.id.buttonNine);
myList.add(0, buttonOne);
myList.add(1, buttonTwo);
myList.add(2, buttonThree);
myList.add(3, buttonFour);
myList.add(4, buttonFive);
myList.add(5, buttonSix);
myList.add(5, buttonSeven);
myList.add(5, buttonEight);
myList.add(5, buttonNine);
}
public void onClickButton(View view) {
if (view.equals(buttonOne)) {
CharSequence textOne = buttonOne.getText().toString();
if (!textOne.equals(" ")) {
Toast.makeText(this, "This filed is not empty, choose another.", Toast.LENGTH_SHORT).show();
} else {
buttonOne.setText("X");
buttonOne.setTextColor(Color.GREEN);
myList.remove(buttonOne);
Button random = myList.get(new Random().nextInt(myList.size()));
if (!random.equals(" ")) {
random.setText("O");
random.setTextColor(Color.RED);
}
}
} else if (view.equals(buttonTwo)) {
CharSequence textTwo = buttonTwo.getText().toString();
if (!textTwo.equals(" ")) {
Toast.makeText(this, "This filed is not empty, choose another.", Toast.LENGTH_SHORT).show();
} else {
buttonTwo.setText("X");
buttonTwo.setTextColor(Color.GREEN);
myList.remove(buttonTwo);
Button random = myList.get(new Random().nextInt(myList.size()));
if (!random.equals(" ")) {
random.setText("O");
random.setTextColor(Color.RED);
}
}
} else if (view.equals(buttonThree)) {
CharSequence textThree = buttonThree.getText().toString();
if (!textThree.equals(" ")) {
Toast.makeText(this, "This filed is not empty, choose another.", Toast.LENGTH_SHORT).show();
} else {
buttonThree.setText("X");
buttonThree.setTextColor(Color.GREEN);
myList.remove(buttonThree);
Button random = myList.get(new Random().nextInt(myList.size()));
if (!random.equals(" ")) {
random.setText("O");
random.setTextColor(Color.RED);
}
}
} else if (view.equals(buttonFour)) {
CharSequence textFour = buttonFour.getText().toString();
if (!textFour.equals(" ")) {
Toast.makeText(this, "This filed is not empty, choose another.", Toast.LENGTH_SHORT).show();
} else {
buttonFour.setText("X");
buttonFour.setTextColor(Color.GREEN);
myList.remove(buttonFour);
Button random = myList.get(new Random().nextInt(myList.size()));
if (!random.equals(" ")) {
random.setText("O");
random.setTextColor(Color.RED);
}
}
} else if (view.equals(buttonFive)) {
CharSequence textFive = buttonFive.getText().toString();
if (!textFive.equals(" ")) {
Toast.makeText(this, "This filed is not empty, choose another.", Toast.LENGTH_SHORT).show();
} else {
buttonFive.setText("X");
buttonFive.setTextColor(Color.GREEN);
myList.remove(buttonFive);
Button random = myList.get(new Random().nextInt(myList.size()));
if (!random.equals(" ")) {
random.setText("O");
random.setTextColor(Color.RED);
}
}
}else if (view.equals(buttonSix)) {
CharSequence textSix = buttonSix.getText().toString();
if (!textSix.equals(" ")) {
Toast.makeText(this, "This filed is not empty, choose another.", Toast.LENGTH_SHORT).show();
} else {
buttonSix.setText("X");
buttonSix.setTextColor(Color.GREEN);
myList.remove(buttonSix);
Button random = myList.get(new Random().nextInt(myList.size()));
if (!random.equals(" ")) {
random.setText("O");
random.setTextColor(Color.RED);
}
}
}else if (view.equals(buttonSeven)) {
CharSequence textSeven = buttonSeven.getText().toString();
if (!textSeven.equals(" ")) {
Toast.makeText(this, "This filed is not empty, choose another.", Toast.LENGTH_SHORT).show();
} else {
buttonSeven.setText("X");
buttonSeven.setTextColor(Color.GREEN);
myList.remove(buttonSeven);
Button random = myList.get(new Random().nextInt(myList.size()));
if (!random.equals(" ")) {
random.setText("O");
random.setTextColor(Color.RED);
}
}
}else if (view.equals(buttonEight)) {
CharSequence textEight = buttonEight.getText().toString();
if (!textEight.equals(" ")) {
Toast.makeText(this, "This filed is not empty, choose another.", Toast.LENGTH_SHORT).show();
} else {
buttonEight.setText("X");
buttonEight.setTextColor(Color.GREEN);
myList.remove(buttonEight);
Button random = myList.get(new Random().nextInt(myList.size()));
if (!random.equals(" ")) {
random.setText("O");
random.setTextColor(Color.RED);
}
}
}else if (view.equals(buttonNine)) {
CharSequence textNine = buttonNine.getText().toString();
if (!textNine.equals(" ")) {
Toast.makeText(this, "This filed is not empty, choose another.", Toast.LENGTH_SHORT).show();
} else {
buttonNine.setText("X");
buttonNine.setTextColor(Color.GREEN);
myList.remove(buttonNine);
Button random = myList.get(new Random().nextInt(myList.size()));
if (!random.equals(" ")) {
random.setText("O");
random.setTextColor(Color.RED);
}
}
}
}
}
答案 0 :(得分:1)
我认为,这段代码会产生问题:
Button random = myList.get(new Random().nextInt(myList.size()));
if (!random.equals(" ")) {
random.setText("O");
random.setTextColor(Color.RED);
}
例如:播放器选择第一个按钮,然后是计算机选择的时间,计算机随机选择0,但此按钮已被播放器点击,因此没有任何事情发生
你应该做一些这样的功能,以确保计算机选择正确的数字。
private void computerMove(){
Button random = myList.get(new Random().nextInt(myList.size()));
if (!random.equals(" ")) {
random.setText("O");
random.setTextColor(Color.RED);
} else {
computerMove();
}
}
它不是完美的解决方案,它可以冻结应用程序,你应该考虑通过计算机选择字段的更好的方法。
修改强> 我看到另一个问题,看看这个:
myList.add(0, buttonOne);
myList.add(1, buttonTwo);
myList.add(2, buttonThree);
myList.add(3, buttonFour);
myList.add(4, buttonFive);
myList.add(5, buttonSix);
myList.add(5, buttonSeven);
myList.add(5, buttonEight);
myList.add(5, buttonNine);
您正在向同一位置添加按钮,位置为5.
此外,我重构了一点你的功能,因为有很多的锅炉:
public void onClickButton(View view) {
if (view instanceof Button){
Button btn = (Button) view;
CharSequence text = btn.getText();
if(!text.equals(" ")){
Toast.makeText(this, "This filed is not empty, choose another.", Toast.LENGTH_SHORT).show();
} else {
btn.setText("X");
btn.setTextColor(Color.GREEN);
myList.remove(btn);
computerMove();
}
}
}
答案 1 :(得分:0)
这可能不是您所述问题的解决方案,但我不认为您可以使用.equals()
比较按钮和字符串,就像您在此处所做的那样...
!random.equals(" ")
。
为什么不重复你在这里做的事情?
CharSequence textTwo = buttonTwo.getText().toString();