I am creating a Sudoku helper program, where I help the user solve the puzzle.
How can I create horizontal " = " and vertical "| " dividers in order to break up the grid into subgrids? For the horizontal dividers, for the outer (row) loop, the dividing line is added after rows 2 and 5. For vertical dividers, inner (cols) loop, add vertical bar " | " after cols 2 and 5.
Lastly, how can I create headers to each row and column in order for the user to identify each row and cols by its id?
public class Sudoku {
public static int rows = 9;
public static int cols = 9;
public static int[][] board = new int[rows][cols];
public static void show() {
for (int rows=1; rows<board.length; rows++) {
for (int cols=1; cols<board.length; cols++) {
board[rows][cols] = '0';
}
}
}
public static void main(String[] args) {
//print out the contents of board array
int board[][] = new int[9][9];
for (int rows = 0; rows < 9; rows++) {
for (int cols = 0; cols < 9; cols++) {
System.out.print(board [rows][cols] + " ");
}
System.out.println("");
}
show();
}
}
The output of my program:
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
This is something I wrote some time ago, just change the sudoku
array to your arrayname ( board
).
for(int x = 0; x < 9; x++){
System.out.print("| | | || | | || | | |\n");
for(int y = 0; y < 9; y++){
if(y == 0 || y == 3 || y == 6) System.out.print("|");
System.out.print(" " + sudoku[x][y] + " |");
}
System.out.print("\n");
System.out.print("| | | || | | || | | |\n");
if(x == 2 || x == 5)
System.out.print("=========================================================");
else
System.out.print("---------------------------------------------------------");
System.out.print("\n");
}
This will create something like this:
---------------------------------------------------------
| | | || | | || | | |
| 0 | 0 | 0 || 0 | 0 | 0 || 0 | 0 | 0 |
| | | || | | || | | |
---------------------------------------------------------
| | | || | | || | | |
| 0 | 0 | 8 || 0 | 6 | 5 || 0 | 0 | 0 |
| | | || | | || | | |
---------------------------------------------------------
| | | || | | || | | |
| 0 | 3 | 0 || 9 | 0 | 4 || 5 | 0 | 8 |
| | | || | | || | | |
=========================================================
| | | || | | || | | |
| 0 | 0 | 0 || 0 | 2 | 0 || 4 | 5 | 0 |
| | | || | | || | | |
---------------------------------------------------------
| | | || | | || | | |
| 0 | 0 | 0 || 0 | 0 | 0 || 0 | 0 | 6 |
| | | || | | || | | |
---------------------------------------------------------
| | | || | | || | | |
| 0 | 0 | 7 || 0 | 0 | 0 || 3 | 0 | 0 |
| | | || | | || | | |
=========================================================
| | | || | | || | | |
| 1 | 2 | 0 || 0 | 4 | 6 || 0 | 7 | 0 |
| | | || | | || | | |
---------------------------------------------------------
| | | || | | || | | |
| 0 | 0 | 3 || 0 | 8 | 0 || 1 | 0 | 0 |
| | | || | | || | | |
---------------------------------------------------------
| | | || | | || | | |
| 6 | 0 | 0 || 7 | 5 | 0 || 0 | 0 | 0 |
| | | || | | || | | |
---------------------------------------------------------
A bit ugly, but you can definitely improve it:
public static void main(String[] args) {
//print out the contents of board array
int board[][] = new int[9][9];
System.out.println(" 0 1 2 | 3 4 5 | 6 7 8 <-- column id");
System.out.println("======================== v- row id");
for (int rows = 0; rows < 9; rows++) {
for (int cols = 0; cols < 9; cols++) {
if (cols % 3 == 0) {
System.out.print("| ");
}
System.out.print(board [rows][cols] + " ");
}
System.out.println("| "+ rows);
if ((rows+1) %3 == 0) {
System.out.println("========================");
}
}
show();
}
It shows something like that:
0 1 2 | 3 4 5 | 6 7 8 <-- column id
======================== v- row id
| 0 0 0 | 0 0 0 | 0 0 0 | 0
| 0 0 0 | 0 0 0 | 0 0 0 | 1
| 0 0 0 | 0 0 0 | 0 0 0 | 2
========================
| 0 0 0 | 0 0 0 | 0 0 0 | 3
| 0 0 0 | 0 0 0 | 0 0 0 | 4
| 0 0 0 | 0 0 0 | 0 0 0 | 5
========================
| 0 0 0 | 0 0 0 | 0 0 0 | 6
| 0 0 0 | 0 0 0 | 0 0 0 | 7
| 0 0 0 | 0 0 0 | 0 0 0 | 8
========================
UPDATE : If you want the row id at the beginning of each line, then do the following:
public static void main(String[] args) {
//print out the contents of board array
int board[][] = new int[9][9];
System.out.println(" 0 1 2 3 4 5 6 7 8");
System.out.println(" ========================");
for (int rows = 0; rows < 9; rows++) {
System.out.print(rows+" |");
for (int cols = 0; cols < 9; cols++) {
System.out.print(board [rows][cols] + " ");
if ((cols+1) % 3 == 0) {
System.out.print("| ");
}
}
System.out.println();
if ((rows+1) %3 == 0) {
System.out.println(" ========================");
}
}
show();
}
It will show:
0 1 2 3 4 5 6 7 8
========================
0 |0 0 0 | 0 0 0 | 0 0 0 |
1 |0 0 0 | 0 0 0 | 0 0 0 |
2 |0 0 0 | 0 0 0 | 0 0 0 |
========================
3 |0 0 0 | 0 0 0 | 0 0 0 |
4 |0 0 0 | 0 0 0 | 0 0 0 |
5 |0 0 0 | 0 0 0 | 0 0 0 |
========================
6 |0 0 0 | 0 0 0 | 0 0 0 |
7 |0 0 0 | 0 0 0 | 0 0 0 |
8 |0 0 0 | 0 0 0 | 0 0 0 |
========================
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.