2014-02-14 133 views
1

我是一個嘗試將android井字遊戲應用程序放在一起的java新手。 我有下面的代碼,但不斷收到有關我在監聽器類中使用變量的錯誤。我的直覺是它可能是一個範圍問題,但我不知道如何解決它。下面是完整的代碼:mBoardButtons無法解析爲變量

------------snip--------------------- 

package edu.harding.tictactoe; 

import android.app.Activity; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 


public class AndroidTicTacToeActivity extends Activity { 

    // Represents the internal state of the game 
    //a variable to make the game logic available in the Activity class - Elton. 
    private TicTacToeGame mGame; 

    // Buttons making up the board - Elton 
    private Button mBoardButtons[]; 

    // Various text displayed - Elton 
    private TextView mInfoTextView; 

    public int BOARD_SIZE = 8; 




    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_android_tic_tac_toe); 

     mBoardButtons = new Button[BOARD_SIZE]; //cheat by creating a local variable BOARD_SIZE. 
     mBoardButtons[0] = (Button) findViewById(R.id.one); 
     mBoardButtons[1] = (Button) findViewById(R.id.two); 
     mBoardButtons[2] = (Button) findViewById(R.id.three); 
     mBoardButtons[3] = (Button) findViewById(R.id.four); 
     mBoardButtons[4] = (Button) findViewById(R.id.five); 
     mBoardButtons[5] = (Button) findViewById(R.id.six); 
     mBoardButtons[6] = (Button) findViewById(R.id.seven); 
     mBoardButtons[7] = (Button) findViewById(R.id.eight); 
     mBoardButtons[8] = (Button) findViewById(R.id.nine); 

     mInfoTextView = (TextView) findViewById(R.id.information); 

     mGame = new TicTacToeGame(); 

     startNewGame(); //start a new game - Elton. 

    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.android_tic_tac_toe, menu); 
     return true; 
    } 

    //a method to start anew game - Elton 
// Set up the game board. 
    private void startNewGame() {  

     mGame.clearBoard(); //clear the board - Elton 

     // Reset all buttons 
     for (int i = 0; i < mBoardButtons.length; i++) { 
      mBoardButtons[i].setText(""); 
      mBoardButtons[i].setEnabled(true);  
      mBoardButtons[i].setOnClickListener(new ButtonClickListener(i)); 
     } 

    // Human goes first 
     mInfoTextView.setText("You go first."); 
     } 

    private void setMove(char player, int location) { 

     mGame.setMove(player, location); 
     mBoardButtons[location].setEnabled(false); 
     mBoardButtons[location].setText(String.valueOf(player)); 
     if (player == TicTacToeGame.HUMAN_PLAYER) 
      mBoardButtons[location].setTextColor(Color.rgb(0, 200, 0));    
     else 
      mBoardButtons[location].setTextColor(Color.rgb(200, 0, 0)); 
    } 

} 

//Handles clicks on the game board buttons 
class ButtonClickListener implements View.OnClickListener { 
int location; 

public ButtonClickListener(int location) { //this is a constructor. 
    this.location = location; 
} 

public void onClick(View view) { 
    if (mBoardButtons[location].isEnabled()) { 
      setMove(TicTacToeGame.HUMAN_PLAYER, location);    

      // If no winner yet, let the computer make a move 
      int winner = mGame.checkForWinner(); 
      if (winner == 0) { 
      mInfoTextView.setText("It's Android's turn."); 
      int move = mGame.getComputerMove(); 
      setMove(TicTacToeGame.COMPUTER_PLAYER, move); 
      winner = mGame.checkForWinner(); 
     } 

     if (winner == 0) 
      mInfoTextView.setText("It's your turn."); 
     else if (winner == 1) 
       mInfoTextView.setText("It's a tie!"); 
      else if (winner == 2) 
       mInfoTextView.setText("You won!"); 
     else 
       mInfoTextView.setText("Android won!"); 
    } 
}  
} 


-----------------snap---------------------------------------- 



The errors I get are: 

mBoardButtons cannot be resolved to a variable 
The method setMove(char, int) is undefined for the type ButtonClickListener 
mGame cannot be resolved 
mInfoTextView cannot be resolved 
The method setMove(char, int) is undefined for the type ButtonClickListener 

All the above errors appear in the onClick() method of the listener. 

Please help. 

回答

0

mGame因爲ButtonClickListener類,但在AndroidTicTacToeActivity沒有定義不能得到解決。你應該從不同的部分訪問遊戲:通常在遊戲中,你需要將遊戲狀態類作爲單例實現。

mInfoTextView原因相同,不屬於ButtonClickListener類定義的一部分,因此您無法直接訪問它。

setMove以及AndroidTicTacToeActivity類的私有方法,所以您不能在該範圍之外使用它。

您應該仔細檢查遊戲和類的設計,可能會在整個架構中創建不同的「模塊」,並暴露其他組件使用的正確功能和屬性。

+0

謝謝您的回覆。我如何給它訪問?我認爲問題的確是我不確定ButtonClickListener類。爲什麼java中的監聽器被聲明爲類中的類。我是否以正確的方式去做? – Elton

+0

這不是強制性的創建一個單獨的類,但你可以定義它「內聯」在這裏的例子:http://developer.android.com/reference/android/widget/Button.html。然後,您可以將遊戲邏輯的代碼放入方法中,並在onClick中調用它。 – fasteque