A simple Tic-Tac-Toe board is a 3 × 3 array filled with either X’s, O’s, or blanks.
Here is a class for a game of Tic-Tac-Toe:
public class TicTacToe
{
private String[][] board;
private static final int ROWS = 3;
private static final int COLS = 3;
/** Construct an empty board. */
public TicTacToe()
{
board = new String[ROWS][COLS];
for (int r = 0; r < ROWS; r++)
for (int c = 0; c < COLS; c++)
board[r][c] = " ";
}
/** @param r the row number
* @param c the column number
* @param symbol the symbol to be placed on board[r][c]
* Precondition: The square board[r][c] is empty.
* Postcondition: symbol placed in that square.
*/
public void makeMove(int r, int c, String symbol)
{
board[r][c] = symbol;
}
/** Creates a string representation of the board, e.g.
* |o |
* |xx |
* | o|
* @return the string representation of board
*/
public String toString()
{
String s = ""; //empty string
/* more code */
return s;
}
}
Which segment represents a correct replacement for
/* more code */ for the toString method?
(A)
for (int r = 0; r < ROWS; r++)
{
for (int c = 0; c < COLS; c++)
{
s = s + "|";
s = s + board[r][c];
s = s + "|\n";
}
}
(B)
for (int r = 0; r < ROWS; r++)
{
s = s + "|";
for (int c = 0; c < COLS; c++)
{
s = s + board[r][c];
s = s + "|\n";
}
}
(C)
for (int r = 0; r < ROWS; r++)
{
s = s + "|";
for (int c = 0; c < COLS; c++)
s = s + board[r][c];
}
s = s + "|\n";
(D)
for (int r = 0; r < ROWS; r++)
s = s + "|";
for (int c = 0; c < COLS; c++)
{
s = s + board[r][c];
s = s + "|\n";
}
(E)
for (int r = 0; r < ROWS; r++)
{
s = s + "|";
for (int c = 0; c < COLS; c++)
s = s + board[r][c];
s = s + "|\n";
}