嗨,我是 android 的新手,正在阅读 tictactoe 的教程。我需要保存电路板的状态,以便当我改变方向时,电路板的字符完好无损。这是代码片段 - 尝试和转折字符串正在保存和显示,但不是板上的 X 和 O。我不知道为什么
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);
}
以下是tictactoe游戏返回棋盘状态的方法:
public char[] getBoardState() {
return mBoard;
}
public void setBoardState(char[] board) {
mBoard = board.clone();
}
提前感谢您的帮助。