Sudoku - Loop scanner

Stylo :

I'm currently making a Sudoku using java, however 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 | . 6 3 
. . . | 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'm able to change the chars '.' whith my code, but i want to loop that properly. So if the sudoku still contains the char '.', i want it to loop the scanner to edit it once more. I'm still fairly new to scripting

And this is the code i have made to edit it 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();
    }
}

So to sum it up, how can i loop the scanner until there are no more dots/'.'?

MWB :

In your code, define an int called numDots that keeps track of the number of dots left. In your game logic, in case of a valid move, you reduce numDots by one.

For 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 do:

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 the number of dots 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;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=97473&siteId=1