-
Notifications
You must be signed in to change notification settings - Fork 1
/
GridToText.java
67 lines (59 loc) · 2.66 KB
/
GridToText.java
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
package com.stackexchange.codereview.rolfl.sudoku_wec3;
public final class GridToText {
private static final char[][] symbols = {
"╔═╤╦╗".toCharArray(),
"║ │║║".toCharArray(),
"╟─┼╫╢".toCharArray(),
"╠═╪╬╣".toCharArray(),
"╚═╧╩╝".toCharArray(),
};
public static final String displayAsString(Grid grid) {
final int dimension = grid.getDimension();
final int blocksize = grid.getBlocksize();
final int[][] rows = new int[dimension][dimension];
for (int r = 0; r < dimension; r++) {
for (int c = 0; c < dimension; c++) {
rows[r][c] = grid.get(r, c).getValue();
}
}
return buildStringForGrid(dimension, blocksize, rows);
}
public static final String displayAsString(int[][]data) {
return buildStringForGrid(data.length, SudokuRules.getBlockSize(data.length), data);
}
private static final String buildStringForGrid(final int dimension, final int blocksize, final int[][]rows) {
final StringBuilder sb = new StringBuilder();
for (int r = 0; r < dimension; r++) {
if (r == 0) {
sb.append(printSymbolLine(dimension, blocksize, null, symbols[0]));
} else if (r % blocksize == 0) {
sb.append(printSymbolLine(dimension, blocksize, null, symbols[3]));
} else {
sb.append(printSymbolLine(dimension, blocksize, null, symbols[2]));
}
sb.append(printSymbolLine(dimension, blocksize, rows[r], symbols[1]));
}
sb.append(printSymbolLine(dimension, blocksize, null, symbols[4]));
return sb.toString();
}
private static String printSymbolLine(int dimension, int blocksize, int[] values, char[] symbols) {
StringBuilder sb = new StringBuilder();
sb.append(symbols[0]);
int vc = 0;
for (int b = 0; b < blocksize; b++) {
for (int c = 0; c < blocksize; c++) {
if (values == null) {
sb.append(symbols[1]).append(symbols[1]).append(symbols[1]).append(symbols[2]);
} else {
final int val = values[vc++];
char ch = SudokuRules.getSudokuDigit(val);
sb.append(symbols[1]).append(ch).append(symbols[1]).append(symbols[2]);
}
}
sb.setCharAt(sb.length() - 1, symbols[3]);
}
sb.setCharAt(sb.length() - 1, symbols[4]);
sb.append("\n");
return sb.toString();
}
}