Friday, January 24, 2025
HomeGamesHow to Play Sudoku in Java

How to Play Sudoku in Java

Sudoku is a popular number-placement puzzle that challenges players to fill a 9×9 grid so that each row, column, and 3×3 sub-grid contains all the digits from 1 to 9. Writing a Sudoku game in Java can be a fun way to explore programming concepts like arrays, backtracking, and user input. This article will guide you through building a simple Sudoku game in Java.

Steps to Create a Sudoku Game in Java

Understand the Basics of Sudoku

    • A Sudoku grid is a 9×9 board divided into nine 3×3 sub-grids.
    • The goal is to fill the board such that:
      • Each row contains the digits 1 to 9 without repetition.
      • Each column contains the digits 1 to 9 without repetition.
      • Each 3×3 sub-grid contains the digits 1 to 9 without repetition.
    • Some cells are pre-filled with numbers to help players get started.

Set Up the Java Environment

    • Use an Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse, or VS Code.
    • Create a new Java project and add a SudokuGame class.
See also  What is the Best PC Gaming Headset?

Create the Data Structure Use a 2D array to represent the Sudoku grid. Each element in the array represents a cell on the board.

java
public class SudokuGame {
private int[][] board = {
{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}
};
}

Here, 0 represents an empty cell.

Display the Sudoku Board Write a method to print the Sudoku grid to the console.

java
public void printBoard() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}

 

Add a Validation Method To ensure a valid Sudoku board, write a method to check whether a number can be placed in a specific cell.

java
public boolean isValid(int row, int col, int num) {
// Check the row
for (int i = 0; i < 9; i++) {
if (board[row][i] == num) return false;
}

// Check the column
for (int i = 0; i < 9; i++) {
if (board[i][col] == num) return false;
}

// Check the 3x3 sub-grid
int startRow = row - row % 3;
int startCol = col - col % 3;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[startRow + i][startCol + j] == num) return false;
}
}

return true;
}

 

Implement the Solver (Backtracking Algorithm) Use a backtracking algorithm to solve the Sudoku puzzle programmatically.

java
public boolean solveSudoku() {
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
if (board[row][col] == 0) { // Find an empty cell
for (int num = 1; num <= 9; num++) {
if (isValid(row, col, num)) {
board[row][col] = num; // Tentative assignment
if (solveSudoku()) { // Recursively solve
return true;
}
board[row][col] = 0; // Backtrack
}
}
return false; // No valid number found
}
}
}
return true; // Solved
}

 

Add a Main Method to Run the Game Finally, write a main method to initialize the game, display the board, and solve the puzzle.

java
public static void main(String[] args) {
SudokuGame game = new SudokuGame();
System.out.println("Initial Sudoku Board:");
game.printBoard();

if (game.solveSudoku()) {
System.out.println("\nSolved Sudoku Board:");
game.printBoard();
} else {
System.out.println("\nNo solution exists for the given Sudoku puzzle.");
}
}

How It Works

  1. The printBoard method displays the grid before and after solving.
  2. The isValid method ensures that numbers are placed according to Sudoku rules.
  3. The solveSudoku method uses backtracking to fill empty cells and find a solution.

Output Example

Initial Sudoku Board

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

Solved Sudoku Board

5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9

This article demonstrates how to create and solve a Sudoku game in Java using a 2D array, validation rules, and a backtracking algorithm. You can extend this program by adding features like user input, graphical interfaces, or generating new puzzles dynamically. Happy coding!

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x