I have a matlab code that creates a puzzle to where the user can move/slide the pieces by using the arrow keys, how can I turn this into an app?

1 view (last 30 days)
My code is VERY long and also contains two functions. Basically it is a little game that takes an image, scrambles the image and allows the user to use the left, right, up, down keys on the computer keyboard to move the pieces. How can I take this working code and transform it into an app using the matlab app design. I need to put this in a working Matlab GUI app. Please assist.
1st function:
function s = puzzlefunc
done = 0;
while done == 0
m = reshape([randperm(8) 9],3,3);
ind = find(m==8);
ind1 = find(m==7);
s=m;
if abs(ind-ind1)<6 && abs(ind-ind1)>3
done = 1;
end
end
2nd function:
function w = additionpuzz(w,ind1,ind2)
temp = w(ind1);
w(ind1) = w(ind2);
w(ind2) = temp;
main script: (this is what creates the puzzle game)
clc;clear;close all;warning off;
image = imread('puzzleIMAGE.jpg');
image1 = imresize(image,[600 600]);
imshow(image1);
text(50,300,'You Have 45 Seconds To Solve This Puzzle :)','FontSize',12,'Color','k','FontWeight','bold');
text(140,350,'Use Arrow Keys To Move: ','FontSize',14,'Color','k');
text(295,400,'\uparrow','FontSize',20,'Color','k','FontWeight','Bold');
text(225,450,'\leftarrow \downarrow \rightarrow','FontSize',20,'Color','k','FontWeight','Bold');
pause(3)
res=1;
for n = 1:3
part1(:,:,n) = image1(1:200,1:200,n);
end
for n = 1:3
part4(:,:,n) = image1(1:200,201:400,n);
end
for n = 1:3
part7(:,:,n) = image1(1:200,401:600,n);
end
for n = 1:3
part2(:,:,n) = image1(201:400,1:200,n);
end
for n = 1:3
part5(:,:,n) = image1(201:400,201:400,n);
end
for n = 1:3
part8(:,:,n) = image1(201:400,401:600,n);
end
for n = 1:3
part3(:,:,n) = image1(401:600,1:200,n);
end
for n = 1:3
part6(:,:,n) = image1(401:600,201:400,n);
end
for n = 1:3
part91(:,:,n) = image1(401:600,401:600,n);
end
part9 = uint8(0.941*ones(200,200,3));
partf = {part1 part4 part7 part2 part5 part8 part3 part6 part91};
part = {part1 part4 part7 part2 part5 part8 part3 part6 part9};
%%
part11 = cell(3,3);
s = puzzlefunc;
for n = 1:9
ind = s(n);
part11(n) = part(ind);
end
part2 = part11;
img = cell2mat(part2);
imshow(img);
move = 0;
tic
%% PLAY
while ~isequal(s,[1:3;4:6;7:9])
move = move + 1;
k = waitforbuttonpress;
cc = get(gcf,'CurrentKey');
ind = find(s==9);
switch ind
case 1
if strcmp(cc,'uparrow')
s = additionpuzz(s,1,2);
elseif strcmp(cc,'leftarrow')
s = additionpuzz(s,1,4);
else
s = s;
end
case 2
if strcmp(cc,'uparrow')
s = additionpuzz(s,2,3);
elseif strcmp(cc,'leftarrow')
s = additionpuzz(s,2,5);
elseif strcmp(cc,'downarrow');
s = additionpuzz(s,2,1);
else
s = s;
end
case 3
if strcmp(cc,'downarrow')
s = additionpuzz(s,3,2);
elseif strcmp(cc,'leftarrow')
s = additionpuzz(s,3,6);
else
s = s;
end
case 4
if strcmp(cc,'uparrow')
s = additionpuzz(s,4,5);
elseif strcmp(cc,'leftarrow')
s = additionpuzz(s,4,7);
elseif strcmp(cc,'rightarrow');
s = additionpuzz(s,4,1);
else
s = s;
end
case 5
if strcmp(cc,'uparrow')
s = additionpuzz(s,5,6);
elseif strcmp(cc,'leftarrow')
s = additionpuzz(s,5,8);
elseif strcmp(cc,'downarrow');
s = additionpuzz(s,5,4);
elseif strcmp(cc,'rightarrow')
s = additionpuzz(s,5,2);
else
s = s;
end
case 6
if strcmp(cc,'downarrow')
s = additionpuzz(s,6,5);
elseif strcmp(cc,'leftarrow')
s = additionpuzz(s,6,9);
elseif strcmp(cc,'rightarrow');
s = additionpuzz(s,6,3);
else
s = s;
end
case 7
if strcmp(cc,'uparrow')
s = additionpuzz(s,7,8);
elseif strcmp(cc,'rightarrow')
s = additionpuzz(s,7,4);
else
s = s;
end
case 8
if strcmp(cc,'downarrow')
s = additionpuzz(s,8,7);
elseif strcmp(cc,'uparrow')
s = additionpuzz(s,8,9);
elseif strcmp(cc,'rightarrow');
s = additionpuzz(s,8,5);
else
s = s;
end
case 9
if strcmp(cc,'downarrow')
s = additionpuzz(s,9,8);
elseif strcmp(cc,'rightarrow')
s = additionpuzz(s,9,6);
else
s = s;
end
end
for n = 1:9
ind = s(n);
part11(n) = part(ind);
end
part2 = part11;
img = cell2mat(part2);
imshow(img);
t = toc;
if t > 45
s = [1:3;4:6;7:9];
res = 0;
end
end
partf1 = cell(3,3);
for n = 1:9
ind = s(n);
partf1(n) = partf(ind);
end
img = cell2mat(partf1);
imshow(img);
if res == 0
text(180,300,'You Ran Out Of Time :(','FontSize',20,'Color','r');
else
text(100,300,['Your Total Moves : ' num2str(move)],'FontSize',20,'Color','r');
  3 Comments
Olivia Garza
Olivia Garza on 6 Apr 2022
@Walter Roberson The other question I did not include my main script and I was trying to input the functions into the app design. I am trying to incorporate everything in to the app design to create a game within the app using the functions and main script.
Walter Roberson
Walter Roberson on 6 Apr 2022
If you are doing this all in app designer, then define each function as a method . The ones that do not need app as an input (in order to get at properties you have defined), then define those as static methods; the ones that do need app, have app be the first parameter.

Sign in to comment.

Answers (0)

Categories

Find more on Word games 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!