Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a script that allows the user to play a simplified version a. Automaticall

ID: 3910951 • Letter: W

Question

Write a script that allows the user to play a simplified version a. Automatically generate a random (matrix of Sudoku. Your script must starting grid (matrix) with some numbers and some example starting grid is: 6. able. An blanon Thestarting grid must be starting gzrid (matrix) with 4. 2 2 b. Ask the user to repeatedly specify the grid location and c. Check to make sure that the user's entry does not break any s number to fill in a blank Sudoku rules. If the entry is not allowed, ask for correct input. Detect when the puzzle example is shown below, with user entries in is finished and display an encouraging message. The finished d. 4 4 4 Develop artificial intelligence that allows your program to solve the puzzle EXTRA CREDIT: autonomously (without any user input), starting with at least 6 blank grid cells

Explanation / Answer

function X = sudoku(X)

% SUDOKU Solve Sudoku using recursive backtracking.

% sudoku(X), expects a 9-by-9 array X.

% Fill in all “singletons”.

% C is a cell array of candidate vectors for each cell.

% s is the first cell, if any, with one candidate.

% e is the first cell, if any, with no candidates.

[C,s,e] = candidates(X);

while ~isempty(s) && isempty(e)

X(s) = C{s};

[C,s,e] = candidates(X);

end

% Return for impossible puzzles.

if ~isempty(e)

return

end

% Recursive backtracking.

if any(X(:) == 0)

Y = X;

z = find(X(:) == 0,1); % The first unfilled cell.

for r = [C{z}] % Iterate over candidates.

X = Y;

X(z) = r; % Insert a tentative value.

X = sudoku(X); % Recursive call.

if all(X(:) > 0) % Found a solution.

return

end

end

end

% ------------------------------

function [C,s,e] = candidates(X)

C = cell(9,9);

tri = @(k) 3*ceil(k/3-1) + (1:3);

for j = 1:9

for i = 1:9

if X(i,j)==0

z = 1:9;

z(nonzeros(X(i,:))) = 0;

z(nonzeros(X(:,j))) = 0;

z(nonzeros(X(tri(i),tri(j)))) = 0;

C{i,j} = nonzeros(z)’;

end

end

end

L = cellfun(@length,C); % Number of candidates.

s = find(X==0 & L==1,1);

e = find(X==0 & L==0,1);

end % candidates

end % sudoku

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote