You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/* * Created by Zhongyi on 06/11/2016. * TicTacToe with multiple-step undo and redo. * Example code for Lab 7. */importjava.util.Scanner;
/** * Class TicTacToe is the entry point of the game. */publicclassTicTacToe {
publicstaticvoidmain(String[] args) {
char[][][] boardHistory = newchar[10][][];
// What are these variables used for? Attach your answer as part of the code review.intcurrentStep = 0, lastStep = 0;
Scannerscanner = newScanner(System.in);
// Initial state of the game, as step 0boardHistory[0] = newchar[][]{{'.', '.', '.'}, {'.', '.', '.'}, {'.', '.', '.'}};
/* * In an legal game, there shall be up to nine steps. * Considering the initial state, the step varies from 0 to 9, inclusively. */while (currentStep <= 9) {
printBoard(boardHistory[currentStep]);
StringnextMove = scanner.next();
/* * If the input is an undo or redo command, its length is the steps to jump. * We can calculate the nextStep based on that. * */intnextStep;
if (nextMove.charAt(0) == 'r') {
// RedonextStep = currentStep + nextMove.length();
currentStep = redo(currentStep, lastStep, nextStep);
} elseif (nextMove.charAt(0) == 'u') {
// UndonextStep = currentStep - nextMove.length();
currentStep = undo(currentStep, nextStep);
} else {
// Drop a new piece.// Parse the input string to integer to get the position to drop the piece.intnextPosition = Integer.parseInt(nextMove);
// Update lastStep as long as currentSteplastStep = ++currentStep;
// Store the current state into boardHistory,// by deep copying the previous state and substitute the position with new piece.boardHistory[currentStep] = copyBoard(boardHistory[currentStep - 1]);
boardHistory[currentStep][nextPosition / 3][nextPosition % 3] = (currentStep % 2 != 0) ? 'O' : 'X';
}
}
}
/** * Redo from currentStep to nextStep. * Throw an error message to stdout if the move is illegal. * * @param currentStep the step to jump from * @param nextStep the step to jump to * @param lastStep the maximum step to jump * @return update currentStep to nextStep, if the move is legal; or change nothing */privatestaticintredo(intcurrentStep, intlastStep, intnextStep) {
if (nextStep <= lastStep) {
currentStep = nextStep;
} else {
System.out.println("Invalid redo.");
}
returncurrentStep;
}
/** * Undo from currentStep to nextStep. * Throw an error message to stdout if the move is illegal. * * @param currentStep the step to jump from * @param nextStep the step to jump to * @return update currentStep to nextStep, if the move is legal; or change nothing */privatestaticintundo(intcurrentStep, intnextStep) {
if (nextStep >= 0) {
currentStep = nextStep;
} else {
System.out.println("Invalid undo.");
}
returncurrentStep;
}
/** * Iterate over the two dimensional array, to print the board. * Then, prompt a message to ask user for the next input. */privatestaticvoidprintBoard(char[][] board) {
for (inti = 0; i < 3; i++) {
for (intj = 0; j < 3; j++) {
System.out.print(board[i][j]);
}
System.out.println();
}
System.out.print("Next move: ");
}
/** * Use a nested iteration to deep copy a 2D array, i.e. the board. */privatestaticchar[][] copyBoard(char[][] array) {
char[][] newArray = newchar[3][3];
for (inti = 0; i < 3; i++) {
for (intj = 0; j < 3; j++) {
newArray[i][j] = array[i][j];
}
}
returnnewArray;
}
}
Lab 7 Code Review
样例代码
思考
boardHistory[0]
的含义是什么?boardHistory[0][0]
呢?currentStep
和lastStep
分别有什么作用?它们的值在游戏过程中是如何变化的?lastStep
是否可以省略?copyBoard
方法,而不直接使用boardHistory[currentStep] = boardHistory[currentStep - 1];
语句?The text was updated successfully, but these errors were encountered: