0

Hi I am a newbie at android and going through a tutorial for tictactoe. I need to save the state of the board so that when I change orientation the board appears with characters intact. Here are the pieces of code - The tries and turns strings are being saved and display but not the X's and O's on the board. I do not have a clue why

MainActivity.java

           mGame = new TicTacToeGame();

       if (savedInstanceState == null) { 
            startNewGame();
            } 
            else { 
             // Restore the game's state                    
            mGame.setBoardState(savedInstanceState.getCharArray("board"));              
            mGameOver = savedInstanceState.getBoolean("mGameOver"); 
            mInfoTextView.setText(savedInstanceState.getCharSequence("info")); 
            mHumanWins = savedInstanceState.getInt("mHumanWins"); 
            mComputerWins = savedInstanceState.getInt("mComputerWins"); 
            mTies = savedInstanceState.getInt("mTies"); 
            mTurn = savedInstanceState.getChar("mTurn"); 
            } 
            displayScores(); 

       }

        @Override
    protected void onSaveInstanceState(Bundle outState) {
     super.onSaveInstanceState(outState);
     outState.putCharArray("board", mGame.getBoardState());
     outState.putBoolean("mGameOver", mGameOver);
     outState.putInt("mHumanWins",Integer.valueOf(mHumanWins));
     outState.putInt("mComputerWins",Integer.valueOf(mComputerWins));
     outState.putInt("mTies", Integer.valueOf(mTies));
     outState.putCharSequence("info", mInfoTextView.getText());
     outState.putChar("mTurn", mTurn);
    }

Here are the methods on the tictactoe game returning the state of the board:

    public char[] getBoardState() {
    return mBoard;
}

public void setBoardState(char[] board) {
    mBoard = board.clone();
}

Thanks in advance for any help.

Beth Ann Meredith
  • 181
  • 1
  • 4
  • 10
  • Did you setBoardState before saving? – Hoan Nguyen Mar 15 '13 at 22:15
  • Could you explain a little further? – Beth Ann Meredith Mar 15 '13 at 22:36
  • Did you ever save your board state by calling setBoardState anywhere before onSaveInstanceState is called. For example you can log just the first value of mBoard in the onSaveInstanceState to see if there is anything there. – Hoan Nguyen Mar 15 '13 at 22:44
  • Thanks I will try that but I'm not very good at debugging because the debugger is very slow on my machine. Thanks again. – Beth Ann Meredith Mar 15 '13 at 23:03
  • All you have to do is to declare a class member like private static final String TAG = "Tic tac toe"; and then inside the onSaveInstanceState put the following Log.d(TAG, "mBoard first element = " + mBoard[0]); and look at your logcat to see what it print. – Hoan Nguyen Mar 15 '13 at 23:11
  • Thanks Hoan - it received the first element it is not passing it to the buttons – Beth Ann Meredith Mar 16 '13 at 16:39

2 Answers2

1

I think the problem might be in your setBoardGame method. The clone() method returns type "Object", but mBoard is type "char[]" (a character array). Therefore, you need to cast from type Object to type char[]

Try this:

public void setBoardState(char[] board) {
    mBoard = (char[]) board.clone();
}
0

Try

public char[] getBoardState() {
return mBoard.clone();

}

Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54