Hi @ Thowseef,
Creating a 6x6 chain-based Sudoku puzzle with the consecutive rule in MATLAB involves several steps, including defining the grid, implementing the rules, and displaying the final results. Below, I will guide you through the process in a detailed manner.
Step 1: Define the Sudoku Grid
Start by initializing a 6x6 grid. In MATLAB, I can represent this grid as a matrix filled with zeros, where each zero indicates an empty cell.
% Initialize a 6x6 Sudoku grid
sudokuGrid = zeros(6, 6);Step 2: Define the Consecutive Rule
The consecutive rule states that no two consecutive numbers (e.g., 1 and 2, 2 and 3, etc.) can be adjacent in any row, column, or 2x3 subgrid. I will create a function to check this rule.
function isValid = checkConsecutiveRule(grid)
isValid = true;
for i = 1:6
for j = 1:6
if j < 6 && abs(grid(i, j) - grid(i, j + 1)) == 1
isValid = false;
return;
end
if i < 6 && abs(grid(i, j) - grid(i + 1, j)) == 1
isValid = false;
return;
end
end
end
end
Step 3: Generate the Sudoku Puzzle
To generate the Sudoku puzzle, I can use a backtracking algorithm. This algorithm will attempt to fill the grid while checking the consecutive rule at each step.
function [solvedGrid] = generateSudoku(grid)
emptyCell = find(grid == 0);
if isempty(emptyCell)
solvedGrid = grid; % Puzzle solved
return;
end
% Randomly select a number to place
for num = 1:6
grid(emptyCell(1)) = num;
if checkConsecutiveRule(grid)
solvedGrid = generateSudoku(grid);
if ~isempty(solvedGrid)
return;
end
end
grid(emptyCell(1)) = 0; % Reset if not valid
end
solvedGrid = []; % No solution found
endStep 4: Display the Final Results
Once the Sudoku puzzle is generated, I can display the final results in a readable format.
% Generate the Sudoku puzzle sudokuGrid = generateSudoku(sudokuGrid);
% Display the final Sudoku grid
disp('Final 6x6 Chain-Based Sudoku Puzzle:');
disp(sudokuGrid);Please see attached.

In the above provided code, the use of backtracking ensures that all constraints are respected, and the final grid is displayed for verification.
Feel free to modify the code to enhance functionality or to explore different Sudoku configurations.
Hope this helps.