#Sudoku help

137 messages · Page 1 of 1 (latest)

versed lark
#
public class Sudoku {
    
    public static void play(Board board) {
        
        boolean solved = false;
        int boxSize = (int) Math.sqrt(board.getN());
        
        while(solved == false) {
            
            board.displayBoard(boxSize);
            
            System.out.println("Enter the command in the following format:\n" +
                    "+ i,j=val: for entering val at position (i,j)\n" +
                    "+ i,j=0  : for clearing a cell (i,j)\n" +
                    "+ 0,0=0  : for saving and ending the game\n" +
                    "Notice: i,j, val numbering is from [1.." + board.getN() + "]");
            
            UserChoice choice = new UserChoice();
            getUserInput(choice);
            
            if(isSolved(board) == true) {
                
                
                
            }
            
        }
        
    }
    
    public static void getUserInput(UserChoice choice) {
            
        System.out.print(">");
        
        
    }
    
    public static boolean isSolved(Board board) {
        
        for(int i=0; i<board.getN(); i++) {
            
            for(int j=0; j<board.getN(); j++) {
                
                if(board.getTableauValue(i,j) == 0) {
                    
                    return false;
                    
                }
                
            }
            
        }
        
        if(board.checkValidity() == false) {
            
            return false;
            
        }
        
        return true;
        
    }
gleaming inletBOT
#

This post has been reserved for your question.

Hey @versed lark! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

versed lark
#
public static void insertMove(UserChoice choice, Board board) {
        
        if(choice.getRow() != 0 && choice.getColumn() != 0 && choice.getValue() == 0) {
            
            board.setTableValue(choice.getRow(),choice.getColumn(),0);
            
        }
        else if(choice.getRow() !=0 && choice.getColumn() != 0 && choice.getValue() != 0) {
            
            board.setTableValue(choice.getRow(),choice.getColumn(),choice.getValue());
            
        }
        
    }
#
public static void main(String[] args) {
        
        if (args.length != 2) {
            
            System.out.println("Please give the dimension N followed by a <game-file> as the only 2 arguments");
            return;
            
        }

        int N = Integer.parseInt(args[0]);

        if (N == 4 || N == 9) {
            
            Board board = new Board(N);
            In ifile = new In(args[1]);
            Out ofile = new Out("out-" + args[1]);

            boolean boardIsMade = board.readBoard(ifile);

            if (boardIsMade ==  false) {
                
                return;
                    
            }

            boolean valid = board.checkValidity();

            if (valid == false) {
                
                return;
                
            }
            
            play(board);
            
            
            
            for(int i=0; i<N; i++) {
                
                for(int j=0; j<N; j++) {
                    
                    ofile.print(board.getTableauValue(i,j) + " ");
                    
                }
                
                ofile.println();
                
            }
            
            if(isSolved(board) == true) {
                
                System.out.println("Game complete!!!");
                return;
                
            }    
            
            System.out.println("Saving game to out-" + args[1]);
            System.out.println("bye!");
            
            ifile.close();
            ofile.close();
            
        } 
        else {
            
            System.out.println("The allowed value for N is either 4 or 9!");
            System.out.println("Please rerun the program and give a valid value for N");
            return;
            
        }
        
    }
    
}
#
public class UserChoice {
    
    private int row;
    private int col;
    private int value;

    public UserChoice() {
        
        this.row = 0;
        this.col = 0;
        this.value = 0;
        
    }

    public void setChoice(int row, int col, int value) {
        
        this.row = row;
        this.col = col;
        this.value = value;
        
    }

    public int getRow() {
        
        return this.row;
        
    }

    public int getColumn() {
        
        return this.col;
        
    }

    public int getValue() {
        
        return this.value;
        
    }
    
}
#

My problem is that the StdIn inside the getUserInput is being skiped idk why

dusky jewel
#

that method is empty except a single print statement

zinc saffron
#

Can you show your getUserInput?

gleaming inletBOT
#

If Scanner#nextLine reads an empty line after calling a different Scanner#next method, take a look at this StackOverflow post.

zinc saffron
versed lark
versed lark
dusky jewel
#

oh wait what is StdIn

versed lark
#

its a library

#

i can do it with scanner also

#

wait let me post another version with scanner

