Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help writing a program in java. I am having to create a sudoku puzzle. Qu

ID: 3777176 • Letter: I

Question

I need help writing a program in java. I am having to create a sudoku puzzle.

Question: Sudoku is a popular logic puzzle that uses a 9 by 9 array of squares that are organized into 3 by 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, any column, or any of the nine 3 by 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 9 by 9 array of integers that represents the current state of the puzzle, where zero indicates a blank square

• start—a 9 by 9 array of boolean values that indicates 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 of the nonpermanent squares back to blanks (zeros)

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 a 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 currently. In the 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 a given square.

Explanation / Answer

program import java.util.Scanner;
public class Sudoku
{
private static int[][] board = new int[9][9];
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Welcome to the sudoku solver!");
System.out.println("Type inital values separated by spaces, with zeros for blank spots.");
for(int y = 0; y < 9; y++)
{
System.out.print("Enter row values: ");
for(int x = 0; x < 9; x++) {
board[x][y] = keyboard.nextInt();
}
keyboard.nextLine();
}
System.out.println("Initial state: ");
printBoard();
solve(0, 0);
System.out.println("Solved state: ");
printBoard();
}
public static void solve(int x, int y)
{
int[] nextOpenCell = findOpenCell(x, y);
if(nextOpenCell == null) return;
int xPos = nextOpenCell[0];
int yPos = nextOpenCell[1];
System.out.println("xPos: " + xPos + " yPos: " + yPos);
for(int currentNumber = fillCell(1, xPos, yPos); currentNumber != -1;
currentNumber = fillCell(currentNumber+1, xPos, yPos))
{
if(xPos >= 8)
solve(0, yPos+1);
else
solve(xPos+1, yPos);
if(finished()) return;
}
}
public static int fillCell(int val, int x, int y)
{
if(val > 9) return -1;
if(checkRow(val, x) && checkColumn(val, y) && checkBox(val, x, y)) {
board[x][y] = val;
return val;
}
return fillCell(val+1, x, y);
}
public static boolean checkRow(int val, int x)
{
for(int i = 0; i < 9; i++)
{
if(board[x][i] == val) return false;
}
return true;
}
public static boolean checkColumn(int val, int y)
{
for(int i = 0; i < 9; i++)
{
if(board[i][y] == val) return false;
}
return true;
}
public static boolean checkBox(int val, int x, int y)
{
int i = (x / 3) * 3;
int j = (y / 3) * 3;
for(int row = i; row < i+3; row++)
{
for(int column = j; column < j+3; column++)
{
if(board[row][column] == val) return false;
}
}
return true;
}
public static void printBoard()
{
for(int y = 0; y < 9; y++)
{
if(y%3 == 0) System.out.println("");
for(int x = 0; x < 9; x++)
{
if(x%3 == 0) System.out.print(" ");
if(board[x][y] == 0)
System.out.print("_ ");
else
System.out.print(board[x][y] + " ");
}
System.out.println("");
}
}
public static int[] findOpenCell(int x, int y)
{
for(int column = y; column < 9; column++)
{
for(int row = x; row < 9; row++)
{
if(board[row][column] == 0)
{
return new int[] {row, column};
}
}
}
return null;
}
public static boolean finished()
{
for(int row = 0; row < 9; row++)
{
for(int column = 0; column < 9; column++)
{
if(board[row][column] == 0)
return false;
}
}
return true;
}
}