-4

I'm a newbie to java programming and created a console-based Sudoku Game as practice. A backtracking algorithm generates a completed Sudoku and another method 'digs holes' into random cells (regarding the difficulty). Right now, the value 0 is set for blank cells but I don't like the way the Sudoku looks..its just too many numbers in the board and I think whitespace would be a good alternative. The player should then still be able to replace (now '0', later whitespace) in the cells with his own guess. Is there a way to replace the int value when it is 0? The algorithm and digHole method both use a setValue method on a 2D array (coordinates).

Thanks for any help!

Here's my board drawing:

public static void drawBoard() {
    String[] abc = { "A", "B", "C", "D", "E", "F", "G", "H", "I" };
    // i := rows
    for (int i = 0; i <= 8; i++) {
        // first row
        if (i == 0) {
            System.out.println("");
            // coordinates 1-9
            System.out.println("       1   2   3   4   5   6   7   8   9\n" + "     _____________________________________ \n"
                            + "    |                                     |");
        }
        // j := columns in row i
        for (int j = 0; j <= 8; j++) {
            if (j == 0) {
                // coordinates A-I and first column
                System.out.print(" " + abc[i] + "  |  " + sudokuCells[i][j].getValue() + "  ");
            } else if (j == 8) {
                // last cell in current row
                System.out.print(" " + sudokuCells[i][j].getValue() + "  |");
                System.out.println("");
            } else if (j == 2 || j == 5) {
                //frame
                System.out.print(" " + sudokuCells[i][j].getValue() + " |");
            } else {
                //whitespace as seperation
                System.out.print(" " + sudokuCells[i][j].getValue() + "  ");
            }
        }
        // last row with frame
        if (i == 8) {
            System.out.println("    |_____________________________________|");
            System.out.println();
        }
Clemensius
  • 43
  • 1
  • 5

2 Answers2

-1

Why not just set it as:

" "

That should do the trick, maybe post an example of your code as well?

Joseph
  • 253
  • 1
  • 8
-1

I suggest to keep the logic representation as it is now (with 0) and just print a space when you encounter a 0 so you have to modify only the function that print the board!

Like this for example

for(int i = 0; i < board.length; i++)
{
   for(int j = 0 j < board.length; j++)
   {
      Cells curr = board[i][j];
      if(curr.getValue() == 0)
         System.out.print(" ");
      else
         System.out.print(curr.getValue())
   }
   System.out.println()
}
Tommaso Pasini
  • 1,521
  • 2
  • 12
  • 16