3

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/'.'?

Stylo
  • 67
  • 5

1 Answers1

3

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;
}
MWB
  • 1,830
  • 1
  • 17
  • 38
  • Aha, i see, but then i should be able to track the amount, but that is one of the issues aswell. Can't seem to crack it or find any information on how to count the number of a char in an ArrayList? – Stylo Nov 04 '18 at 16:13
  • 1
    In your `moves`, you check if the value is a dot. If so, you replace it and there you could also reduce `numDots`. In that case, you only need to check the number of dots ones, during initialization. Also (an even better solution, I think), you can have `moves` return a boolean: `true` if the move is legal or `false` if not. In that case you can do `boolean validMove = s.moves(row, column, value);` followed by an `if` statement that if the move is indeed valid, you reduce `numDots`. – MWB Nov 04 '18 at 16:16
  • I updated my answer to include the logic to keep track of `numDots`. Hope you accept this solution. – MWB Nov 04 '18 at 16:22
  • Please share the data structure of your Sudoko class. Is it an ArrayList with Strings with 9 characters for each row? – MWB Nov 04 '18 at 16:26
  • But how would i make the int numDots check the amount dots in the ArrayList? It's not necessarily the same sudoku plate every time, so i can not put a specific amount of dots in the int myself. The code has to check it by itself - EDIT, one moment – Stylo Nov 04 '18 at 16:29
  • To begin with, it contains 9 characters for each row, which it gets from reading a file, afterwards the code rearranges it into the output which i posted at the top of the question. However it looks like this: `public class Sudoku { ArrayList list; public Sudoku (String filnavn ) throws Exception { readFile(filnavn); } public void readFile(String fileName)throws Exception { Scanner s = new Scanner(new File(fileName)); list = new ArrayList(); while (s.hasNext()){ list.add(s.next()); } s.close(); }` – Stylo Nov 04 '18 at 16:38
  • I adapted my answer with a method for your `Sudoku` class to get the number of dots. Hope this helps! – MWB Nov 04 '18 at 16:39
  • By the way, welcome to StackOverflow. Don't forget to upvote and accept answer you receive! – MWB Nov 04 '18 at 17:01
  • Thank you, and even more thanks for the help MWB, it worked with minor tweaks! - I can't upvote yet (too lew rep) but i'll be sure to do so when i'm able! – Stylo Nov 04 '18 at 17:12