Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

So I am able to get my sudoku to check for correct and wrong answer but how do I get the player to be able

So I am able to get my sudoku to check for correct and wrong answer but how do I get the player to be able to play the game?
public class Sudoku {
// Method to check if placing a number at a particular position is safe
public boolean isSafe(int[][] b, int r, int c, int n){
for (int d =0; d < b.length; d++){
// Check if the number is already present in the row or column
if (b[r][d]== n || b[d][c]== n){
return false;
}
}
int sqt =(int) Math.sqrt(b.length);
int boxRowStart = r - r % sqt;
int boxColStart = c - c % sqt;
// Check if the number is already present in the 3x3 box
for (int r1= boxRowStart; r1< boxRowStart + sqt; r1++){
for (int d = boxColStart; d < boxColStart + sqt; d++){
if (b[r1][d]== n){
return false;
}
}
}
return true;
}
// Method to solve the Sudoku puzzle using backtracking
public boolean solveSudoku(int[][] b, int num){
int r =-1;
int c =-1;
boolean isVacant = false;
// Find the first vacant position in the puzzle
for (int i =0; i < num; i++){
for (int j =0; j < num; j++){
if (b[i][j]==0){
r = i;
c = j;
isVacant = true;
break;
}
}
if (isVacant){
break;
}
}
// If there are no vacant positions, the puzzle is solved
if (!isVacant){
return true;
}
// Try placing numbers from 1 to num at the vacant position
for (int no =1; no <= num; no++){
if (isSafe(b, r, c, no)){
b[r][c]= no;
// Recursively try to solve the rest of the puzzle
if (solveSudoku(b, num)){
return true;
} else {
// If the current placement does not lead to a solution, backtrack
b[r][c]=0;
}
}
}
// No valid number can be placed at the current position
return false;
}
// Method to display the Sudoku grid
public void display(int[][] b, int n){
for (int i =0; i < n; i++){
for (int d =0; d < n; d++){
System.out.print(b[i][d]+"");
}
System.out.println();
}
}
// Main method to test the Sudoku solver
public static void main(String[] args){
// Example Sudoku puzzle
int[][] b ={
{5,3,0,0,7,0,0,0,0},
{6,0,0,1,9,5,0,0,0},
{0,9,8,0,0,0,0,6,0},
{8,0,0,0,6,0,0,0,3},
{4,0,0,8,0,3,0,0,1},
{7,0,0,0,2,0,0,0,6},
{0,6,0,0,0,0,2,8,0},
{0,0,0,4,1,9,0,0,5},
{0,0,0,0,8,0,0,7,9}
};
Sudoku obj = new Sudoku();
int size = b.length;
System.out.println("The grid is: ");
obj.display(b, size);
// Solve the Sudoku puzzle and display the solution
if (obj.solveSudoku(b, size)){
System.out.println("The solution of the grid is: ");
obj.display(b, size);
System.out.println("Correct!");
} else {
System.out.println("There is no solution available.");
}
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database 11g SQL

Authors: Jason Price

1st Edition

0071498508, 978-0071498500

More Books

Students also viewed these Databases questions

Question

explain the concept of strategy formulation

Answered: 1 week ago