6.4k views
4 votes
Create a TicTacToe class that initializes a 3x3 board of "-" values. We will use this class in future exercises to fully build out a Tic Tac Toe game! The TicTacToe class should have a 2D array as an instance variable and a constructor that initializes the 2D array with the "-" value. Add a getter method that returns the private 2D instance variable.

2 Answers

6 votes

Answer:

public class TicTacToe{

private String [][] board;

public TicTacToe()

{

board = new String [3][3];

for (int row = 0; row<3; row++)

{

for (int play = 0; play<3; play++)

{

board[row][play] = "-";

}

}

}

public String [] [] getBoard()

{

return board;

}

}

Step-by-step explanation:

User Wagng
by
4.4k points
4 votes

Answer:

public class TicTacToe //Defining TicTacToe class

{

char board[3][3] ; //Creating a 2D array instance variable

TicTacToe() //Constructor to initialize the array with " - "

{

for( int i = 0;i<3;i++) //Loop for the row of array

{

for(int j = 0;j<3;j++) //Loop for the column of array

{

Board[i][j] = '-'; //Assigning "-" in each cell

}

}

}

public char getter() //Defining getter() method to return the array

{

return Board; //returning the array

}

}

User MushinNoShin
by
4.5k points