LordPankake

Square matrix guide

Apr 11th, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class Snippet {
  5.     private final static Scanner s = new Scanner(System.in);
  6.     private final static Random r = new Random();
  7.  
  8.     public static void main(String[] args) throws Exception {
  9.         print("Enter grid size: ");
  10.         // read grid size
  11.         int n = s.nextInt();
  12.  
  13.         // spacing
  14.         println();
  15.  
  16.         // make square grid
  17.         int[][] g = new int[n][n];
  18.         // iterate over indicies in grid, set the value.
  19.         for (int y = 0; y < n; y++) {
  20.             for (int x = 0; x < n; x++) {
  21.                 // set value at y:x, then print it
  22.                 print(g[y][x] = r.nextInt(2));
  23.             }
  24.             println();
  25.         }
  26.  
  27.         // spacing
  28.         println();
  29.  
  30.         // find rows with same values
  31.         for (int y = 0; y < n; y++) {
  32.             // initial value in row-y.
  33.             int row = g[y][0];
  34.             String rowStr = String.valueOf(row);
  35.             String line = rowStr;
  36.             // check and see if value of 'row' changes.
  37.             for (int x = 1; x < n; x++) {
  38.                 // if same, add to 'rowStr'
  39.                 // if different make 'rowStr' null so we will not add to it
  40.                 if (row == g[y][x] && line != null) {
  41.                     // same content
  42.                     line += rowStr;
  43.                 } else {
  44.                     // failed
  45.                     line = null;
  46.                 }
  47.             }
  48.             // print row string if not null
  49.             if (line != null) {
  50.                 println("Row[" + (y + 1) + "]: " + line);
  51.             }
  52.         }
  53.  
  54.  
  55.         // spacing
  56.         println();
  57.  
  58.         // find columns with same values
  59.         // this is mostly the same as the last block of code, but with x/y
  60.         // switched.
  61.         for (int x = 0; x < n; x++) {
  62.             // initial value in col-y.
  63.             int col = g[0][x];
  64.             String colStr = String.valueOf(col);
  65.             String line = colStr;
  66.             // check and see if value of 'col' changes.
  67.             for (int y = 1; y < n; y++) {
  68.                 // if same, add to 'colStr'
  69.                 // if different make 'colStr' null so we will not add to it
  70.                 if (col == g[y][x] && line != null) {
  71.                     // same content
  72.                     line += colStr;
  73.                 } else {
  74.                     // failed
  75.                     line = null;
  76.                 }
  77.             }
  78.             // print column string if not null
  79.             if (line != null) {
  80.                 println("col[" + (x + 1) + "]: " + line);
  81.             }
  82.         }
  83.        
  84.         // spacing
  85.         println();
  86.  
  87.         // diagonal-1: top-left --> bottom-right
  88.         int d1 = g[0][0];
  89.         String d1Str = String.valueOf(d1);
  90.         String d1Line = d1Str;
  91.         // diagonal-2: top-right --> bottom-left
  92.         int d2 = g[0][n - 1];
  93.         String d2Str = String.valueOf(d2);
  94.         String d2Line = d2Str;     
  95.         for (int d = 1; d < n; d++) {          
  96.             // check diagonal 1,
  97.             if (g[d][d] == d1 && d1Line != null) {
  98.                 d1Line += d1Str;
  99.             } else {
  100.                 d1Line = null;
  101.             }
  102.  
  103.             // check diagonal 2,
  104.             if (g[d][(n-1)-d] == d2 && d2Line != null) {
  105.                 d2Line += d2Str;
  106.             } else {
  107.                 d2Line = null;
  108.             }
  109.         }
  110.         // print diagonals
  111.         if (d1Line != null) println("Diagonal-Forwards: " + d1Line);
  112.         if (d2Line != null) println("Diagonal-Backward: " + d2Line);
  113.     }
  114.  
  115.     private static void println() { System.out.println(); }
  116.     private static void println(Object x) { System.out.println(x); }
  117.     private static void print(Object x) { System.out.print(x); }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment