我做了一个 tictactoe 游戏,但是当我点击一个空的 space 时它就崩溃了

I made a tictactoe game but it is crashing once I click on an empty space

我做了一个 tictactoe 游戏,但是当我点击一个空的 space 时它就崩溃了 space。

我使用了一个Arraylist 来改变当在tictactoe 网格中点击相应位置时索引的状态。例如,如果我在网格右上角的单元格中打勾,如果当前玩家是 'O',则索引 2 将更改为 1,如果当前玩家是 'X'.

,则索引 2 将更改为 2

有一个计数器可以跟踪谁在玩,如果余数为 0 则放置 'O' 否则放置 'X'。

此外,我使用了一个标志 'gstate' 在一个玩家获胜时停止游戏。

问题是游戏一开始是可以运行的,但是当涉及到我检查哪个玩家是赢家的任何部分时,游戏崩溃了。

谁能帮帮我。

这是主要的 Jave 代码

package com.example.tictactoe;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
    
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
    
public class MainActivity extends AppCompatActivity {

    public int counter = 0;
    Toast msg;
    ImageView img, img2, img3, img4, img5, img6, img7, img8, img9;


    int o = 1;
    int x = 2;

    List<Integer> winner = new ArrayList<>(9);

    boolean owin, xwin = false;
    boolean gstate = true;

    //oOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO

    public void isOm() {
        if (counter % 2 != 0 && img5.getDrawable() != null) {
            winner.set(4, 1);
        } else {
        }
    }

    public void isOmr() {
        if (counter % 2 != 0 && img6.getDrawable() != null) {
            winner.set(5, 1);
        } else {
        }
    }

    public void isOml() {
        if (counter % 2 != 0 && img4.getDrawable() != null) {
            winner.set(3, 1);
        } else {
        }
    }

    public void isOru() {
        if (counter % 2 != 0 && img3.getDrawable() != null) {
            winner.set(2, 1);
        } else {
        }
    }

    public void isOmu() {
        if (counter % 2 != 0 && img2.getDrawable() != null) {
            winner.set(1, 1);
        } else {
        }
    }


    public void isOlu() {
        if (counter % 2 != 0 && img.getDrawable() != null) {
            winner.set(0, 1);
        } else {
        }
    }

    public void isOdr() {
        if (counter % 2 != 0 && img9.getDrawable() != null) {
            winner.set(8, 1);
        } else {
        }
    }

    public void isOmd() {
        if (counter % 2 != 0 && img8.getDrawable() != null) {
            winner.set(7, 1);
        } else {
        }
    }

    public void isOdl() {
        if (counter % 2 != 0 && img7.getDrawable() != null) {
            winner.set(6, 1);
        } else {
        }
    }

    //oOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO

    //xXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxX

    public void isXm() {
        if (counter % 2 == 0 && img5.getDrawable() != null) {
            winner.set(4, 2);
        } else {
        }
    }

    public void isXmr() {
        if (counter % 2 == 0 && img6.getDrawable() != null) {
            winner.set(5, 2);
        } else {
        }
    }

    public void isXml() {
        if (counter % 2 == 0 && img4.getDrawable() != null) {
            winner.set(3, 2);
        } else {
        }
    }

    public void isXru() {
        if (counter % 2 == 0 && img3.getDrawable() != null) {
            winner.set(2, 2);
        } else {
        }
    }

    public void isXmu() {
        if (counter % 2 == 0 && img2.getDrawable() != null) {
            winner.set(1, 2);
        } else {
        }
    }

    public void isXlu() {
        if (counter % 2 == 0 && img.getDrawable() != null) {
            winner.set(0, 2);
        } else {
        }
    }

    public void isXdr() {
        if (counter % 2 == 0 && img9.getDrawable() != null) {
            winner.set(8, 2);
        } else {
        }
    }

    public void isXmd() {
        if (counter % 2 == 0 && img8.getDrawable() != null) {
            winner.set(7, 2);
        } else {
        }
    }

    public void isXdl() {
        if (counter % 2 == 0 && img7.getDrawable() != null) {
            winner.set(6, 2);
        } else {
        }
    }


    //xXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxX

    public boolean oWin() {
        try {
           if(((img.getDrawable() != null) && (img2.getDrawable() != null) && (img3.getDrawable() != null) && ((int)winner.get(0) == 1) && ((int)winner.get(1) == 1) && ((int)winner.get(2) == 1)) || ((img4.getDrawable() != null) && (img5.getDrawable() != null) && (img6.getDrawable() != null) && ((int)winner.get(3) == 1) && ((int)winner.get(4) == 1) && ((int)winner.get(5) == 1)) || ((img7.getDrawable() != null) && (img8.getDrawable() != null) && (img9.getDrawable() != null) && ((int)winner.get(6) == 1) && ((int)winner.get(7) == 1) && ((int)winner.get(8) == 1)) || ((img.getDrawable() != null) && (img4.getDrawable() != null) && (img7.getDrawable() != null) && ((int)winner.get(0) == 1) && ((int)winner.get(3) == 1) && ((int)winner.get(6) == 1)) || ((img2.getDrawable() != null) && (img5.getDrawable() != null) && (img8.getDrawable() != null) && ((int)winner.get(1) == 1) && ((int)winner.get(4) == 1) && ((int)winner.get(7) == 1)) || ((img3.getDrawable() != null) && (img6.getDrawable() != null) && (img9.getDrawable() != null) && ((int)winner.get(2) == 1) && ((int)winner.get(5) == 1) && ((int)winner.get(8) == 1)) || ((img.getDrawable() != null) && (img5.getDrawable() != null) && (img9.getDrawable() != null) && ((int)winner.get(0) == 1) && ((int)winner.get(4) == 1) && ((int)winner.get(8) == 1)) || ((img3.getDrawable() != null) && (img5.getDrawable() != null) && (img7.getDrawable() != null) && ((int)winner.get(2) == 1) && ((int)winner.get(4) == 1) && ((int)winner.get(6) == 1))){
                owin = true;
            }
            return owin;
        }
        catch(Exception e) {
            //Toast.makeText(this,"An image is not here",Toast.LENGTH_SHORT).show();
            return owin;
        }
    }

    public boolean xWin() {
        try {
            if (((img.getDrawable() != null) && (img2.getDrawable() != null) && (img3.getDrawable() != null) && ((int)winner.get(0) == 2) && ((int)winner.get(1) == 2) && ((int)winner.get(2) == 2)) || ((img4.getDrawable() != null) && (img5.getDrawable() != null) && (img6.getDrawable() != null) && ((int)winner.get(3) == 2) && ((int)winner.get(4) == 2) && ((int)winner.get(5) == 2)) || ((img7.getDrawable() != null) && (img8.getDrawable() != null) && (img9.getDrawable() != null) && ((int)winner.get(6) == 2) && ((int)winner.get(7) == 2) && ((int)winner.get(8) == 2)) || ((img.getDrawable() != null) && (img4.getDrawable() != null) && (img7.getDrawable() != null) && ((int)winner.get(0) == 2) && ((int)winner.get(3) == 2) && ((int)winner.get(6) == 2)) || ((img2.getDrawable() != null) && (img5.getDrawable() != null) && (img8.getDrawable() != null) && ((int)winner.get(1) == 2) && ((int)winner.get(4) == 2) && ((int)winner.get(7) == 2)) || ((img3.getDrawable() != null) && (img6.getDrawable() != null) && (img9.getDrawable() != null) && ((int)winner.get(2) == 2) && ((int)winner.get(5) == 2) && ((int)winner.get(8) == 2)) || ((img.getDrawable() != null) && (img5.getDrawable() != null) && (img9.getDrawable() != null) && ((int)winner.get(0) == 2) && ((int)winner.get(4) == 2) && ((int)winner.get(8) == 2)) || ((img3.getDrawable() != null) && (img5.getDrawable() != null) && (img7.getDrawable() != null) && ((int)winner.get(2) == 2) && ((int)winner.get(4) == 2) && ((int)winner.get(6) == 2))){
                xwin = true;
            }
            return xwin;
        }
        catch(Exception e) {
            //Toast.makeText(this,"An image is not here",Toast.LENGTH_SHORT).show();
            return xwin;
        }
    }

    public void greset(View view) {
        counter = 0;
        //img = (ImageView) findViewById(R.id.imageView);
        img.setImageResource(0);
        //img2 = (ImageView) findViewById(R.id.imageView2);
        img2.setImageResource(0);
        //img3 = (ImageView) findViewById(R.id.imageView3);
        img3.setImageResource(0);
        //img4 = (ImageView) findViewById(R.id.imageView4);
        img4.setImageResource(0);
        //img5 = (ImageView) findViewById(R.id.imageView5);
        img5.setImageResource(0);
        //img6 = (ImageView) findViewById(R.id.imageView6);
        img6.setImageResource(0);
        //img7 = (ImageView) findViewById(R.id.imageView7);
        img7.setImageResource(0);
        //img8 = (ImageView) findViewById(R.id.imageView8);
        img8.setImageResource(0);
        //img9 = (ImageView) findViewById(R.id.imageView9);
        img9.setImageResource(0);
        gstate = true;
    }

