Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2) An n-by-n TicTacToeBoard class uses a two-dimensional array of char called sq

ID: 3726934 • Letter: 2

Question

2) An n-by-n TicTacToeBoard class uses a two-dimensional array of char called squares to store the current board. That means squares is an array of n rows, where each row is a one-dimensional array of char having length = n. The instance variable int numMoves will store the current number of squares that contain an 'X' or an 'O'. The board starts with all elements of squares initialized to '-'. Each move changes one of those to an 'X' or an 'O', depending on who’s turn it is. It’s X’s turn if moveNumber is even and O’s turn otherwise. Your job is to complete the TicTacToeBoard methods below.

public class TicTacToeBoard {

       private int n;   // The size of the board e.g. n=3 means traditional 3 by 3 tictactoe.

       private char [][] squares; // Allocated in the constructor with size n by n.

       int numMoves;    // Initial value = zero, incremented with each move.

// Initialize this.n to n, allocate squares and set each element to '-'.

// If n < 3, throw an IllegalArgumentException with message "__ by __ board is too small"

// e.g. "2 by 2 board is too small"

public TicTacToeBoard(int n)

{

   }

   // If square[row][column] is a legal position and free (i.e. still a '-')

// then set it to 'X' or 'O' depending on whose turn it is, update numMoves and

// return true. If the position is illegal or the square is taken then return false.

       public boolean moveTo(int row, int column)

{

  

       }

Explanation / Answer

public class TicTacToeBoard { private int n; // The size of the board e.g. n=3 means traditional 3 by 3 tictactoe. private char[][] squares; // Allocated in the constructor with size n by n. int numMoves; // Initial value = zero, incremented with each move. // Initialize this.n to n, allocate squares and set each element to '-'. // If n < 3, throw an IllegalArgumentException with message "__ by __ board is too small" // e.g. "2 by 2 board is too small" public TicTacToeBoard(int n) { squares = new char[n][n]; numMoves = 0; for (int i = 0; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote