Overview You are to write a Java program to allow a user to play Sudoku. Sudoku
ID: 3833651 • Letter: O
Question
Overview You are to write a Java program to allow a user to play Sudoku. Sudoku is a popular logic puzzle that uses a 9 x 9 array of squares that are organized into 3 x 3 subarrays. The puzzle solver must fill in the squares with the digits 1 to 9 such that no digit is repeated in any row, column, or any of the nine 3 x 3 subgroups of squares. Initially, some squares are filled in already and cannot be changed. For example, the following might be a starting configuration for a Sudoku puzzle. Create a class SudokuPuzzle that has the attributes: board – a 9x9 array of integers that represents the current state of the puzzle, where 0 indicates a blank square start – a 9 x 9 array of boolean values that indicate which squares in board are given values that cannot be changed. And the following methods: SudokuPuzzle – a constructor that creates an empty puzzle toString – returns a string representation of the puzzle that can be printed addInitial(row, col, value) – sets the given square to the given value as an initial value that cannot be changed by the puzzle solver addGuess(row, col, value) – sets the given square to the given value; the value can be changed later by another call to addGuess • checkPuzzle – returns true if the values in the puzzle do not violate the restrictions • getValueIn(row, col) – returns the value in the given square • getAllowedValues(row, col) – returns a one-dimensional array of nine Booleans, each of which corresponds to a digit, and is true if the digit can be placed in the given square without violating the restrictions • isFull – returns true if every square has a value • reset – changes all nonpermanent squares back to blanks Write a main method in the class Sudoku that creates a SudokuPuzzle object and sets its initial configuration. Then use a loop to allow someone to play Sudoku. Display the current configuration and ask for a row, column, and value. Update the game board and display it again. If the configuration does not satisfy the restrictions, let the user know. Indicate when the puzzle has been solved correctly. In that case both checkPuzzle, and isFull would return true. You should also allow options for resetting the puzzle, and displaying the values that can be placed in any given square. Suggestions (Use at your discretion) Write the method to display the board first. This will be instrumental in debugging All row and col values should be 0 based. Must not be an Applet, do it as a text based game. One (1) “run” of the program, should show the following: -a completed correct puzzle -an invalid move -the list of available numbers for any square in the game -the completed array for a game, and the Boolean array for the same game.
I've tried working around with this and my working code is below. Much help appreciated if you can fix my code, and make it run.
Working Code:
import java.util.Scanner;
public class Sudoku {
private Cell [][] board= new Cell [9][9];
Scanner keyboard = new Scanner (System.in);
public void displayTheBoard() {
System.out.println(" 0 1 2 3 4 5 6 7 8");
System.out.println(" ________________________________________________________");
System.out.println(" || | | || | | || | | ||");
System.out.println(" 0 ||__"+ board[0][0] + "__|__" + board[0][1] + "__|__" + board[0][2]+ "_||"+ "__" + board[0][3]+ "__|__" + board[0][4]+ "__|__" + board[0][5]+ "_||__"+ board[0][6]+ "__|__" + board[0][7]+ "__|__" + board[0][8]+ "_||");
System.out.println(" || | | || | | || | | ||");
System.out.println(" 1 ||__"+ board[1][0] + "__|__" + board[1][1] + "__|__" + board[1][2]+ "_||"+ "__" + board[1][3]+ "__|__" + board[1][4]+ "__|__" + board[1][5]+ "_||__"+ board[1][6]+ "__|__" + board[1][7]+ "__|__" + board[1][8]+ "_||");
System.out.println(" || | | || | | || | | ||");
System.out.println(" 2 ||__"+ board[2][0] + "__|__" + board[2][1] + "__|__" + board[2][2]+ "_||"+ "__" + board[2][3]+ "__|__" + board[2][4]+ "__|__" + board[2][5]+ "_||__"+ board[2][6]+ "__|__" + board[2][7]+ "__|__" + board[2][8]+ "_||");
System.out.println(" ________________________________________________________");
System.out.println(" || | | || | | || | | ||");
System.out.println(" 3 ||__"+ board[2][0] + "__|__" + board[2][1] + "__|__" + board[2][2]+ "_||"+ "__" + board[2][3]+ "__|__" + board[2][4]+ "__|__" + board[2][5]+ "_||__"+ board[2][6]+ "__|__" + board[2][7]+ "__|__" + board[2][8]+ "_||");
System.out.println(" || | | || | | || | | ||");
System.out.println(" 4 ||__"+ board[2][0] + "__|__" + board[2][1] + "__|__" + board[2][2]+ "_||"+ "__" + board[2][3]+ "__|__" + board[2][4]+ "__|__" + board[2][5]+ "_||__"+ board[2][6]+ "__|__" + board[2][7]+ "__|__" + board[2][8]+ "_||");
System.out.println(" || | | || | | || | | ||");
System.out.println(" 5 ||__"+ board[2][0] + "__|__" + board[2][1] + "__|__" + board[2][2]+ "_||"+ "__" + board[2][3]+ "__|__" + board[2][4]+ "__|__" + board[2][5]+ "_||__"+ board[2][6]+ "__|__" + board[2][7]+ "__|__" + board[2][8]+ "_||");
System.out.println(" ________________________________________________________");
System.out.println(" || | | || | | || | | ||");
System.out.println(" 6 ||__"+ board[2][0] + "__|__" + board[2][1] + "__|__" + board[2][2]+ "_||"+ "__" + board[2][3]+ "__|__" + board[2][4]+ "__|__" + board[2][5]+ "_||__"+ board[2][6]+ "__|__" + board[2][7]+ "__|__" + board[2][8]+ "_||");
System.out.println(" || | | || | | || | | ||");
System.out.println(" 7 ||__"+ board[2][0] + "__|__" + board[2][1] + "__|__" + board[2][2]+ "_||"+ "__" + board[2][3]+ "__|__" + board[2][4]+ "__|__" + board[2][5]+ "_||__"+ board[2][6]+ "__|__" + board[2][7]+ "__|__" + board[2][8]+ "_||");
System.out.println(" || | | || | | || | | ||");
System.out.println(" 8 ||__"+ board[2][0] + "__|__" + board[2][1] + "__|__" + board[2][2]+ "_||"+ "__" + board[2][3]+ "__|__" + board[2][4]+ "__|__" + board[2][5]+ "_||__"+ board[2][6]+ "__|__" + board[2][7]+ "__|__" + board[2][8]+ "_||");
}
public void SudokuPuzzle(){
for (int i =0; i<9; i++){
for(int j=0; j<9; j++){
board[i][j]= new Cell();
}
}
}
public String getAllowedValuesString (int row, int col){
return board [row][col].getAllowedValueString();
}
public void addInitial (int row, int col,int value){
board [row][col].setValue(value);
board[row][col].isFixed= true;
}
public void reset(){
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if(!isFixedValue(i,j)){
board[i][j].isEmpty();
}
}
}
}
public Cell cell(int row, int col){
return board[row][col];
}
public boolean addGuess(int row, int col, int value){
return board[row][col].setValue(value);
}
public boolean isFull(){
for(int x=0;x<9;x++){
for(int y=0;y<9;y++){
if(board[x][y].isEmpty())
return false;
}
}
} return true
public int getValues(int row, int col){
return board[row][col].value;
}
public boolean isFixedValue (int row,int col){
return board[row][col].isFixed;
}
public String toString(){
String output="";
output +="------,ln";
for (int i=0;i<9; i++){
output +="1";
for(int j=0;j<9;j++){
if (board [i][j].value<0)
output+="";
else
output+=board[i][j]+"";
if(i%3==2)
output+="/";
else
output+=":";
}
if(i%3==2)
output+=(" == ");
else
output+=(" ---- ");
}
return output;
}
public boolean isSolved(){
for(int x=0;x<9;x++{
for(in y=0;y<9;y++){
//if every cell has its final solution in place
if(board[x][y].isSolved()){
return false;
}
}
}
} return true; //if not located then result is failed and/or the problem solved
public boolean isOver(){
//the game is completed when every spot is filled
return (isFull() && isSolved());
}
//initialize board
public void initializeBoard(){
addInitial (0,0,1);
addInitial (0,1,2);
addInitial (0,2,3);
addInitial (0,3,4);
addInitial (0,4,9);
addInitial (0,5,7);
addInitial (0,6,8);
addInitial (0,7,6);
addInitial (0,8,5);
addInitial (1,0,4);
addInitial (1,1,5);
addInitial (1,2,9);
addInitial (2,0,6);
addInitial (2,1,7);
addInitial (2,2,8);
addInitial (3,0,3);
addInitial (3,4,1);
addInitial (4,0,2);
addInitial (5,0,9);
addInitial (5,5,5);
addInitial (6,0,8);
addInitial (7,0,7);
addInitial (8,0,5);
addInitial (8,3,9);
}
private class Cell{
private static final int LOCATION_EMPTY=0;
public int row,column,vlue;
public boolean isFixed;
public Cell(){
value=LOCATION_EMPTY;
column=row=0;
}
public boolean isEmpty(){
returnValue == LOCATION_EMPTY;
}
public boolean setValue(int value){
if(isFixed)
throw new ImmutableValueException()
//make sure values all in the right range
if(value>9||value<=0)
throw new IllegalArgumentException
("The Value Must Be Between 1 to 9");
//enforce game logic
if(!isAllowed(value))
throw new DuplicateValueException();
this.value=value;
return type;
}
public String getAllowedValueString(){
boolean() vals=getAllowedValues();
String output ="";
for(int i=0; i
if(vals[i]){
output+=i+1+",";
}
} return output.substringCO,output.lastIndexof(",");
}
}
private boolean[] getAllowedValues(){
boolean[] valNums=newboolean[9];
//set all to true
for(int i=0;i<9;i++){
valNums[i] true;
}
//checks the rows input
for(int i=0;i<9;i++){
if(!board[row][1].isEmpty()){
valBums[board[row][1].value=1]=false;
}
}
}
//checks the columns input
for (int i=0;i<0;i++){
if(!board[row][i].isEmpty()){
valNums[board][row][i].value-1]=false;
}
}
int rl=(row/3)*3;
int rh= rl+ 3;
int cl= (column/3)*3;
int ch= cl +3;
for (int r=rl;r
for (int c=cl;c
if(!board[r][c].isEmpty())
valNums[board[r][c].value-1]-false;
}
}
int thisVal=getValueIn(row,column);
if(thisVal !=LOCATION_EMPTY)
ValNums[this.val -1]=true;
return valNums;
}
public boolean isSolved(){
return Integer.parseInt(getAllowedValueString())==Value;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Sudoku game = new Sudoku();
game.displayBoard();
game.SudokuPuzzle();
game.initializeBoard();
System.out.println(game.toString());
String input ="r";
int row, column,value;
row=column=value=0;
do{
System.out.println("Enter a row,column, and a numerical value:");
try{
input= game.getInput();
row= Integer.parseInt(input);
} catch (NumberFormatException e){
if(input.equalsIgnoreCase("-")){
game.reset();
System.out.println("Board Will Reset. Beginning To Start Over");
System.out.println(game.toString());
} else{
System.out.println("Invalid Row Value. Please Try Again");
} continue;
}
try{
input= game.getInput();
column= Integer.parseInt(input);
} catch(NumberFormatException e){
System.out.println("Invalid Column");
continue;
}
try{
input = game.getInput();
value= Integer.parseInt(input);
} catch (NumberFormatException e){
if (input.equals("?")){
System.out.println("Acceptable Values:"+game.getAllowedValuesString(row,column));
} else {
System.out.println("Invalid Value, try again");
} continue;
}
if(game.isOver())
System.out.println("Congrats!");
System.out.println(game.toString());
} while(!game.isOver());
}
private String getInput(){
Scanner in = new Scanner(System.in);
return in.next();
}
}
Explanation / Answer
The values which you were inserting manually is not an appropriate way. Therefore I have modified your code by taking the values from user, If you want same values you can simply do by inserting through command prompt. Here we go Thank You. public class Sudoku { public static void main(String[] args) { int[][] matrix = parseProblem(args); writeMatrix(matrix); if (solve(0,0,matrix)) // solves in place writeMatrix(matrix); else System.out.println("NONE"); } static boolean solve(int i, int j, int[][] cells) { if (i == 9) { i = 0; if (++j == 9) return true; } if (cells[i][j] != 0) // skip filled cells return solve(i+1,j,cells); for (int val = 1; valRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.