Question

I have to create a tic tac toe board using the following classes. The main function...

I have to create a tic tac toe board using the following classes. The main function is already been made as another java file and presented below to test if it works or not. 

Using these methods as the public class:
public TTTBoard(int size) //Present DEFAULT_SIZE = 3. Throw IllegalArgumentException if the size is less than 1.
public TTTBoard() 
public char get(int r, int c) //Throw IndexOutOfBoundsException if out of bounds
public void set(int r, int c, char ch) //Char can be anything besides "O" and "X" 
public int size() //Flexible in size
public char winner() //Decide winner from Horizontal, Vertical, both Front Diagonal and Back Diagonal
public String toString() //Draw the board

Using this as the main testing function. Final result will present "pass" if the board works:
 
public class TTTBoardTester {

    public static void main(String[] args) {
        // Code passes unless we find a problem
        boolean pass = true;
        
        // Test existence of class constant
        pass = pass && (TTTBoard.DEFAULT_SIZE == 3);
        
        // Test default board creation, legal set/get, toString, winners
        TTTBoard b = new TTTBoard();
        pass = pass && (b.winner()==' ');
        b.set(0,2,'X');
        b.set(1,1,'X');
        b.set(2,0,'X');
        b.set(2,2,'X');
        pass = pass && (b.get(2,2)=='X');
        pass = pass && (b.get(2,1)==' ');
        String expected = " | |X\n-+-+-\n |X| \n-+-+-\nX| |X";
        pass = pass && (b.toString().indexOf(expected)==0);
        pass = pass && (b.winner()=='X');
        b.set(1,1,' ');
        b.set(1,2,'X');
        expected = " | |X\n-+-+-\n | |X\n-+-+-\nX| |X";
        pass = pass && (b.toString().indexOf(expected)==0);
        pass = pass && (b.winner()=='X');
        b.set(2,1,'X');
        b.set(1,2,' ');
        expected = " | |X\n-+-+-\n | | \n-+-+-\nX|X|X";
        pass = pass && (b.toString().indexOf(expected)==0);
        pass = pass && (b.winner()=='X');

        // Test bigger board creation, toString, size, not winner
        b = new TTTBoard(4);
        b.set(0,0,'X');
        b.set(1,1,'X');
        b.set(2,2,'X');
        b.set(0,2,'X');
        b.set(2,0,'X');
        b.set(1,2,'X');
        b.set(2,1,'X');
        b.set(1,3,'X');
        b.set(3,0,'X');
        b.set(0,1,'X');
        expected = "X|X|X| \n-+-+-+-\n |X|X|X\n-+-+-+-\nX|X|X| \n-+-+-+-\nX| | | ";
        pass = pass && (b.toString().indexOf(expected)==0);
        pass = pass && (b.winner()==' ');
        pass = pass && (b.size()==4);
        // Make it a winner
        b.set(3,3,'X');
         pass = pass && (b.winner()=='X');
       
        // Test smaller board creation, toString, not winner
        b = new TTTBoard(2);
        expected = " | \n-+-\n | ";
        pass = pass && (b.toString().indexOf(expected)==0);
        pass = pass && (b.winner()==' ');
        
        // Test exceptions
        try {
            b = new TTTBoard(0);
            pass = false;        // Exception in constructor will bypass this
        }
        catch(IllegalArgumentException e) {
            // Geting here won't change 'pass'
        }
        catch(Exception e) {
            pass = false;
        }
        
        // Print final result
        System.out.println(pass);
    }

}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is the code for the question. Run the TTTBoardTester.java provided by instructor and the output is true... that is all test pass
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you

TTTBoard.java
----

public class TTTBoard {
private char[][] board;
public static final int DEFAULT_SIZE = 3;
  
public TTTBoard(int size) //Present DEFAULT_SIZE = 3. Throw IllegalArgumentException if the size is less than 1.
{
if(size < 1)
throw new IllegalArgumentException();
board = new char[size][size];
for(int i = 0; i < size; i++)
for(int j = 0; j < size; j++ )
board[i][j] = ' ';
}

public TTTBoard(){
int size = 3;
board = new char[size][size];
for(int i = 0; i < size; i++)
for(int j = 0; j < size; j++ )
board[i][j] = ' ';

}

public char get(int r, int c) //Throw IndexOutOfBoundsException if out of bounds
{
int s = board.length;
if(!(r >= 0 && c >= 0 && r < s && c < s))
throw new IndexOutOfBoundsException();

return board[r][c];
}

public void set(int r, int c, char ch) //Char can be anything besides "O" and "X"
{
int s = board.length;

if(!(r >= 0 && c >= 0 && r < s && c < s))
throw new IndexOutOfBoundsException();

board[r][c] = ch;
}

public int size() //Flexible in size
{
return board.length;
}

public char winner() //Decide winner from Horizontal, Vertical, both Front Diagonal and Back Diagonal
{
int s = size();
//check Horizontal
for(int r = 0; r < s; r++){
if(board[r][0] != ' '){
boolean won = true;
for(int c = 1; c < s; c++){
if(board[r][c] != board[r][c-1]){
won = false;
break;
}
}

if(won)
return board[r][0];
}
}
//check vertical
for(int c = 1; c < s; c++){
if(board[0][c] != ' '){
boolean won = true;
for(int r = 1; r < s; r++){

if(board[r-1][c] != board[r][c]){
won = false;
break;
}
}

if(won)
return board[0][c];
}
}
  
//check Front diagonal
if(board[0][0] != ' '){
boolean won = true;
for(int i = 1; i < s; i++){
if(board[i-1][i-1] != board[i][i]){
won = false;
break;
}
}
  
if(won)
return board[0][0];
}

//check Back diagonal

if(board[s-1][0] != ' '){
boolean won = true;
for(int r = 1, c = s-2; r < s; r++, c--){
if(board[r-1][c+1] != board[r][c]){
won = false;
break;
}
}
  
if(won)
return board[s-1][0];
}
  
return ' ';
  

}
public String toString() //Draw the board
{
String str = "";
int s = size();
for(int i = 0; i < s; i++){
for(int j = 0; j < s; j++){
if(j != 0)
str += "|";
str += board[i][j];
  
}
str += "\n";
for(int j = 0; j < s-1; j++)
str += "-+";
str += "-\n";
}
return str;
}

}

