Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Java please Write a class named TTTBoard. Its purpose is to model a Tic-Tac-Toe game board. We will refer to each position of the

In Java please

Write a class named TTTBoard. Its purpose is to model a Tic-Tac-Toe game board.

We will refer to each position of the TTT board as an ordered pair (r,c) where the upper-left position of the board is at (0,0) and r increases downward and c increases to the right. So, the following board has an X at position (2,1) and an O at position (1,0).

 | | -+-+- O| | -+-+- |X| 

The following public methods should be supported in TTTBoard:

 public TTTBoard(int size) public TTTBoard() public char get(int r, int c) public void set(int r, int c, char ch) public int size() public char winner() public String toString() 

One constructor should take an int parameter indicating the size of the TTT board to be used (in case someone wanted to play a variation of TTT where a player needed, say, four-in-a-row to win). The other constructor should take no arguments, indicating that a standard size 3 board should be used. If size is less than 1, your constructor should throw an IllegalArgumentException.

The method get should return the character that is at position (r,c) or a space character ' ' if no character is at that position. The method set should set position (r,c) to be character ch. It should not be considered an error to use set to change an already set character to another one, including back to a space. If (r,c) is out-of-bounds, throw an IndexOutOfBoundsException.

The method size should return the dimension of the TTT board. It will be whatever size was passed to the constructor, or 3 if no value was passed to the constructor. All values for r and c in the range 0..size()-1should be legal for set and get.

The method winner should determine whether there are size() of the same character in a row, either horizontally, vertically, or diagonally. If there is, the found character should be returned, otherwise a space character ' ' should be returned. If more than one winning sequence is found, you may return any of them.

The method toString should return a string representation of the board. For example, the board shown above as a single string is " | | -+-+- O| | -+-+- |X| ". Notice that is used to advance the output to the next line, so even though this is a single string, when it is printed it will appear as five lines on the screen. For testing purposes it is essential that each row of output is exactly 2 * size() - 1 characters (not counting the ), that the returned string does not end in , and that it uses the same pattern of characters as the example board above.

Your class should have a public class constant DEFAULT_SIZE set to 3 so that code can easily know what the default size of boards are.

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

Pro Database Migration To Azure Data Modernization For The Enterprise

Authors: Kevin Kline, Denis McDowell, Dustin Dorsey, Matt Gordon

1st Edition

1484282299, 978-1484282298

More Books

Students also viewed these Databases questions