2017-04-09 39 views
0

從本質上講,我們的任務是創建一個井字遊戲+1遊戲,您通過獲取連續三個以及一個以上或以下(如果您有3行水平)或行的一側(如果你有3行垂直)。我知道我的勝利條件檢查有什麼問題,只是不知道是什麼,如果我現在單擊任何按鈕,它立即崩潰。這是我的Java,Android Tic-Tac-Toe應用程序崩潰按鈕推

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 

} 

} 

任何幫助表示讚賞! 以下是錯誤日誌

04-09 19:27:26.197 2390-2390/? E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.example.owner.tttone, PID: 2390 
java.lang.IllegalStateException: Could not execute method for android:onClick 
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) 
    at android.view.View.performClick(View.java:4438) 
    at android.view.View$PerformClick.run(View.java:18422) 
    at android.os.Handler.handleCallback(Handler.java:733) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:136) 
    at android.app.ActivityThread.main(ActivityThread.java:5017) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
    at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.reflect.InvocationTargetException 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
    at android.view.View.performClick(View.java:4438)  
    at android.view.View$PerformClick.run(View.java:18422)  
    at android.os.Handler.handleCallback(Handler.java:733)  
    at android.os.Handler.dispatchMessage(Handler.java:95)  
    at android.os.Looper.loop(Looper.java:136)  
    at android.app.ActivityThread.main(ActivityThread.java:5017)  
    at java.lang.reflect.Method.invokeNative(Native Method)  
    at java.lang.reflect.Method.invoke(Method.java:515)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)  
    at dalvik.system.NativeStart.main(Native Method)  
    Caused by: java.lang.ArrayIndexOutOfBoundsException: length=4; index=-1 
    at com.example.owner.tttone.tttone.isPlayerLeft(tttone.java:197) 
    at com.example.owner.tttone.tttone.isWinner(tttone.java:257) 
    at com.example.owner.tttone.tttone.buttonClick(tttone.java:98) 
    at java.lang.reflect.Method.invokeNative(Native Method)  
    at java.lang.reflect.Method.invoke(Method.java:515)  
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)  
    at android.view.View.performClick(View.java:4438)  
    at android.view.View$PerformClick.run(View.java:18422)  
    at android.os.Handler.handleCallback(Handler.java:733)  
    at android.os.Handler.dispatchMessage(Handler.java:95)  
    at android.os.Looper.loop(Looper.java:136)  
    at android.app.ActivityThread.main(ActivityThread.java:5017)  
    at java.lang.reflect.Method.invokeNative(Native Method)  
    at java.lang.reflect.Method.invoke(Method.java:515)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)  
    at dalvik.system.NativeStart.main(Native Method) 

04-09 19:27:26.197 1678-1812/system_process W/ActivityManager: Force finishing activity com.example.owner.tttone/.tttone 
+0

張貼堆棧跟蹤,請 – quiro

+0

發佈錯誤日誌 – Kaushal28

+0

我在一臺不同於普通的電腦上,所以我現在無法訪問工作室,但是如果您想自己運行它,請繼續使用xml,非常感謝! –

回答

0

你在的isValid(INT R,INT C)功能時,您如果病情有一個錯字,試試這個:

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