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;
}
#Sudoku help
137 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @versed lark! Please use
/closeor theClose Postbutton 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.
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
that method is empty except a single print statement
Can you show your getUserInput?
If Scanner#nextLine reads an empty line after calling a different Scanner#next method, take a look at this StackOverflow post.
I guess it would be this
even when i put a String input = StdIn.readLine(); it just ignores it
how can i fix that?
oh wait what is StdIn
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);
}
}
}
```
Can you show the Stdin parts?
as well as getUserInput
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
Are you using Scanner/System.in/In anywhere before that?
because if so, it's probably that issue
If Scanner#nextLine reads an empty line after calling a different Scanner#next method, take a look at this StackOverflow post.
i dont use it before
how can i fix it could you tell me
where you do String input = scanner.nextLine().trim();
try adding another scanner.nextLine() before
what do you mean with it being skipped?
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
How are you running the program?
eclipse
are you launching it with a debugger or normal ?
i only use StdLib
How are you using it?
wdym
Are you using that library anywhere?
what is the value of input when it is skipped?
I think it might be reading stuff from a file instead of your console?
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
yeah so Input File is set here
yeah
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
i must read from file
but don't forget to re-enable it if necessary
and also i must read from the console
well that option configures it to replace the console by a file
cause the input to fill the sudoku must be read from file
but if the application should read from a file, you can do that without the option as well
how?
e.g. you can define a Scanner that just reads from a file
never done that before
can the scanner also output to a new file?
that it creates?
Scanner only does input
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
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
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");
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
then you can either read files or read from the console but not both
same with writing
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?
What do you need the input for?
to fill the unsolved sudoku bpard
you'd need to import it
then you'd need to use either new Scanner(Path.of("someFile.txt")) or FileReader/FileInputStream
which of these option you think is the easiest
or rather what a begginer wold learn
scanner?
ok so
and you would still need to remove the checkmark in Eclipse
because with the checkmark, you cannot read anything from the console
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?
yes
now every time you want to read from the file, you'd need to use the other Scanner
these are the arguments
wait but each time i have to run the program i have to change the path ?
?
this i how i did it
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
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;
}
```
oh wait
you see the commands In ifile = new In(args[1]);
I think In already reads from the file
so you don't need another Scanner
you can just useifile if you want to read from the file
no
or at least I don't think so
and ofile should write to the file