output
----
true

Add a comment
Know the answer?
Add Answer to:
I have to create a tic tac toe board using the following classes. The main function...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Q1) Write a C function that allows the user to initialize a Tic-Tac-Toe board. The board...

    Q1) Write a C function that allows the user to initialize a Tic-Tac-Toe board. The board is a 2D array of size 3x3. The function will set an id for each cell of the board starting from 1 and stop at 9. The function prototype is ​void​​ ​​InitializeBoard​​(​​int​​ m, ​​int​​ n , ​​char​​ board[][n]) void​​ ​​InitializeBoard​​(​​int​​ m, ​​int​​ n , ​​char​​ board[][n])​​{ ​​int​​ c =​​1​​; ​ for​​(​​int​​ i =​​0​​; i<m; i++){ ​​ for​​(​​int​​ j=​​0​​; j< n; j++){ board[i][j] = c+​​'0'​​;...

  • The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[...

    The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[SIZE];    int player, choice, win, count;    char mark;    count = 0; // number of boxes marked till now    initBoard(board);       // start the game    player = 1; // default player    mark = 'X'; // default mark    do {        displayBoard(board);        cout << "Player " << player << "(" << mark...

  • JAVA TIC TAC TOE - please help me correct my code Write a program that will...

    JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below |    1       |   2        |   3 |    4       |   5        |   6                 |    7      |   8        |   9 The program will assign X to Player 1, and O to Player    The program will ask Player 1, to...

  • My homework is to write a tic tac toe function, this code isn't working and I...

    My homework is to write a tic tac toe function, this code isn't working and I can not find the error. It prints the board and accepts user input just fine, the only problem is the checkBoard function. I can not get it to return a value(player win) to end the while loop. #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int checkBoard(char board[3][3]) {    int i, j;    for (i = 0; i < 3; i += 1) {        if...

  • Implement a tic-tac-toe game. Currently, if the variables provided in main are commented out, the program...

    Implement a tic-tac-toe game. Currently, if the variables provided in main are commented out, the program will compile. Complete the specifications for each function. As you develop the program, implement one function at a time, and test that function. The provided comments provide hints as to what each function should do and when each should be called. Add variables where you see fit. Implementation Strategies: The template has variables and two global constants for you to utilize. The template has...

  • This is my code for a TicTacToe game. However, I don't know how to write JUnit...

    This is my code for a TicTacToe game. However, I don't know how to write JUnit tests. Please help me with my JUnit Tests. I will post below what I have so far. package cs145TicTacToe; import java.util.Scanner; /** * A multiplayer Tic Tac Toe game */ public class TicTacToe {    private char currentPlayer;    private char[][] board;    private char winner;       /**    * Default constructor    * Initializes the board to be 3 by 3 and...

  • In the C++ programming language write a program capable of playing 3D Tic-Tac-Toe against the user....

    In the C++ programming language write a program capable of playing 3D Tic-Tac-Toe against the user. Your program should use OOP concepts in its design. Use Inheritance to create a derived class from your Lab #9 Tic-Tac-Toe class. You can use ASCII art to generate and display the 3x3x3 playing board. The program should randomly decide who goes first computer or user. Your program should know and inform the user if an illegal move was made (cell already occupied). The...

  • Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you...

    Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you must copy and paste to cloud9. Do not change the provided complete functions, or the function stub headers / return values. Currently, if the variables provided in main are commented out, the program will compile. Complete the specifications for each function. As you develop the program, implement one function at a time, and test that function. The provided comments provide hints as to what...

  • This is an advanced version of the game tic tac toe, where the player has to...

    This is an advanced version of the game tic tac toe, where the player has to get five in a row to win. The board is a 30 x 20. Add three more functions that will check for a win vertically, diagonally, and backwards diagonally. Add an AI that will play against the player, with random moves. Bound check each move to make sure it is in the array. Use this starter code to complete the assignment in C++. Write...

  • java ****Please fix this tic tac toe coding error *****following is the error I've found difficult...

    java ****Please fix this tic tac toe coding error *****following is the error I've found difficult for me to fix. 1. Before I click the board, the grid doesn’t show up at all 2. It is not an automatic system.(if I click, it doesn’t move to next move) 3. if you click a button twice it shows o, x both 4. it is hard to close (you have to close the instruction) 5. instruction keep pops up (start->if you lose...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT