-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.java
More file actions
99 lines (85 loc) · 2.9 KB
/
Grid.java
File metadata and controls
99 lines (85 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
public class Grid {
public static void main(String[] args) {
// String[][] ans = randomGenerator(3);
String[][] ans = randomGenerator(Integer.parseInt(args[0]));
removeNumbers(ans);
displayGrid(ans);
}
public static void displayGrid(String [][] layout) {
int size=layout.length;
int row = 0, col = 0;
int digits = calculateDigits(layout);
for(int i = 0; i < 2*size+1; i++) {
if(i % 2 == 0) {
for(int j = 0; j < 2*size+1; j++) {
if(j % 2 == 0) {
System.out.print(" ");
} else {
String s = "-";
s = s.repeat(digits);
System.out.print("-" + s +"-");
}
}
} else {
for(int j = 0; j < 2*size+1; j++) {
if(j % 2 == 0) {
System.out.print("|");
} else {
String value = (layout[row][col++]);
String ans = String.format("%"+digits+"s",value);
System.out.print(" "+ans+" ");
if(col >= size) {
row++;
col = 0;
}
}
}
}
System.out.println();
}
}
public static String[][] randomGenerator(int size) {
String [][] arr = new String[size][size];
int [] checkRandom = new int[size];
for(int i = 0;i < size; i++) {
int randomStart = (int) (System.nanoTime() % size);
while(checkRandom[randomStart] == 1) {
randomStart = (randomStart+1)%size ;
}
checkRandom[randomStart++] = 1;
for(int j = 0; j < size; j++) {
arr[i][j] = randomStart++ +"";
if(randomStart > size) {
randomStart = 1;
}
}
}
return arr;
}
public static void removeNumbers(String[][] layout) {
int oneThird = (layout.length * layout.length)/3;
int size = layout.length;
for(int i = 0; i < oneThird; i++) {
int randoRow = (int)(System.nanoTime() % size);
int randomColumn = (int)(System.nanoTime() % size);
layout[randoRow][randomColumn] = " ";
}
}
public static int calculateDigits(String[][] layout) {
int size = layout.length;
int count = 0;
while(size>0) {
count++;
size/=10;
}
return count;
}
public static int calculateDigits(int size) {
int count = 0;
while(size > 0) {
count++;
size /= 10;
}
return count;
}
}