Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android Tic Tac Toe应用程序在按下按钮时崩溃_Java_Android - Fatal编程技术网

Java Android Tic Tac Toe应用程序在按下按钮时崩溃

Java Android Tic Tac Toe应用程序在按下按钮时崩溃,java,android,Java,Android,基本上,我们的任务是创建一个tic-tac-toe+1游戏,在这个游戏中,你可以获得三个连续,一个在上面或下面(如果你有三个水平的连续),或者在你的线的一侧(如果你有三个垂直的连续)。我知道我的赢条件检查有问题,只是不确定是什么,如果我现在点击任何按钮,它会立即崩溃。这是我的Java import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; im

基本上,我们的任务是创建一个tic-tac-toe+1游戏,在这个游戏中,你可以获得三个连续,一个在上面或下面(如果你有三个水平的连续),或者在你的线的一侧(如果你有三个垂直的连续)。我知道我的赢条件检查有问题,只是不确定是什么,如果我现在点击任何按钮,它会立即崩溃。这是我的Java

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

 // Tic-Tac-Toe plus 1
 // This version of Tic-Tac-Toe has a 4x4 grid
 // In order to win, a player must make a 3x2 "L"
 //    on the first or last element
 //    Example 1: X would win horizontal + 1
 //    XXXO
 //    O XO
 //    X  X
 //    O  O
 //
 //    Example 2: X would win horizontal + 1
 //    XXXO
 //    X OO
 //    O  X
 //    X  O
 //
 //    Example 3: X would win vertically + 1
 //    XOXO
 //    X OO
 //    XX X
 //    O
 //
 //    Example 4: X would win
 //    XOXO
 //    O XO
 //    OXX
 //    X  O
 //
 // *** IMPORTANT ***
 // make sure that you replace the line below with YOUR package statement

 // Make sure to use your class name (public class YourClassName)
 public class tttone extends AppCompatActivity {

// declare variables to reference any UI objects that you need to access
Button[][] board = new Button[4][4];
TextView winLoseTextView;

// declare instance variables
String whosTurn;
String winLoseText;
boolean gameOver = false;


// Called when the activity is first created.
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tttone);

    setTitle("Tic Tac Toe");
    whosTurn = "X";
    winLoseTextView = (TextView) findViewById(R.id.winlosetextview);
    board[0][0] = (Button) findViewById(R.id.button00);
    board[0][1] = (Button) findViewById(R.id.button01);
    board[0][2] = (Button) findViewById(R.id.button02);
    board[0][3] = (Button) findViewById(R.id.button03);
    board[1][0] = (Button) findViewById(R.id.button10);
    board[1][1] = (Button) findViewById(R.id.button11);
    board[1][2] = (Button) findViewById(R.id.button12);
    board[1][3] = (Button) findViewById(R.id.button13);
    board[2][0] = (Button) findViewById(R.id.button20);
    board[2][1] = (Button) findViewById(R.id.button21);
    board[2][2] = (Button) findViewById(R.id.button22);
    board[2][3] = (Button) findViewById(R.id.button23);
    board[3][0] = (Button) findViewById(R.id.button30);
    board[3][1] = (Button) findViewById(R.id.button31);
    board[3][2] = (Button) findViewById(R.id.button32);
    board[3][3] = (Button) findViewById(R.id.button33);
    // FINISH ME
    // add more of these
    whosTurn = "X";
    // FINISH ME
    // add variable for winlose text
    winLoseText = "";

}

public void buttonClick(View v) {
    if (gameOver) {
        return;
    } else {
        for (int r = 0; r < 4; r++) {
            for (int c = 0; c < 4; c++) {
                if (v == board[r][c]) {
                    if (board[r][c].getText().equals(" ")) {
                        board[r][c].setText(whosTurn);
                        if (whosTurn.equals("X")) {
                            whosTurn = "O";
                            winLoseTextView.setText(isWinner("X"));
                            winLoseTextView.setVisibility(View.VISIBLE);
                        } else {
                            whosTurn = "X";
                            winLoseTextView.setText(isWinner("O"));
                            winLoseTextView.setVisibility(View.VISIBLE);
                        }
                    }
                }

            }
        }

    }

}
    // FINISH   ME
    // use double for loops to loop through the all the rows and columns of the board
    //   determine which button was clicked by using  if (v == board[#][#])
    //      make sure the button that was clicked is empty (i.e. it's text == " ")
    //        set the button text to the correct character
    //        determine if there is a winner or not



public boolean isValid(int r, int c) {
    // FINISH ME
    // return true if (r,c) is on the board else return false
    if (r >= 0 && r <= 3 && c <= 3 && r >= 0) {
        return true;
    }
    else {
        return false;
    }
}

public boolean isPlayer(String player, int r, int c)
{
    if (!isValid(r,c))
        return false;

    // FINISH ME
    // return true if this player is in the (r,c) position else return false
    if (board[r][c].getText().toString().equals(player)){
        return true;
    }
    else {
        return false;
    }
}

public boolean isPlayerAbove(String player, int r, int c) {
    // FINISH ME
    // return true if this player is in the (r,c) position else return false
    if (isValid(r - 1, c)){
        if (board[r - 1][c].getText().toString().equals(player)) {
            return true;
        } else {
            return false;
        }
    }
    return false;
}


public boolean isPlayerBelow(String player, int r, int c)
{
    // FINISH ME
    // return true if this player is in the (r,c) position else return false
    if (isValid(r+1,c)) {
        if (board[r + 1][c].getText().toString().equals(player)) {
            return true;
        } else {
            return false;
        }
    }
    return false;
}


public boolean isPlayerRight(String player, int r, int c) {
    // FINISH ME
    // return true if this player is in the (r,c) position else return false
    if (isValid(r, c + 1)) {
        if (board[r][c + 1].getText().toString().equals(player)) {
            return true;
        } else {
            return false;
        }
    }
    return false;
}


public boolean isPlayerLeft(String player, int r, int c)
{
    // FINISH ME
    // return true if this player is in the (r,c) position else return false
    if (isValid(r,c-1)) {
        if (board[r][c - 1].getText().toString().equals(player)) {
            return true;
        } else {
            return false;
        }

    }
    return false;
}


public boolean is3InARowHorizontal(String player, int r, int c)
    // FINISH ME
    // return true if player appears in positions (r,c) (r,c+1) and (r,c+2)
    // else return false
{
    if ((board[r][c].getText().toString().equals(player))&&(board[r][c+1].getText().toString().equals(player))&&(board[r][c+2].getText().toString().equals(player)) ){
        return true;
    }
    else {
        return false;
    }
}

public boolean is3InARowVertical(String player, int r, int c)
    // FINISH ME
    // return true if player appears in positions (r,c) (r+1,c) and (r+2,c)
    // else return false
    {

        if ((board[r][c].getText().toString().equals(player))&&(board[r+1][c].getText().toString().equals(player))&&(board[r+2][c].getText().toString().equals(player)) ){
            return true;
        }
        else {
            return false;
        }
    }




public String isWinner(String player)
{
    // FINISH ME
    // you should look for 3 (yes 3) in a row anywhere on the board (horizontal, vertical, but NO diagonals)
    // PLUS one more up or down (or left or right) on the first or last element
    // That is in order to win, a player must make a 3x2 "L" horizontally or vertically.
    // this method should return the empty String if there is no winner or cat's game
    //
    // YOU MUST CALL METHODS ABOVE IN YOUR SOLUTION
    // return "X Wins Horizontal", or "O Wins Horizontal", or
    //        "X Wins Vertical", or "O Wins Vertical" or
    //        "" if there are no winners (yet)


    // FINISH ME
    // check for vertical win

    for (int r = 0; r < 4; r++){
        for (int c = 0; c <4; c++){
            if ((is3InARowVertical(player, r, c))&&((isPlayerRight(player,r, c)))||((isPlayerLeft(player,r, c)))||((isPlayerLeft(player,r+2, c)))||((isPlayerRight(player,r+2, c)))){
                winLoseText =  player + " wins vertical";
            }
        }
    }
    for (int r = 0; r < 4; r++){
        for (int c = 0; c <4; c++){
            if ((is3InARowHorizontal(player, r, c))&&((isPlayerAbove(player,r, c)))||((isPlayerBelow(player,r, c)))||((isPlayerLeft(player,r+2, c)))||((isPlayerRight(player,r+2, c)))){
                winLoseText = player + " wins horizontal";
            }
        }
    }
    winLoseText = "meme";

    // FINISH ME
    // check for horizontal win
    return winLoseText;
}


public void clearBoard() {
    for (int r = 0; r < 4; r++) {
        for (int c = 0; c < 4; c++) {
            board[r][c].setText(" ");
        }
    }
}
    // FINISH ME
    // use two loops to set the Button objects to " "

public void newGameClick(View v)
{
    clearBoard();// FINISH ME
    whosTurn = "X";
    winLoseText = "";
    // call clearBoard() and do any other initialization
    // set whosTurn = 'X'
    // clear the message field

}

}

如果在isValid(int r,int c)函数中的if条件中有输入错误,请尝试以下操作:

public boolean isValid(int r, int c) {
    // FINISH ME
    // return true if (r,c) is on the board else return false
   // CHANGED'r >= 0' to 'c >= 0'
    if (r >= 0 && r <= 3 && c <= 3 && c >= 0) { 
        return true;
    }
    else {
        return false;
    }
}
公共布尔值有效(int r,int c){
//干掉我
//如果(r,c)在板上,则返回true,否则返回false
//将“r>=0”更改为“c>=0”

如果(r>=0&&r发布stacktrace请公布错误日志我现在在一台与正常情况不同的计算机上,因此我现在无法访问studio,但这是xml,如果您想自己运行它,非常感谢!原因:java.lang.ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException:length=4;index=-1
当c==0,c-1 == -1...
public boolean isValid(int r, int c) {
    // FINISH ME
    // return true if (r,c) is on the board else return false
   // CHANGED'r >= 0' to 'c >= 0'
    if (r >= 0 && r <= 3 && c <= 3 && c >= 0) { 
        return true;
    }
    else {
        return false;
    }
}