In Java Write a program which asks the users for the dimensi

In Java:

Write a program which asks the users for the dimensions of an n x n matrix. The matrix should be randomly populated with the characters x and o. Display the matrix then report all rows, columns, and both diagonals that have all x\'s or all o\'s.

Sample Output:

Solution

import java.util.Random;
import java.util.Scanner;


public class Tictactoe {
  
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Random rand = new Random();
       int n,rd;
       System.out.print(\"Enter matrix size: \");
       Scanner scan = new Scanner(System.in);
       n=scan.nextInt();
       int tt[][] = new int[n][n];
       for(int i=0;i<n;i++)
       {
           for(int j=0;j<n;j++)
           {
               tt[i][j] = rand.nextInt(2);
               if(tt[i][j]==1)
               {
                   System.out.print(\"o\");
               }
               if(tt[i][j]==0)
               {
                   System.out.print(\"x\");
               }
           }  
           System.out.println(\"\");
       }
       int hat1=0,hat2=0,hat3=0,hat4=0;
       for(int i=0;i<n;i++)
       {
           hat1=0;hat2=0;
           hat3=0;hat4=0;
           for(int j=0;j<n;j++)
           {
               if(tt[i][j]==1)
               {
                   hat1=1;
               }
               if(tt[i][j]==0)
               {
                   hat2=1;
               }
               if(tt[j][i]==1)
               {
                   hat3=1;
               }
               if(tt[j][i]==0)
               {
                   hat4=1;
               }
           }
           if(hat1==0)
           {
               System.out.print(\"All x\'s in row \");
               System.out.println(i+1);
           }
           if(hat2==0)
           {
               System.out.print(\"All o\'s in row \");
               System.out.println(i+1);
           }
           if(hat3==0)
           {
               System.out.print(\"All x\'s in column \");
               System.out.println(i+1);
           }
           if(hat4==0)
           {
               System.out.print(\"All o\'s in column \");
               System.out.println(i+1);
           }
       }
       hat1=0;hat2=0;
       hat3=0;hat4=0;
       for(int i=0,j=n-1;i<n;i++,j--)
       {
           if(tt[i][i]==1)
           {
               hat1=1;
           }
           if(tt[i][i]==0)
           {
               hat2=1;
           }
           if(tt[i][j]==1)
           {
               hat3=1;
           }
           if(tt[i][j]==0)
           {
               hat4=1;
           }
       }
       if(hat1==0)
       {
           System.out.println(\"All x\'s in main diagonal\");
       }
       if(hat2==0)
       {
           System.out.println(\"All o\'s in main diagonal\");
           //System.out.println(i+1);
       }
       if(hat3==0)
       {
           System.out.println(\"All x\'s in second diagonal\");
           //System.out.println(i+1);
       }
       if(hat4==0)
       {
           System.out.println(\"All o\'s in second diagonal\");
             
       }
       // System.out.println(randomNum);
   }
}

In Java: Write a program which asks the users for the dimensions of an n x n matrix. The matrix should be randomly populated with the characters x and o. Displa
In Java: Write a program which asks the users for the dimensions of an n x n matrix. The matrix should be randomly populated with the characters x and o. Displa
In Java: Write a program which asks the users for the dimensions of an n x n matrix. The matrix should be randomly populated with the characters x and o. Displa

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site