Clear Filters
Clear Filters

Sudoku Checker code that checks for a correct sudoku

13 views (last 30 days)
I am trying to write a code that checks if a solved Sudoku puzzle is correct or not. I don't know if I did any of this right.
function [] = SudokuChecker( sudoku )
sze = length( sudoku );
sample = 1:9;
%check rows
for i = 1:sze
y = sudoku(i,:);
x = ismember(y, sample);
if sum(x) ~= 9
disp("false sudoku at row: "+i)
end
end
for j = 1:sze
y = sudoku(:, j);
x = ismember(y, sample);
if sum(x) ~= 9
disp("false sudoku at col: "+j)
end
end
for k = 0:8
row = 3*floor(k/3);
col = 3*mod(k,3);
for colNew = col+(1:3)
y = sudoku(:, colNew);
x = ismember(y, sample);
if sum(x) ~= 9
disp("false sudoku at subsquare col: "+colNew)
end
for rowNew = row+(1:3)
y = sudoku ( rowNew, : ) ;
x = ismember(y, sample);
if sum(x) ~= 9
disp("false sudoku at subsquare row: "+rowNew)
end
end
end
end
end
[SL: Edited to format the code as code. In the future please use the code mode button (the first button in the Code section of the toolstrip above the editor box) to enter code to enable syntax highlighting. IMO that also makes it easier to read.]
  1 Comment
Derrick Joseph
Derrick Joseph on 1 Oct 2019
When I test it, it gives me the right output if I change one of the values to zero but when i change it to a repeated number, it gives me a true instead of a false value

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 1 Oct 2019
As written, you're checking that all the elements in each row, column, and sub-matrix are in the set 1:9. That's a necessary condition for a Sudoku solution to be correct, but it's not sufficient as you realized (it doesn't guard against repeated numbers.)
As stated on Wikipedia's Sudoku page, "The objective is to fill a 9×9 grid with digits so that each column, each row, and each of the nine 3×3 subgrids that compose the grid (also called "boxes", "blocks", or "regions") contain all of the digits from 1 to 9." It's difficult to give a clue as to how to improve your code without giving you the solution, but since you showed what you've done: can the sort function help you?
A couple other suggestions:
sze = length( sudoku );
I wouldn't use length here as you know/expect sudoku to be a matrix. I'd probably use ismatrix (to eliminate your user passing in a 9-by-3-by-3 array) and size then check the output from size that sudoku is a square matrix. Doing this could also allow you to generalize your checker to any (n^2)-by-(n^2) Sudoku grid. The standard Sudoku has n = 3 for a 9-by-9 grid, but I've seen n = 2 (4-by-4 grid) and n = 4 (16-by-16 grid) Sudoku problems in puzzle magazines.
if sum(x) ~= 9
disp("false sudoku at row: "+i)
end
Rather than just displaying a message, unless the terms of your assignment state that your function should return no outputs and just display a message, I'd also return an output argument that is true if the input is a valid Sudoku solution and false if it isn't.
for k = 0:8
row = 3*floor(k/3);
col = 3*mod(k,3);
for colNew = col+(1:3)
The colon operator can accept two or three inputs. In the two input case, the step between elements in the result is 1. In the three input case, the middle input is the step. You can take advantage of that to simplify these loops.
x = 1:9
y = 1:2:9
  2 Comments
Derrick Joseph
Derrick Joseph on 1 Oct 2019
So are you saying to use the sort function and then find the repeated values that way?
Steven Lord
Steven Lord on 1 Oct 2019
The vector [3 1 4 5 9 2 6 7 8] is a valid Sudoku solution row. It contains each of the integer values between 1 and 9 inclusive exactly once.
The vector [3 1 4 1 5 9 2 6 7] is not a valid Sudoku solution row. It contains two 1's and no 8's.
The vector [3 1 4 0 5 9 2 6 7] is not a valid Sudoku solution row. It contains no repeats, but contains a 0 and no 8.
Do you notice anything interesting if you sort each?

Sign in to comment.

Categories

Find more on Sudoku in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!