- permuting the symbols '1'-'9'.
- permuting any three rows/columns within the same band (e.g. X([4 5 6],:) = X([6 5 4],:)
- permuting any three rows/columns of bands (e.g. X([1:3 4:6 7:9],:) = X([7:9 1:3 4:6],:)
- transposition (e.g. X = X')
Trying to make a sudoku puzzle generator
28 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Greg Dionne
2016-11-18
I like your randperm() idea. You can use it to do the scrambling without any for-loops at all.
You may safely transform a valid Suduko grid into another via:
Have fun!
9 个评论
Greg Dionne
2016-11-18
I find it helpful to step through with the debugger to see what is going on. If you see an error message, Google it! Chances are good someone else may have run into the same issue you did.
Good luck with your program!
更多回答(2 个)
Yeshwanth Devara
2016-11-16
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.
Image Analyst
2019-11-28
(Cleve Moler is the founder of The Mathworks).
There are lots of other fun things in his laboratory in addition to that.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!