#
public static void play(Board board) {
        
        UserChoice choice = new UserChoice();
        boolean solved = false;
        int boxSize = (int) Math.sqrt(board.getN());
        Scanner scanner = new Scanner(System.in);

        
        while(solved == false) {
            
            boolean validMove = false;
            
            board.displayBoard(boxSize);
            
            System.out.println("Enter the command in the following format:\n" +
            "+ i,j=val: for entering val at position (i,j)\n" +
            "+ i,j=0  : for clearing a cell (i,j)\n" +
            "+ 0,0=0  : for saving and ending the game\n" +
            "Notice: i,j, val numbering is from [1.." + board.getN() + "]");
            System.out.print(">");
            
            getUserInput(choice,board);
            
            if(choice.getRow() != -1) {
                
                if(choice.getRow() == 0 && choice.getColumn() == 0 && choice.getValue() == 0) {

                    return;
                    
                }
            
                validMove = isValidMove(choice,board);
            
                if(validMove == true) {
                
                    insertMove(choice,board);
                    solved = isSolved(board);
                    
                }
                
            }
            
        }
        ```
zinc saffron
#

as well as getUserInput

versed lark
#
public static void getUserInput(UserChoice choice, Board board) {
            
        System.out.print(">");
        
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine().trim();
        
        System.out.println(input);

        if (input.length() != 5 || input.charAt(1) != ',' || input.charAt(3) != '=') {
            
            System.out.println("Error: wrong format of command!");
            choice.setChoice(-1, -1, -1);
            return;
            
        }

        int row = Character.getNumericValue(input.charAt(0));
        int col = Character.getNumericValue(input.charAt(2));
        int value = Character.getNumericValue(input.charAt(4));

        if (row < 0 || row > board.getN() || col < 0 || col > board.getN() || value < 0 || value > board.getN()) {
            
            System.out.println("Error: i,j,val numbering outside the allowed range [1.." + board.getN() + "]");
            choice.setChoice(-1, -1, -1);
            return;
            
        }
        
        choice.setChoice(row,col,value);
        
        System.out.println(row + " " + col + " " + value);

     }
    ```
#

This is another try that i did with scanner but it just still skips it

zinc saffron
#

Are you using Scanner/System.in/In anywhere before that?

#

because if so, it's probably that issue

gleaming inletBOT
#

If Scanner#nextLine reads an empty line after calling a different Scanner#next method, take a look at this StackOverflow post.

versed lark
#

how can i fix it could you tell me

zinc saffron
#

where you do String input = scanner.nextLine().trim();

#

try adding another scanner.nextLine() before

versed lark
#

1 sec let me try that

#

nop

#

still skips it

zinc saffron
#

what do you mean with it being skipped?

versed lark
#

well normally when it finds and StdIn or a scanne the program stops and waits for the user to input something

#

but it doesnt do that

zinc saffron
#

How are you running the program?

versed lark
#

eclipse

dusky jewel
#

are you launching it with a debugger or normal ?

versed lark
#

noraml

#

normal

zinc saffron
#

YOu said you are using libraries

#

Can you show them?

versed lark
#

i only use StdLib

zinc saffron
#

Yeah can you show that?

#

I feel like it might be redirecting studin to use a file

versed lark
#

i dont think its taht

zinc saffron
#

How are you using it?

versed lark
#

wdym

zinc saffron
#

Are you using that library anywhere?

#

what is the value of input when it is skipped?

versed lark
#

-5 0 0 0 0 0 -4 0 0
i think this

#

thats what it says

zinc saffron
#

I think it might be reading stuff from a file instead of your console?

versed lark
#

wait

#

you are so smart

#

i does

#

-5 0 0 0 0 0 -4 0 0
0 -1 0 0 -5 -7 0 0 -3
0 -2 0 -3 0 -6 0 -9 0
-2 -7 0 -9 0 0 0 -4 0
0 -9 0 0 0 0 0 -3 0
0 -4 0 0 0 -5 0 -8 -7
0 -8 0 -5 0 -1 0 -2 0
-1 0 0 -8 -4 0 0 -5 0
0 0 -3 0 0 0 0 0 -8

#

here is the file i am reading from to fill the board of the sudoku

#

and the first line is the same as the value of input when it is first being skipped

#

so how can i fix that

zinc saffron
#

Can you show the run configuration you are using?

#

especially this part

versed lark
zinc saffron
#

yeah so Input File is set here

versed lark
#

yeah

zinc saffron
#

this means that you cannot enter anything in the console but it will use the file instead

#

if you don't want that, you can disable it

versed lark
#

i must read from file

zinc saffron
#

but don't forget to re-enable it if necessary

versed lark
#

and also i must read from the console

zinc saffron
#

well that option configures it to replace the console by a file

versed lark
#

cause the input to fill the sudoku must be read from file

zinc saffron
#

but if the application should read from a file, you can do that without the option as well

versed lark
#

how?

zinc saffron
#

e.g. you can define a Scanner that just reads from a file

versed lark
#

never done that before

#

can the scanner also output to a new file?

#

that it creates?

zinc saffron
#

Scanner only does input

versed lark
#

damn

#

cant use that then

zinc saffron
#

you can create a Scanner that reads from a file using new Scanner(Path.of("someFile.txt"))

#

but Java alos allows to write to files

versed lark
#

well the problem is

#

i have to read the input from an In file then i have to create an Out file and also i have to read input given by the user from console

zinc saffron
#

you can write to a file like this

PrintStream fileOut = new PrintStream(new FileOutputSteream("youroutput.txt"));
fileOut.println("This will be written to the file");
versed lark
#

i dont think we are allowed to use this command

#

since the only commands we know for reading and printing to files are the ones given by StdIn

zinc saffron
#

then you can either read files or read from the console but not both

#

same with writing

versed lark
#

so in other words i have to do what you are saying

#

wait is the PrintStream given or do i have to import something in order to use iit?

zinc saffron
#

What do you need the input for?

versed lark
#

to fill the unsolved sudoku bpard

versed lark
#

1 sec

#

so if i want to leave the reading from a file i cant also read from console

zinc saffron
#

then you'd need to use either new Scanner(Path.of("someFile.txt")) or FileReader/FileInputStream

versed lark
#

which of these option you think is the easiest

#

or rather what a begginer wold learn

#

scanner?

zinc saffron
#

yeah

#

since you already know how to use Scanner

versed lark
#

ok so

zinc saffron
#

and you would still need to remove the checkmark in Eclipse

#

because with the checkmark, you cannot read anything from the console

versed lark
#

i wont select the option to read from files

#

can you help me a bit cause it my first time doing something like this

#

like this?

zinc saffron
#

yes

#

now every time you want to read from the file, you'd need to use the other Scanner

versed lark
#

these are the arguments

#

wait but each time i have to run the program i have to change the path ?

zinc saffron
#

?

versed lark
#

this i how i did it

zinc saffron
#

if you want different arguments every time, you could do it like that

#

or you run the program once and let the program do the thing multiple times/for each file

versed lark
#
public static void main(String[] args) {
        
        if (args.length != 2) {
            
            System.out.println("Please give the dimension N followed by a <game-file> as the only 2 arguments");
            return;
            
        }

        int N = Integer.parseInt(args[0]);

        if (N == 4 || N == 9) {
            
            Board board = new Board(N);
            In ifile = new In(args[1]);
            Out ofile = new Out("out-" + args[1]);

            boolean boardIsMade = board.readBoard(ifile);

            if (boardIsMade ==  false) {
                
                return;
                    
            }

            boolean valid = board.checkValidity();

            if (valid == false) {
                
                return;
                
            }
            
            play(board);
            
            
            
            for(int i=0; i<N; i++) {
                
                for(int j=0; j<N; j++) {
                    
                    ofile.print(board.getTableauValue(i,j) + " ");
                    
                }
                
                ofile.println();
                
            }
            
            if(isSolved(board) == true) {
                
                System.out.println("Game complete!!!");
                return;
                
            }    
            
            System.out.println("Saving game to out-" + args[1]);
            System.out.println("bye!");
            
            ifile.close();
            ofile.close();
            
        } 
        else {
            
            System.out.println("The allowed value for N is either 4 or 9!");
            System.out.println("Please rerun the program and give a valid value for N");
            return;
            
        }
        ```
zinc saffron
#

oh wait

versed lark
#

you see the commands In ifile = new In(args[1]);

zinc saffron
#

I think In already reads from the file

versed lark
#

yeah

#

i does

zinc saffron
#

so you don't need another Scanner

versed lark
#

wait what?

#

i dont have to select that option in eclispe to read from files?

zinc saffron
#

you can just useifile if you want to read from the file

zinc saffron
#

or at least I don't think so

versed lark
#

omg

#

wait give me 2 min

zinc saffron
#

and ofile should write to the file

versed lark
#

mins

#

bro you are the goat

#

omg tysm

#

you dont have any idea how long i have been trying to solve it for