有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法在Android Studio中调试错误(很可能是逻辑错误)

package com.example.sys.tictactoe2;

import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.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("");
    }
}
}

这是我在Android Studio中的主要活动文件(重构为GameBoard)。我正在尝试建立一个Tic-Tac-Toe游戏。当我试图在安卓设备上运行此应用程序时,它会崩溃,并显示消息“不幸的是,应用程序已停止”

根据我的搜索,我试图删除设备上的缓存并重建安卓项目。控制台根本没有显示任何错误。一定是逻辑错误。请告诉我,我哪里做错了。还有,有人能建议我如何发现逻辑错误吗


共 (0) 个答案