Code analysis and solution to transfer of values

2 views (last 30 days)
Manuel
Manuel on 20 Dec 2022
Answered: Muskan on 24 Mar 2023
Hi everyone,
I'm working on a code for a game that shows a player a sequence of images starting at 1 image and progressing to up to a sequence of 30 images. The code relies on 2 matrices that are exactly the same, one for player input and the other that takes the randomized order of 6 images(the order can have the same image multiple times in that sequence) and transfers that randomized order onto the other matrix for comparison. The code compares each row of the two matrices after player input and if the rows do not match the game is over and a score is generated. However, the sort order that is input into the matrix is not correct because it does not match the sequence of images that was shown. If any could tell me how to fix this code, specifically in the second while loop.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
answer = questdlg('Are you ready to play the visual memory game?','Visual Memory Game','Yes','No','No');
Game = 0;
switch answer
case 'Yes'
disp([answer '. Get ready to begin!'])
Game = 1;
case 'No'
disp([answer '. Wrong answer, please try again.'])
Game = 0;
end
while Game > 0
myfolder = 'C:\Users\Beansie nana\Documents\MATLAB\Project Images';
filePattern = fullfile(myfolder, '*.jpg');
disp('Castle = 1')
disp('Chameleon = 2')
disp('Mountain = 3')
disp('Tiger = 4')
disp('Train = 5')
disp('Tree = 6')
O = zeros(31,31);
O(:,:) = 7;
d = triu(O,1); % O and d are lower triangle matrix for sequence of images
d(31,:) = 7; % 31st row filled in with ones stops the the while loop for n=31
d_P = triu(O,1); %
d_P(31,:) = 7;
l=1;
T=4; % The max level
Score = 0;
for n = 1:T % can go on to 10 levels
while d(n,l)<7
theFiles = dir(filePattern);
%----------------------------------------->
sortOrder = zeros(1,n);
sortOrder(1,:) = 0;
for r = 1:n
sortOrder(1,r) = ceil(6*rand());
end
%-----------------------------------------<
theFiles = theFiles(sortOrder);
for k = 1
baseFileName = theFiles(k).name;
fullFileName = fullfile(myfolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
imshow(imageArray);
drawnow;
pause(.15)
end
close
d(n,l) = sortOrder(1,l); %%there is a problem with columns
%%
l=l+1;
end
for j = 1:l-1
Ans_wer = inputdlg('What is the numerical image sequence?','Input',[1 50]);
W = cellfun(@str2num,Ans_wer);
d_P(n,j) = W;
end
l=1;
end
Game = 0;
end

Answers (1)

Muskan
Muskan on 24 Mar 2023
Hi Manuel,
I tried to run your code, and observed that the variable “theFiles” is actually a 0×1 empty struct array and not an array itself and that is probably why you are getting the error.
I hope this resolves your query.
Thanks,
Muskan

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!