Waiting for answer This question has not been answered yet. You can hire a professional tutor to get the answer.

QUESTION

Part 1 - Completing the TicTac Toe Class The Tic Tac Toe class is nearly complete , except that the bodies of the print !' and tostring ! methods are...

Class TicTacToe

import java.util.*;

/**

* A class modelling a tic-tac-toe (noughts and crosses, Xs and Os) game.

*

* @author Lynn Marshall

* @version November 8, 2012

*/

public class TicTacToe

{

public static final String PLAYER_X = "X"; // player using "X"

public static final String PLAYER_O = "O"; // player using "O"

public static final String EMPTY = " "; // empty cell

public static final String TIE = "T"; // game ended in a tie

private String player; // current player (PLAYER_X or PLAYER_O)

private String winner; // winner: PLAYER_X, PLAYER_O, TIE, EMPTY = in progress

private int numFreeSquares; // number of squares still free

private String board[][]; // 3x3 array representing the board

/**

* Constructs a new Tic-Tac-Toe board.

*/

public TicTacToe()

{

board = new String[3][3];

}

/**

* Sets everything up for a new game. Marks all squares in the Tic Tac Toe board as empty,

* and indicates no winner yet, 9 free squares and the current player is player X.

*/

private void clearBoard()

{

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

board[i][j] = EMPTY;

}

}

winner = EMPTY;

numFreeSquares = 9;

player = PLAYER_X; // Player X always has the first turn.

}

/**

* Plays one game of Tic Tac Toe.

*/

public void playGame()

{

int row, col;

Scanner sc;

clearBoard(); // clear the board

// print starting board

print();

// loop until the game ends

while (winner==EMPTY) { // game still in progress

// get input (row and column)

while (true) { // repeat until valid input

System.out.print("Enter row and column of chosen square (0, 1, 2 for each): ");

sc = new Scanner(System.in);

row = sc.nextInt();

col = sc.nextInt();

if (row>=0 && row<=2 && col>=0 && col<=2 && board[row][col]==EMPTY) break;

System.out.println("Invalid selection, try again.");

}

board[row][col] = player; // fill in the square with player

numFreeSquares--; // decrement number of free squares

// see if the game is over

if (haveWinner(row,col))

winner = player; // must be the player who just went

else if (numFreeSquares==0)

winner = TIE; // board is full so it's a tie

// print current board

print();

// change to other player (this won't do anything if game has ended)

if (player==PLAYER_X)

player=PLAYER_O;

else

player=PLAYER_X;

}

}

/**

* Returns true if filling the given square gives us a winner, and false

* otherwise.

*

* @param int row of square just set

* @param int col of square just set

*

* @return true if we have a winner, false otherwise

*/

private boolean haveWinner(int row, int col)

{

// unless at least 5 squares have been filled, we don't need to go any further

// (the earliest we can have a winner is after player X's 3rd move).

if (numFreeSquares>4) return false;

// Note: We don't need to check all rows, columns, and diagonals, only those

// that contain the latest filled square. We know that we have a winner

// if all 3 squares are the same, as they can't all be blank (as the latest

// filled square is one of them).

// check row "row"

if ( board[row][0].equals(board[row][1]) &&

board[row][0].equals(board[row][2]) ) return true;

// check column "col"

if ( board[0][col].equals(board[1][col]) &&

board[0][col].equals(board[2][col]) ) return true;

// if row=col check one diagonal

if (row==col)

if ( board[0][0].equals(board[1][1]) &&

board[0][0].equals(board[2][2]) ) return true;

// if row=2-col check other diagonal

if (row==2-col)

if ( board[0][2].equals(board[1][1]) &&

board[0][2].equals(board[2][0]) ) return true;

// no winner yet

return false;

}

/**

* Prints the board to standard out using toString().

*/

public void print()

{

// something needs to be added here

}

/**

* Returns a string representing the current state of the game. This should look like

* a regular tic tac toe board, and be followed by a message if the game is over that says

* who won (or indicates a tie).

*

* @return String representing the tic tac toe game state

*/

public String toString()

{

return ""; // this needs to be updated

}

}

Class TicTacToeFrame

import java.util.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

/**

* A class modelling a tic-tac-toe (noughts and crosses, Xs and Os) game in a very

* simple GUI window.

*

* @author Lynn Marshall

* @version November 8, 2012

*/

public class TicTacToeFrame extends TicTacToe

{

private JTextArea status; // text area to print game status

/**

* Constructs a new Tic-Tac-Toe board and sets up the basic

* JFrame containing a JTextArea in a JScrollPane GUI.

*/

public TicTacToeFrame()

{

// add the necessary code here

}

/**

* Prints the board to the GUI using toString().

*/

public void print()

{

// add code here

}

}

Part 1 - Completing the TicTac Toe ClassThe Tic Tac Toe class is nearly complete , except that the bodies of the print !' andtostring ! ! methods are missing .1 . Complete the print ! ! method . It will just output the current Tic Tac Toe object . I'Mlint*This method is just one line long . All the &quot; work &quot; is in tostring (1 . ]2 .So that the print ! ! method works properly , you must now complete thetostring ! ! method . It should return &amp; textual representation of the current state of thetic tac - ture game . This textual representation will include the vertical and horizontal lineswe usually draw when playing this game on paper lie . you should use characters that will!approximate these lines , such as &quot; &quot; and &quot; _ &quot;. Note that the board field is a }{} array Of&quot;{ &quot;&amp;. &quot;O &quot;'s, and blanks. The indices of each square are as follows :`101 101 1 101 1 1 1 1 101 121- - - - - - -1 1 1 101 1 1 1 1 1 1 1 1 1 1/121121 101 1 121 1 1 1 1 121 12 1Note that you do not enter the &quot; [ &quot; or &quot; I &quot; when playing the game , just in writing the Javacoule .The board representation will include the winning player (or indicate a Lie ; if the game isover . The winner field is EMIFT'S if there is no winner (yet ) . and otherwise containsPLAYER_*. PLAYER_ O. &quot; THE Isee the constants defined at the top of the class !Here are some examples of what tostring ! ' should return .&quot;Start of the game !&amp; Part way through the game .3 11 # 1IEnd of the game*$ 1 # 1^` WINS
Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question