Trying to make a sudoku puzzle generator
Show older comments
Before I start, I know that there are tons of Sudoku puzzle generators for MATLAB out there but none of them do what I would like. I am trying to create a Sudoku generator that will take a 9x9 array of zeros and replace the first row with a randomly generated array of the numbers 1-9. To do this, I am using the randperm function as follows:
numb=zeros(9,9);
numb(1,:)=randperm(9,9);
this part works fine. Where I am having issues is generating all following lines. As of right now, I am using a a different for loop for each row with nested for loops inside for each column. An example of the code I am using for the second row of the array is as follows:
for r=2;
for c=1;
numb(r,c)=randi(9);
for i=1:3;
while numb(r,c) == numb(r-1,i);
numb(r,c)=randi(9);
end
end
end
for c=2;
numb(r,c)=randi(9);
for i=1:3;
while numb(r,c) == numb(r-1,i) || numb(r,c) == numb(r,c-1);
numb(r,c)=randi(9);
end
end
end
for c=3;
numb(r,c)=randi(9);
for i=1:3;
while numb(r,c) == numb(r-1,i) || numb(r,c) == numb(r,c-1) || numb(r,c) == numb(r,c-2);
numb(r,c)=randi(9);
end
end
end
for c=4;
numb(r,c)=randi(9);
for i=4:6;
for j=1:(c-1);
while numb(r,c) == numb(r-1,i) || numb(r,c) == numb(r,c-j);
numb(r,c)=randi(9);
end
end
end
end
for c=5;
numb(r,c)=randi(9);
for i=4:6;
for j=1:(c-1);
while numb(r,c) == numb(r-1,i) || numb(r,c) == numb(r,c-j);
numb(r,c)=randi(9);
end
end
end
end
for c=6;
numb(r,c)=randi(9);
for i=4:6;
for j=1:(c-1);
while numb(r,c) == numb(r-1,i) || numb(r,c) == numb(r,c-j);
numb(r,c)=randi(9);
end
end
end
end
for c=7;
numb(r,c)=randi(9);
for i=4:6;
for j=1:(c-1);
while numb(r,c) == numb(r-1,i) || numb(r,c) == numb(r,c-j);
numb(r,c)=randi(9);
end
end
end
end
for c=8;
numb(r,c)=randi(9);
for i=4:6;
for j=1:(c-1);
while numb(r,c) == numb(r-1,i) || numb(r,c) == numb(r,c-j);
numb(r,c)=randi(9);
end
end
end
end
for c=9;
numb(r,c)=randi(9);
for i=4:6;
for j=1:(c-1);
while numb(r,c) == numb(r-1,i) || numb(r,c) == numb(r,c-j);
numb(r,c)=randi(9);
end
end
end
end
end
Can anyone tell me why the code as I have it written will work most of the time but not all the time to check and make sure there are no repeats in the second row, each column, and each 3x3 box? also, if anyone has any tips on how to extend this for loop or a better one to encorporate remaining rows, I'd love your input.
Accepted Answer
More Answers (2)
Yeshwanth Devara
on 16 Nov 2016
I am assuming that you are looking to create a sudoku generator but don't want to use any of the existing ones.
It is possible that you are receiving the issue because the loops for the elements in row 2 columns 7 to 9, you are verifying the columns 4 to 6?
I changed the code to columns 7 to 9 and it seems to work fine.
for c=7;
numb(r,c)=randi(9);
for i=7:9;
for j=1:(c-1);
while numb(r,c) == numb(r-1,i) || numb(r,c) == numb(r,c-j);
numb(r,c)=randi(9);
end
end
end
end
for c=8;
numb(r,c)=randi(9);
for i=7:9;
for j=1:(c-1);
while numb(r,c) == numb(r-1,i) || numb(r,c) == numb(r,c-j);
numb(r,c)=randi(9);
end
end
end
end
for c=9;
numb(r,c)=randi(9);
for i=7:9;
for j=1:(c-1);
while numb(r,c) == numb(r-1,i) || numb(r,c) == numb(r,c-j);
numb(r,c)=randi(9);
end
end
end
end
It would be good to know if you would like to create a sudoku generator that is functionally different from others.
1 Comment
Morgan Clendennin
on 17 Nov 2016
Image Analyst
on 28 Nov 2019
0 votes
(Cleve Moler is the founder of The Mathworks).
There are lots of other fun things in his laboratory in addition to that.
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!