    public void pgame(View view) {
        ImageView image = (ImageView) view;

        img = (ImageView) findViewById(R.id.imageView);
        img2 = (ImageView) findViewById(R.id.imageView2);
        img3 = (ImageView) findViewById(R.id.imageView3);
        img4 = (ImageView) findViewById(R.id.imageView4);
        img5 = (ImageView) findViewById(R.id.imageView5);
        img6 = (ImageView) findViewById(R.id.imageView6);
        img7 = (ImageView) findViewById(R.id.imageView7);
        img8 = (ImageView) findViewById(R.id.imageView8);
        img9 = (ImageView) findViewById(R.id.imageView9);

        if (image.getDrawable() != null) {
            msg = Toast.makeText(this, "Please select another space", Toast.LENGTH_SHORT);
            msg.show();
            if((ImageView)view == img){
                winner.set(0, 1);
            }
            else if((ImageView)view == img2){
                winner.set(1, 1);
            }
            else if((ImageView)view == img3){
                winner.set(2, 1);
            }
            else if((ImageView)view == img4){
                winner.set(3, 1);
            }
            else if((ImageView)view == img5){
                winner.set(4, 1);
            }
            else if((ImageView)view == img6){
                winner.set(5, 1);
            }
            else if((ImageView)view == img7){
                winner.set(6, 1);
            }
            else if((ImageView)view == img8){
                winner.set(7, 1);
            }
            else if((ImageView)view == img9){
                winner.set(8, 1);
            }
            else if((ImageView)view == img){
                winner.set(0, 2);
            }
            else if((ImageView)view == img2){
                winner.set(1, 2);
            }
            else if((ImageView)view == img3){
                winner.set(2, 2);
            }
            else if((ImageView)view == img4){
                winner.set(3, 2);
            }
            else if((ImageView)view == img5){
                winner.set(4, 2);
            }
            else if((ImageView)view == img6){
                winner.set(5, 2);
            }
            else if((ImageView)view == img7){
                winner.set(6, 2);
            }
            else if((ImageView)view == img8){
                winner.set(7, 2);
            }
            else if((ImageView)view == img9){
                winner.set(8, 2);
            }

            if (counter > 5 && counter < 9){
                this.xWin();
                this.oWin();
            }

            if (owin) {
                msg = Toast.makeText(this, "\'O\' is the winner, press the reset button", Toast.LENGTH_SHORT);
                msg.show();
                xwin = false;
                gstate = false;
            }

            if (xwin) {
                msg = Toast.makeText(this, "\'X\' is the winner, press the reset button", Toast.LENGTH_SHORT);
                msg.show();
                xwin = false;
                gstate = false;
            }

        }

        if (image.getDrawable() == null) {
            if (counter % 2 == 0) {
                image.setImageResource(R.drawable.o);
                counter += 1;

            } else {
                image.setImageResource(R.drawable.x);
                counter += 1;

            }

            image.setTranslationY(-1500);
            image.animate().translationYBy(1500).setDuration(500);




        }


    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
}

xml代码如下

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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:background="@color/cardview_light_background"
tools:context=".MainActivity">

<androidx.gridlayout.widget.GridLayout
    android:id="@+id/gridLayout"
    android:layout_width="420dp"
    android:layout_height="420dp"
    android:background="@drawable/grid"
    app:columnCount="3"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.555"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.733"
    app:rowCount="3">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:onClick="pgame"
        app:layout_column="0"
        app:layout_row="0" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:layout_marginLeft="22dp"
        android:onClick="pgame"
        app:layout_column="1"
        app:layout_row="0" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:layout_marginLeft="18dp"
        android:onClick="pgame"
        app:layout_column="2"
        app:layout_row="0" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:layout_marginTop="23dp"
        android:onClick="pgame"
        app:layout_column="0"
        app:layout_row="1" />

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="23dp"
        android:onClick="pgame"
        app:layout_column="1"
        app:layout_row="1" />

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="23dp"
        android:onClick="pgame"
        app:layout_column="2"
        app:layout_row="1" />

    <ImageView
        android:id="@+id/imageView7"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:layout_marginTop="23dp"
        android:onClick="pgame"
        app:layout_column="0"
        app:layout_row="2" />

    <ImageView
        android:id="@+id/imageView8"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="23dp"
        android:onClick="pgame"
        app:layout_column="1"
        app:layout_row="2" />

    <ImageView
        android:id="@+id/imageView9"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="23dp"
        android:onClick="pgame"
        app:layout_column="2"
        app:layout_row="2" />
</androidx.gridlayout.widget.GridLayout>

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="90dp"
    android:layout_marginBottom="45dp"
    android:onClick="greset"
    android:text="Reset"
    app:layout_constraintBottom_toTopOf="@+id/gridLayout"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

我将您的 XML 和 Java 代码复制到 Android Studio 中的新项目中并复制了错误。看起来您没有正确初始化 ArrayList winner,当您的代码尝试替换 ArrayList 中的值时导致 IndexOutOfBoundsException。

在你的代码开头你有:

    List<Integer> winner = new ArrayList<>(9);

但是,ArrayList 在这个阶段是空的,因此当您的 pgame() 方法尝试按照下面的示例调用 set() 方法时,它失败了:

    winner.set(0, 2); 

请注意,set() 是一种替换 ArrayList 中现有值的方法,正如我提到的,ArrayList 是空的,因此没有要替换的值。

将以下行添加到 onCreate 方法中以通过添加空白值来初始化 ArrayList(这并不优雅,但应该可以解决问题):

    winner.add(0);
    winner.add(0);
    winner.add(0);
    winner.add(0);
    winner.add(0);
    winner.add(0);
    winner.add(0);
    winner.add(0);
    winner.add(0);