Java – Sudoku – Loop Scanner

Sudoku – Loop Scanner… here is a solution to the problem.

Sudoku – Loop Scanner

I’m currently making Sudoku games in Java, but I can’t seem to figure out how to loop my scanner properly. So far, it prints this:

. 1 . | 3 . . | 8 . . 
5 . 9 | 6 . . | 7 . . 
7 . 4 | . 9 5 | . 2 . 
----------------------
4 . . | . . . | 1 . . 
. 2 8 | . 7 1 | . 2 · 
. . . | 2 . 4 | 9 5 . 
----------------------
6 . 3 | . . 9 | . . 7 
. . . | 4 2 . | 5 1 6 
. 5 2 | . 8 . | . 4 . 
Next move, please (row , column , value )

And I was able to change the character “.” My code, but I want to loop properly. So if Sudoku still contains the character “.”, I want it to loop through the scanner to edit it again. I’m still pretty new to scripting

Here is the code I edited so far :

    public void moves(int row , int column , int value) {
        value += 48;
        if (list.get(row).charAt(column) == '.') {
            StringBuilder sb = new StringBuilder(list.get(row));
            sb.setCharAt(column, (char)value);
            list.set(row, sb.toString());
        }
    }

public static void main(String[] args) throws Exception  {
        Sudoku s = new Sudoku("C:\\Users\\caspe\\Downloads\\Sudoku001.sdk");
        s.printBoard();
        s.errorCheck();

System.out.println("Next move, please (row , column , value )");
        Scanner scanner = new Scanner(System.in);
        int row = scanner.nextInt();
        int column = scanner.nextInt() ;
        int value = scanner.nextInt();
        s.moves(row, column, value);
        s.errorCheck();
        s.printBoard();
    }
}

All in all, how do I loop the scan until there are no more dots/’.’?

Solution

In your code, define an int named numDots to track the number of points remaining. In your game logic, you subtract numDots by one if the move works.

To do this, you can change move to:

public boolean moves(int row , int column , int value) {
    value += 48;
    if (list.get(row).charAt(column) == '.') {
        StringBuilder sb = new StringBuilder(list.get(row));
        sb.setCharAt(column, (char)value);
        list.set(row, sb.toString());
        return true;
    }
    else {
        return false;
    }
}

Now, in your main, you can:

Sudoku s = new Sudoku("C:\\Users\\caspe\\Downloads\\Sudoku001.sdk");
s.printBoard();
s.errorCheck();
int numDots = s.getNumDots();
Scanner scanner = new Scanner(System.in);
while (numDots > 0) {
    System.out.println("Next move, please (row , column , value )");
    int row = scanner.nextInt();
    int column = scanner.nextInt() ;
    int value = scanner.nextInt();
    if (s.moves(row, column, value)) {
        numDots--;
        s.errorCheck();
        s.printBoard();
    }
}

To get points from your Sudoku, add the following method to your Sudoku class :

public int getNumDots() {
    int numDots = 0;
    for (String row: list) {
        for (int i = 0; i < row.length(); i++) {
            if (charAt(i) == '.') {
                numDots++;
            }
         }
    }
    return numDots;
}

Related Problems and Solutions