Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

ok I went through many help and got to the point that I can interact with the game but is there a way to interact

ok I went through many help and got to the point that I can interact with the game but is there a way to interact with the grid and then code checks and gives solution if its right or wrong?
import java.util.Scanner;
public class Sudoku {
public static void main(String[] args){
// Create a Sudoku object
Sudoku obj = new Sudoku();
// Initial Sudoku puzzle (0 represents empty cells)
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}
};
// Display the initial Sudoku grid
int size = b.length;
System.out.println("The grid is:");
obj.display(b, size);
// Allow the player to input numbers into the Sudoku grid
obj.playSudoku(b, size);
// Attempt to solve the Sudoku puzzle and display the solution
if (obj.solveSudoku(b, size)){
System.out.println("Sudoku solved:");
obj.display(b, size);
} else {
System.out.println("No solution exists");
}
}
// Method to allow the player to play Sudoku
public void playSudoku(int[][] b, int num){
Scanner scanner = new Scanner(System.in);
while (true){
System.out.println("Enter row and column (separated by space) to place a number (1 to "+ num +"), or enter -1 to finish:");
int row = scanner.nextInt();
int col = scanner.nextInt();
// Check for the termination condition
if (row ==-1|| col ==-1){
break; // Player wants to finish
}
// Validate the input for row and column
if (row <0|| row >= num || col <0|| col >= num){
System.out.println("Invalid input. Row and column must be between 0 and "+(num -1)+".");
continue;
}
// Get the number to be placed
System.out.println("Enter a number (1 to "+ num +"):");
int number = scanner.nextInt();
// Validate the input number
if (number <1|| number > num){
System.out.println("Invalid number. Enter a number between 1 and "+ num +".");
continue;
}
// Check if the move is safe
if (!isSafe(b, row, col, number)){
System.out.println("Invalid move. The number cannot be placed at this position.");
continue;
}
// Place the number and display the updated grid
b[row][col]= number;
System.out.println("Number placed successfully!");
display(b, num);
}
}
// Method to check if placing a number at a particular position is safe
public boolean isSafe(int[][] b, int row, int col, int number){
// Check row and column
for (int i =0; i < b.length; i++){
if (b[row][i]== number || b[i][col]== number){
return false;
}
}
// Check 3x3 subgrid
int sqrt =(int) Math.sqrt(b.length);
int boxRowStart = row - row % sqrt;
int boxColStart = col - col % sqrt;
for (int r = boxRowStart; r < boxRowStart + sqrt; r++){
for (int d = boxColStart; d < boxColStart + sqrt; d++){
if (b[r][d]== number){
return false;
}
}
}
return true;
}
// Method to solve the Sudoku puzzle using backtracking
public boolean solveSudoku(int[][] b, int num){
int row =-1;
int col =-1;
boolean isEmpty = true;
// Find the first empty cell
for (int i =0; i < num; i++){
for (int j =0; j < num; j++){
if (b[i][j]==0){
row = i;
col = j;
isEmpty = false;
break;
}
}
if (!isEmpty){
break;
}
}
// If there is no empty cell, then the puzzle is solved
if (isEmpty){
return true;
}
// Try placing numbers in the empty cell
for (int number =1; number <= num; number++){
if (isSafe(b, row, col, number)){
b[row][col]= number;
// Recursively solve the rest of the grid
if (solveSudoku(b, num)){
return true;
} else {
b[row][col]=0; // Backtrack
}
}
}
return false; // No solution found
}
// Method to display the Sudoku grid

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

Databases And Python Programming MySQL MongoDB OOP And Tkinter

Authors: R. PANNEERSELVAM

1st Edition

9357011331, 978-9357011334

Students also viewed these Databases questions