Sudoku is a popular logic puzzle that uses a 9 by 9 array of squares that are or
ID: 3666856 • Letter: S
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)
Explanation / Answer
import java.util.Scanner;
public class SudokuPuzzle {
private int board [][];
private int start[][];
public SudokuPuzzle(){
start = new int [9][9];
board = new int [9][9];
}
public String toString(){
String puzzleString = "Row/Col 1 2 3 4 5 6 7 8 9 ";
puzzleString = puzzleString+ " -------------------------- ";
for (int i=0; i<9; i++){
puzzleString = puzzleString + (i+1) + " |";
for (int j=0; j<9; j++){
if (board [i][j] == 0)
puzzleString = puzzleString + " " + ".|";
else
puzzleString = puzzleString + " " +board [i][j] + "|";
}
puzzleString = puzzleString + " ";
puzzleString = puzzleString + " |__|__|__|__|__|__|__|__|__| ";
}
return puzzleString;
}
public void addInitial(int row, int col, int value){
if (row>=0 && row<=9 && col >=0 && col <=9 && value >=1 && value <=9){
start [row][col] = value;
board [row][col] = value;
}
}
public void addGuess(int row, int col, int value){
// only set the value if the start is 0
if (row>=0 && row<=9 && col >=0 && col <=9 && value >=1 && value <=9 && start [row][col] == 0){
board [row][col]= value;
}
}
public int getValueIn(int row, int col){
return board[row][col];
}
public void reset(){
for (int i=0;i<9;i++)
for( int j=0;j<9;j++)
board[i][j] = start[i][j];
}
public boolean isFull(){
boolean allFilled = true;
for (int i=0;i<9;i++)
for( int j=0;j<9;j++)
allFilled = allFilled && board[i][j]>0;
return allFilled;
}
public boolean[] getAllowedValues(int row, int col){
// save the value at the location, thren try all 9 values
int savedValue = board[row][col];
boolean result[] = new boolean[9];
for (int value = 1; value <=9; value++){
board [row][col] = value;
result[value-1] = checkPuzzle();
}
board [row][col] = savedValue;
return result;
}
public boolean checkPuzzle(){
boolean looksGood = true;
for (int i=0;i<9;i++){
looksGood = looksGood && checkRow(i);
looksGood = looksGood && checkCol(i);
looksGood = looksGood && checkSub(i);
}
return looksGood;
}
public boolean checkRow(int row){
int count[]= new int[10];
for (int col=0;col<9;col++){
count[board[row][col]]++;
}
boolean countIsOk = true;
for(int i=1; i<=9; i++)
countIsOk = countIsOk && (count[i]<=1);
return countIsOk;
}
public boolean checkCol(int col){
int count[] = new int[10];
for(int row=0; row<9; row++){
count[board[row][col]]++;
}
boolean countIsOk = true;
for(int i=1; i<=9; i++)
countIsOk = countIsOk && (count[i]<=1);
return countIsOk;
}
public boolean checkSub(int sub){
int count[] = new int[10];
int rowBase = (sub/3) *3;
// The above will give 0, 3, or 6 because of integer division
int colBase = (sub%3) *3;
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
count[board[rowBase+i][colBase+j]]++;
}
}
boolean countIsOk = true;
for(int i=1; i<=9; i++)
countIsOk = countIsOk && (count[i]<=1);
return countIsOk;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.