【问题标题】:Android tic tac toe game - logic issueAndroid tic tac toe 游戏 - 逻辑问题
【发布时间】:2017-07-12 16:14:53
【问题描述】:

我正在制作“井字游戏”游戏。 3x3 字段,基本。现在我做了一些逻辑,加上随机电脑 选择。但是有时当我单击该字段时会发生这种情况,计算机不会使其移动。我认为 问题出在数组列表中。查看我的 xml 和 java 文件:

我的 XML 文件:

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

我的 JAVA 文件:

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);
                }
            }
        }
    }
}

【问题讨论】:

    标签: android


    【解决方案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();
            }
        }
    }
    

    【讨论】:

    • 我可以在后面添加: Button random = myList.get(new Random().nextInt(myList.size())); if (!random.equals(" ")) { random.setText("O"); random.setTextColor(Color.RED);如果 random 等于 X 或 O 则另选一个代码,然后选择另一个随机数?
    • 无论如何,有什么更好的方法?谢谢你的时间,我的朋友。
    • 可以,但是第二个随机数也被X或者O占据了怎么办?这就是为什么最好进行一些迭代以找到空闲字段并将 O 放在这里。
    【解决方案2】:

    这可能无法解决您所说的问题,但我认为您不能像在此处那样使用.equals() 来比较按钮和字符串... !random.equals(" ")。 为什么不重复你在这里所做的?

    CharSequence textTwo = buttonTwo.getText().toString();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      相关资源
      最近更新 更多