Limited access

Upgrade to access all content for this subject

A programmer has used functional decomposition to determine the methods needed to play a tic-tac-toe game. A player is selected at random to start the game. The tic-tac-toe grid is 3 rows by 3 columns. Each player alternates by adding their symbol, either “X’ or “O” to the grid. The player wins if they have three of their symbols in a row, either horizontally, vertically, or diagonally.

The following is the initial implementation of the TicTacToe class with the methods that the programmer has determined are necessary. Note that the programmer has decided to use only one instance variable.

public class TicTacToe 
{
    // 3 x 3 grid for playing tic-tac-toe
    private String[][] grid = new String[3][3];

    // No other instance variable are defined

    // Play one game of tic-tac-toe
    // Post-condition: one game of tic-tac-toe is completed
    public void playGame()
    {
          // Implementation to be selected below
    }

    // Player chooses the square on the grid to place their symbol.
    // Post-condition: grid is updated to include the symbol in the chosen 
    // square.
    public void playTurn(String symbol)
    {
        // Implementation not shown
    }

    // Randomly returns either "X" or "O" to determine the symbol that 
    // plays first.
    public String determineWhoGoesFirst()
    {
        // Implementation not shown
    }

    // Returns true if the player wins the game.  Else, it returns false.
    public boolean isWinner()
    {
        // Implementation not shown
    }

    // Returns the symbol whose turn it now is to play the game. 
    public String changeSymbol(String symbol)
    {
        if (symbol.equals("X"))
            return "O";
        else
            return "X";
    }
}

Using the design as specified above, which of the following segments of code would result in a successful implementation of playGame()?

A
public void playGame()
{
    String symbol = "X";
    while(!isWinner())
    {
        playTurn(symbol);
        changeSymbol(symbol);
    }
    System.out.println(symbol + " won the game");
}
B
public void playGame()
{
    String symbol = determineWhoGoesFirst();
    while(isWinner())
    {
        playTurn();
        changeSymbol(symbol);
    }
    symbol = changeSymbol(symbol);    
    System.out.println(symbol + " won the game");
}
C
public void playGame()
{
    String symbol = determineWhoGoesFirst();
    while(!isWinner())
    {
        playTurn(symbol);
        symbol = changeSymbol(symbol);
    }
    System.out.println(symbol + " won the game");
}
D
public void playGame()
{
    String symbol = determineWhoGoesFirst();
    if(!isWinner())
    {
        playTurn(symbol);
        symbol = changeSymbol(symbol);
    }
    System.out.println(symbol + " won the game");
}
E
public void playGame()
{
    String symbol = determineWhoGoesFirst();
    while(!isWinner())
    {
        playTurn(symbol);
        symbol = changeSymbol(symbol);
    }
    symbol = changeSymbol(symbol);    
    System.out.println(symbol + " won the game");
}
Select an assignment template