Challenge

FINAL INDEPENDENT PROJECT

Aim:

The main aim of this project is to create a code for designing the Sudoku grid by using necessory commands and plots.

Commands used in the code:

* flipud – This flip function can flip arrays in any direction.​

*For loop – The Matlab function to execute a group of statements for a specified number of times.​

*If else - Executes a block of code if a specified condition is true​

*Fill and text command ​

MAINCODE:

clear all
close all
clc

S = flipud([6, 0, 0 1, 5, 3, 0, 9, 8; 
2, 0, 0, 0, 0, 4, 0, 1, 0; 
0, 0, 0, 0, 0, 0, 0, 0, 7; 
3, 0, 0, 9, 0, 0, 0, 0, 1;
9, 0, 0, 2, 1, 7, 0, 0, 6; 
4, 0, 0, 0, 0, 5, 0, 0, 2; 
5, 0, 0, 0, 0, 0, 0, 0, 0; 
0, 6, 0, 5, 0, 0, 0, 0, 4; 
1, 9, 0, 8, 3, 2, 0, 0, 5]);
hold on
axis off equal

%using for loops and if conditions
%r=rows
%column

for r = 1:9
for c = 1:9
if S(r, c)~=0
fill([c-1, c, c, c-1, c-1], [r-1, r-1, r, r, r-1], 'y', 'LineWidth', 1.5)
text(c-0.5, r-0.5, int2str(S(r, c)), 'fontsize', 13,...
'HorizontalAlignment','center', 'VerticalAlignment','middle')
else
plot([c-1, c, c, c-1, c-1], [r-1, r-1, r, r, r-1], 'k-', 'LineWidth', 1.5)
end
end
end
for k = 1:9
text(k-0.5, -0.3, int2str(k), 'fontsize', 10, 'HorizontalAlignment', 'center',...
'VerticalAlignment', 'middle')
text(-0.3, k-0.5, int2str(10-k), 'fontsize', 10, 'HorizontalAlignment', 'center',...
'VerticalAlignment', 'middle')
end
Hev = [3, 6];
plot([Hev; Hev], [zeros(1, 2); repmat(9, 1, 2)], 'k', 'linewidth', 3);
plot([zeros(1, 2); repmat(9, 1, 2)], [Hev; Hev], 'k', 'linewidth', 3);

Code Explaination:

1. The script 'S' with the inputs is given randomly within 1 to 9 an 9 by 9 array values and in order to represent the square without numbers zeroes are used.​

2. For loop and if else statements are used for r and c where r indicates rows and c indicates column.

3. Then the inputs for fill and text command is given then the loop gets ended.

for r = 1:9
for c = 1:9
if S(r, c)~=0
fill([c-1, c, c, c-1, c-1], [r-1, r-1, r, r, r-1], 'y', 'LineWidth', 1.5)
text(c-0.5, r-0.5, int2str(S(r, c)), 'fontsize', 13,...
'HorizontalAlignment','center', 'VerticalAlignment','middle')
else
plot([c-1, c, c, c-1, c-1], [r-1, r-1, r, r, r-1], 'k-', 'LineWidth', 1.5)
end
end
end

4. The for loop of k is then given with the text inputs including fontsize and alignment then the loop gets ended.

text(k-0.5, -0.3, int2str(k), 'fontsize', 10, 'HorizontalAlignment', 'center',...
'VerticalAlignment', 'middle')
text(-0.3, k-0.5, int2str(10-k), 'fontsize', 10, 'HorizontalAlignment', 'center',...
'VerticalAlignment', 'middle')
end

5. Then the plotting is done with the necessary inputs.

Hev = [3, 6];
plot([Hev; Hev], [zeros(1, 2); repmat(9, 1, 2)], 'k', 'linewidth', 3);
plot([zeros(1, 2); repmat(9, 1, 2)], [Hev; Hev], 'k', 'linewidth', 3);

Output:

Result:

* Thus the coding is done with the necessory plots fot the sudoku grid and the obtained sudoku grid is shown in the output.

* Inaddition to the code and the output i have also attached the Powerpoint Presentation for this Final Independent Project.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.