import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
// To fill a 3x3 grid to be a valid Sudoku grid, you must ensure that each row,
// column, and the 3x3 grid contains the numbers 1 through 9 without repetition.
public class SudokuGridGenerator {
private static final int SIZE = 3;
// Function to shuffle an array
private static void shuffle(Integer[] array) {
List<Integer> list = Arrays.asList(array);
Collections.shuffle(list, new Random());
}
// Function to fill the Sudoku grid
private static void fillSudokuGrid(int[][] grid) {
Integer[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
// Shuffle the numbers randomly
shuffle(numbers);
// Fill the 3x3 grid row by row
int index = 0;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
grid[i][j] = numbers[index++];
}
}
}
// Function to print the grid
private static void printGrid(int[][] grid) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
System.out.print(grid[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
// Initialize an empty 3x3 grid
int[][] grid = new int[SIZE][SIZE];
// Fill the grid with a valid Sudoku configuration
fillSudokuGrid(grid);
// Print the grid
System.out.println("Generated 3x3 Sudoku Grid:");
printGrid(grid);
}
}
/*
run:
Generated 3x3 Sudoku Grid:
7 8 3
9 5 2
4 1 6
*/