无法在Android Studio中调试错误(很可能是逻辑错误) package com.example.sys.tictactoe2; 导入android.support.v7.app.AppActivity; 导入android.os.Bundle; 导入android.view.view; 导入android.widget.Button; 导入android.widget.TextView; 导入java.util.Random; 公共类GameBoard扩展AppCompative活动{ INTC[][]; int i,j=0; 按钮b[][]; 按钮新游戏; 哎呀,; 人类; 文本视图文本视图; //启动应用程序 @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity\u game\u board); 立根板(); 随机移动(); } //随机游戏者先移动 公共布尔随机移动(){ Random firstMove=新随机(); int player=firstMove.nextInt(2); 如果(玩家==1){ setText(“你先玩!”); human.humanTurn(); 返回true; }否则{ setText(“我先玩!”); ai.aiTurn(); 返回false; } } //启动游戏板 公众谘询委员会({ ai=新ai(); b=新按钮[4][4]; c=新整数[4][4]; newgame=(按钮)findviewbyd(R.id.New); setOnClickListener(newnewClickListener()); textView=(textView)findViewById(R.id.message); b[1][1]=(按钮)findViewById(R.id.button1); b[1][2]=(按钮)findViewById(R.id.button2); b[1][3]=(按钮)findViewById(R.id.button3); b[2][1]=(按钮)findViewById(R.id.button4); b[2][2]=(按钮)findViewById(R.id.button5); b[2][3]=(按钮)findViewById(R.id.button6); b[3][1]=(按钮)findViewById(R.id.button7); b[3][2]=(按钮)findViewById(R.id.button8); b[3][3]=(按钮)findViewById(R.id.button9); 对于(i=1;i

无法在Android Studio中调试错误(很可能是逻辑错误) package com.example.sys.tictactoe2; 导入android.support.v7.app.AppActivity; 导入android.os.Bundle; 导入android.view.view; 导入android.widget.Button; 导入android.widget.TextView; 导入java.util.Random; 公共类GameBoard扩展AppCompative活动{ INTC[][]; int i,j=0; 按钮b[][]; 按钮新游戏; 哎呀,; 人类; 文本视图文本视图; //启动应用程序 @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity\u game\u board); 立根板(); 随机移动(); } //随机游戏者先移动 公共布尔随机移动(){ Random firstMove=新随机(); int player=firstMove.nextInt(2); 如果(玩家==1){ setText(“你先玩!”); human.humanTurn(); 返回true; }否则{ setText(“我先玩!”); ai.aiTurn(); 返回false; } } //启动游戏板 公众谘询委员会({ ai=新ai(); b=新按钮[4][4]; c=新整数[4][4]; newgame=(按钮)findviewbyd(R.id.New); setOnClickListener(newnewClickListener()); textView=(textView)findViewById(R.id.message); b[1][1]=(按钮)findViewById(R.id.button1); b[1][2]=(按钮)findViewById(R.id.button2); b[1][3]=(按钮)findViewById(R.id.button3); b[2][1]=(按钮)findViewById(R.id.button4); b[2][2]=(按钮)findViewById(R.id.button5); b[2][3]=(按钮)findViewById(R.id.button6); b[3][1]=(按钮)findViewById(R.id.button7); b[3][2]=(按钮)findViewById(R.id.button8); b[3][3]=(按钮)findViewById(R.id.button9); 对于(i=1;i,java,arrays,android-studio-2.2,Java,Arrays,Android Studio 2.2,我们确实需要更多信息。我将首先向我们显示布局文件。错误可能在其中。或者错误可能在以下两种方法之一中: 立根板() 随机移动() 调用human.humanMove()时,randomMove()中可能会出现此错误。您尚未为human对象指定值,因此它们认为该值为null 我建议: package com.example.sys.tictactoe2; import android.support.v7.app.AppCompatActivity; import android.os.Bun

我们确实需要更多信息。我将首先向我们显示布局文件。错误可能在其中。或者错误可能在以下两种方法之一中:

  • 立根板()
  • 随机移动()
调用human.humanMove()时,randomMove()中可能会出现此错误。您尚未为human对象指定值,因此它们认为该值为null

我建议:

package com.example.sys.tictactoe2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;

public class GameBoard extends AppCompatActivity {
int c[][];
int i,j=0;
Button b[][];
Button newgame;
AI ai;
Human human;
TextView textView;

//Initiates the application
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_board);

    setBoard();
    randomMove();
}

// Random player moves first
public boolean randomMove(){
    Random firstMove = new Random();
    int player = firstMove.nextInt(2);

    if (player == 1) {
        textView.setText("You play first!");
        human.humanTurn();
        return true;

    } else {
        textView.setText("I play first!");
        ai.aiTurn();
        return false;
    }
}

//Initiates the Game Board
public void setBoard(){
ai = new AI();
    b = new Button[4][4];
    c = new int[4][4];

    newgame = (Button) findViewById(R.id.New);
    newgame.setOnClickListener(new NewClickListener());

    textView = (TextView) findViewById(R.id.message);

    b[1][1] = (Button) findViewById(R.id.button1);
    b[1][2] = (Button) findViewById(R.id.button2);
    b[1][3] = (Button) findViewById(R.id.button3);

    b[2][1] = (Button) findViewById(R.id.button4);
    b[2][2] = (Button) findViewById(R.id.button5);
    b[2][3] = (Button) findViewById(R.id.button6);

    b[3][1] = (Button) findViewById(R.id.button7);
    b[3][2] = (Button) findViewById(R.id.button8);
    b[3][3] = (Button) findViewById(R.id.button9);

    for (i=1; i<=3;i++){
        for (j=1;j<=3;j++)
            c[i][j] = 2;
        if (!b[i][j].isEnabled()) {
            b[i][j].setText("");
            b[i][j].setEnabled(true);
        }
    }
}

//If human plays first, class MyClickListener is initialized
public class Human {
    private void humanTurn() {
        for (i = 1; i <= 3; i++) {
            for (j = 1; j <= 3; j++) {
                b[i][j].setOnClickListener(new MyClickListener(i, j));
            }
        }
    }
}

public class MyClickListener implements View.OnClickListener{
    int x ;
    int y;

    private MyClickListener (int x, int y){
        this.x = x;
        this.y = y;
    }

    public void onClick(View View){
        if (b[x][y].isEnabled()){
            b[x][y].setEnabled(false);
            b[x][y].setText("O");
            c[x][y] = 0;
            textView.setText("");
            checkBoard();
            if(!checkBoard()){
               ai.aiTurn();
            }
      }
    }
}

public class AI {
    private void aiTurn(){
        if(c[1][1]==2 &&
        ((c[1][2]==0 && c[1][3]==0) ||
                (c[2][2]==0 && c[3][3]==0) ||
                (c[2][1])==0 && c[3][1]==0)){
            markSquare(1,1);
        }  else if (c[1][2]==2 &&
                ((c[2][2]==0 && c[3][2]==0) ||
                        (c[1][1]==0 && c[1][3]==0))) {
            markSquare(1,2);
        } else if(c[1][3]==2 &&
                ((c[1][1]==0 && c[1][2]==0) ||
                        (c[3][1]==0 && c[2][2]==0) ||
                        (c[2][3]==0 && c[3][3]==0))) {
            markSquare(1,3);
        } else if(c[2][1]==2 &&
                ((c[2][2]==0 && c[2][3]==0) ||
                        (c[1][1]==0 && c[3][1]==0))){
            markSquare(2,1);
        } else if(c[2][2]==2 &&
                ((c[1][1]==0 && c[3][3]==0) ||
                        (c[1][2]==0 && c[3][2]==0) ||
                        (c[3][1]==0 && c[1][3]==0) ||
                        (c[2][1]==0 && c[2][3]==0))) {
            markSquare(2,2);
        } else if(c[2][3]==2 &&
                ((c[2][1]==0 && c[2][2]==0) ||
                        (c[1][3]==0 && c[3][3]==0))) {
            markSquare(2,3);
        } else if(c[3][1]==2 &&
                ((c[1][1]==0 && c[2][1]==0) ||
                        (c[3][2]==0 && c[3][3]==0) ||
                        (c[2][2]==0 && c[1][3]==0))){
            markSquare(3,1);
        } else if(c[3][2]==2 &&
                ((c[1][2]==0 && c[2][2]==0) ||
                        (c[3][1]==0 && c[3][3]==0))) {
            markSquare(3,2);
        } else if( c[3][3]==2 &&
                ((c[1][1]==0 && c[2][2]==0) ||
                        (c[1][3]==0 && c[2][3]==0) ||
                        (c[3][1]==0 && c[3][2]==0))) {
            markSquare(3, 3);
        } else {
            Random rand = new Random();

            int a = rand.nextInt(4);
            int b = rand.nextInt(4);
            while (a==0 || b==0 || c[a][b]!=2) {
                a = rand.nextInt(3);
                b = rand.nextInt(3);
            }
            markSquare(a,b);
        }
        }
    private void markSquare(int x, int y){
        b[x][y].setEnabled(false);
        b[x][y].setText("X");
        c[x][y] = 1;
        checkBoard();
        if(!checkBoard()){
            human.humanTurn();
        }
    }

}

//Check for the winner
public boolean checkBoard(){
    boolean gameOver=false;
    if ((c[1][1]==0 && c[2][2]==0 && c[3][3]==0) ||
            (c[1][3]==0 && c[2][2]==0 && c[3][1]==0) ||
            (c[1][2]==0 && c[2][2]==0 && c[3][2]==0) ||
            (c[1][3]==0 && c[2][3]==0 && c[3][3]==0) ||
            (c[1][1]==0 && c[1][2]==0 && c[1][3]==0) ||
            (c[2][1]==0 && c[2][2]==0 && c[2][3]==0) ||
            (c[3][1]==0 && c[3][2]==0 && c[3][3]==0) ||
            (c[1][1]==0 && c[2][1]==0 && c[3][1]==0)){
        textView.setText("Game over. You win!");
        gameOver = true;

    } else if ((c[1][1]==1 && c[2][2]==1 && c[3][3]==1) ||
            (c[1][3]==1 && c[2][2]==1 && c[3][1]==1) ||
            (c[1][2]==1 && c[2][2]==1 && c[3][2]==1) ||
            (c[1][3]==1 && c[2][3]==1 && c[3][3]==1) ||
            (c[1][1]==1 && c[1][2]==1 && c[1][3]==1) ||
            (c[2][1]==1 && c[2][2]==1 && c[2][3]==1) ||
            (c[3][1]==1 && c[3][2]==1 && c[3][3]==1) ||
            (c[1][1]==1 && c[2][1]==1 && c[3][1]==1)){
        textView.setText("Game over. You lost!");
        gameOver = true;
    } else {
        boolean empty = false;
        for (i=1; i<=3; i++) {
            for (j = 1; j <= 3; j++) {
                if (c[i][j] == 2) {
                    empty = true;
                    break;
                }
            }
        }
        if(!empty){
            gameOver=true;
            textView.setText("Game over. It's a draw!");
        }
    }
    return gameOver;
}

//New Button resets the game
class NewClickListener implements View.OnClickListener{

    private NewClickListener(){
    }

    @Override
    public void onClick(View v) {
        setBoard();
        textView.setText("");
    }
}
}
虽然我认为setBoard()不会抛出致命错误,但我可以看出您已经习惯了基于非零的索引。您的数组是1个元素到大。例如,您应该声明一个包含三个元素的数组,如下所示:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_board);

    human = new Human();
    setBoard();
    randomMove();
}
并访问以下所有元素:

b = new Button[3][3];
所以你可以看到索引2实际上是第三个元素


我还建议您创建一个通用的玩家类,并让AI和Human继承该类-这将使您更容易确保两个玩家按照相同的规则行事,而不是每次引入新的规则时都必须执行两次规则。

要么学习使用调试器,要么添加一些日志记录,以便您可以缩小失败的范围-在最后一条消息出现和下一条消息没有出现之间的某个地方,“还有,有人可以建议我,一般来说,如何查找逻辑错误吗?”是的。使用IDE的内置调试器,一步一步地检查代码。将期望与现实相比较。
b[0][0] = ...; 
b[0][1] = ...; 
b[0][2] = ...; 
b[1][0] = ...;