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

The file “TicTacToeBoard” randomly fills in the 0s and 1s into a TicTacToe board

ID: 3759496 • Letter: T

Question

The file “TicTacToeBoard” randomly fills in the 0s and 1s into a TicTacToe board, displays the board, and finds the rows, columns, or diagonals with all 0s, or 1s. Use a two-dimensional array to represent the tic tac toe board.

Sample Run:

001

001

111

All 1’s on row 2

All 1’s on column 2

1. Use nested for loops and Java’s Random class to load a 3 by 3 array with randomly generated     0s and 1s.

2. Display the array contents.

3. Check all 3 rows for all 0s or 1s and display a message if all values are 0 or

     all values are 1. (This has been completed for you.)

4. Check all columns for all 0s or 1s and display a messaged if all values are 0

    or all values are 1

5. Check both diagonals for all 0s or 1s and display a message if all values are 0 or

    all values are 1.

//*******************************************************

// TicTacToeBoard.java

//

// Illustrates a two dimensional array.

//*******************************************************

public class TicTacToeBoard {
    public static void main(String[] args) {
        int[][] board = new int[3][3];

//Load a 3 by 3 array with randomly generated 0s and 1s.

//Display the array contents

//Check all 3 rows for all 0s or 1s and display a message if all values are 0 or

//all values are 1.

for (int i = 0; i < 3; i++)
if (board[i][0] == board[i][1] && board[i][0] == board[i][2])
    System.out.println("All " + board[i][0] + "'s on row " + (i + 1));

//Check all columns for all 0s or 1s and display a messaged if all values are 0

//or all values are 1

//Check both diagonals for all 0s or 1s and display a message if all values are 0 or

//all values are 1.

Explanation / Answer

Hi,

Below is the solution to your problem:

//*******************************************************
// TicTacToeBoard.java
//
// Illustrates a two dimensional array.
//*******************************************************

public class TicTacToeBoard {
public static void main(String[] args) {
   // create random object
int[][] board = new int[3][3];
      
       // Initialize board
for (int i = 0; i <= 2; ++i) {
for (int j = 0; j <= 2; ++j) {
board[i][j] = (int)(2*Math.random());
               System.out.println("Random number "+board[i][j]);
}
}
       //Prints the board
       System.out.println("------------------------------");
for (int i= 0; i <= 2; ++i) {
for (int j = 0; j <= 2; ++j) {
System.out.print(" " + board[i][j]);
}
System.out.println();
}
System.out.println();
       //Check all 3 rows for all 0s or 1s and display a message if all values are 0 or
//all values are 1.
       for (int i = 0; i < 3; i++)
if (board[i][0] == board[i][1] && board[i][0] == board[i][2])
System.out.println("All " + board[i][0] + "'s on row " + (i + 1));
  
   //Check all 3 columns for all 0s or 1s and display a message if all values are 0 or
//all values are 1.
       for (int j = 0; j < 3; j++)
if (board[0][j] == board[1][j] && board[0][j] == board[2][j])
System.out.println("All " + board[0][j] + "'s on column " + (j + 1));
  
   //Check both diagonals for all 0s or 1s and display a message if all values are 0 or
//all values are 1.

   if ( (board[0][0] == board[1][1]) && (board[1][1]==board[2][2])
    ||(board[1][1]==board[0][2]) && (board[1][1]==board[2][0]))
         
System.out.println("All " + board[0][0] "has 0's or 1's");
return;  
   }
  
Board positions: 00 01 02
10 11 12
20 21 22
      
      
Hope that helps...HAPPY ANSWERING!!!!!!!

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