Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a class called TicTacToe to handle the basics of a two-player game of Tic-Tac-Toe. The required methods are below. You may add any additional

Write a class called TicTacToe to handle the basics of a two-player game of Tic-Tac-Toe. The required methods are below. You may add any additional methods you believe would aid the user of your class to play the game.

Instance Variables

-board

-a two-dimensional array of chars

-turns

-an integer keeping track of the number of turns played this game

Constructors

-TicTacToeClass()

the default constructor, which just creates the two-dimensional array and fills each slot with ' ' (a blank space) and initializes the other attributes

Accessors

-int getTurns()

returns the numbers of turns played so far

-char getPlayerAt( int r, int c )

returns the character representing the piece at the given location. Should return either ' ', 'X', or 'O'.

-String toString()

returns the string representation of the current board

-boolean isTied()

returns true if all nine spaces are filled AND neither X nor O has won

-boolean isWinner(char p)

returns true if the letter passed in currently has three in a row. That is, isWinner('X') will return true if X has won, and isWinner('O') will return true if O has won

-boolean isFull()

returns true if nine turns have been played and false otherwise

-boolean isValid( int r, int c )

returns true if the given row and column corresponds to a valid space on the board

Modifiers

-void playMove(char p, int r, int c)

allows the given player to place their move at the given row and column. The row and column numbers are 0-based, so valid numbers are 0, 1, or 2

A game in progress (example)

 X O O X X X O 'O', choose your location (row, column): 0 1 X O O O X X X O 'X', choose your location (row, column): 2 0 X O O O X X X X O The game is a tie.

Files Needed

PlayTicTacToe.java a main program to play TicTacToe (V3.2) (BELOW)

package tictactoe;
import java.util.Scanner;
public class PlayTicTacToe {
private static void playGame(Scanner keyboard) {
char p = 'X';
TicTacToe ttt = new TicTacToe();
int r, c;
do {
System.out.println(ttt);
do {
System.out.print("'" + p + "', choose your location (row, column): ");
try {
r = keyboard.nextInt();
c = keyboard.nextInt();
if (!ttt.isValid(r, c))
System.out.println("That is not a valid location. Try again.");
else if (ttt.playerAt(r, c) != ' ')
System.out.println("That location is already full. Try again.");
else
break;
}
catch (Exception e) {
System.out.println("Bad Integer Entered. Try Again.");
keyboard.nextLine(); //IMPORTANT! Clear keyboard!!!
}
} while (true);
ttt.playMove(p, r, c);
if (p == 'X')
p = 'O';
else
p = 'X';
} while (!ttt.isWinner('X') && !ttt.isWinner('O') && !ttt.isFull());
System.out.println(ttt);
String status;
if (ttt.isWinner('X'))
status = "X is the winner!";
else if (ttt.isWinner('O'))
status = "O is the winner!";
else
status = "The game is a tie.";
status += " After " + ttt.getTurns() + " plays.";
System.out.println(status);
}
// **********************************************
private static void process(Scanner sc, String args[]) {
playGame(sc);
sc.nextLine(); // IMPORTANT!! Reset Scanner
}
// **********************************************
private static boolean doThisAgain(Scanner sc, String prompt) {
System.out.print(prompt);
String doOver = sc.nextLine();
return doOver.equalsIgnoreCase("Y");
}
// **********************************************
public static void main(String args[]) {
final String TITLE = "Play Tic Tac Toe V1.0";
final String CONTINUE_PROMPT = "Play again? [y/N] ";
System.out.println("Welcome to " + TITLE);
Scanner sc = new Scanner(System.in);
do {
process(sc, args);
} while (doThisAgain(sc, CONTINUE_PROMPT));
sc.close();
System.out.println("Thank you for using " + TITLE);
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Icdt 88 2nd International Conference On Database Theory Bruges Belgium August 31 September 2 1988 Proceedings Lncs 326

Authors: Marc Gyssens ,Jan Paredaens ,Dirk Van Gucht

1st Edition

3540501711, 978-3540501718

More Books

Students also viewed these Databases questions

Question

Identify the potential uses of molecular genetics research.

Answered: 1 week ago