I have a problem that the counter doesn`t increase in case 3, this code is an intelligence test that give you 3 stages

1 view (last 30 days)
name = input('Please enter your name to proceed: \n', 's');
clc
disp(['Hello ', name, '!', newline, 'We would like to welcome you to GeniusQuest.', newline, 'A game that consists of three different phases.', newline, 'Each phase contributes to determining your intelligence level!', newline, 'We believe in democracy, therefore, you, dear ', name, ', will choose what phase to begin the game with :)',newline, 'But HEY, you are required to finish them all!'])
count = 0;
while true
ST = input('\n Please type ready to pick the required phase! \n', 's');
clc
if strcmpi(ST, 'ready')
fprintf('Great! Let''s get started.\n');
else
fprintf('Whoops! It seems like you are not ready yet!\n');
break;
end
begin = menu("Please click on the phase you require to finish now, and click exit once you are done", "1. MATH PROBLEMS", "2. GENERAL KNOWLEDGE", "3. MEMORY GAME", "4. EXIT");
switch begin
case 1
clc
disp('You have chosen to solve the math problems! How brave of you?')
disp(['GENERAL INSTRUCTIONS:', newline, '1- All the answers are numerical (do include the sign if needed).', newline, '2- If your answer consisted of letters, punctuation marks...etc, it will be dismissed.', newline '3- Scores are equally distributed.'])
SCM = input('Shall we begin? (yes/no): ', 's');
if strcmpi(SCM, 'yes')
clc
M1Q = input('Mary surveyed students in her school to find out what their favourite sports were.\nOut of 1,200 students, 120 said hockey was their favourite sport.\nWhat percentage of students said hockey was their favourite sport? \n');
if str2double(M1Q) == 10
count = count + 1;
end
clc
M2Q = input('Solve for x; 5x + 499 + 38 + 92x - 3x + 6 = 231 \n');
if str2double(M2Q) == -3
count = count + 1;
end
clc
M3Q = input('Complete the pattern; 1, 9, 49, 249, 1249, 6249, ... \n');
if str2double(M3Q) == 31249
count = count + 1;
end
clc
M4Q = input('Joey has 6 siblings.\nAll of them were born 2 years apart. \nThe youngest is Chloe who is only 7 years old while Joey is the eldest.\nCalculate the age of Joey.\n');
if str2double(M4Q) == 19
count = count + 1;
end
clc
M5Q = input('9876, 6987, 7698, What is the next number in the series? \n');
if str2double(M5Q) == 8769
count = count + 1;
end
clc
disp(['You have reached the end of the Mathematics phase.', newline, 'Bravo for making it out in one piece!'])
elseif strcmpi(SCM, 'no')
disp('Math might seem intimidating but why not try?')
else
disp('Invalid answer. Please try again')
end
case 2
clc
disp('General knowledge would not be difficult for you, or would it?')
disp(['GENERAL INSTRUCTIONS:', newline, '1- Please type all of your answers in small letters.', newline, '2- Answers with punctuation marks, capital letters, numbers...etc will be dismissed.', newline, '3- Scores are equally distributed.', newline, '4- All answers consist of NO MORE THAN ONE WORD.'])
SCG = input('Shall we begin? (yes/no): ', 's');
if strcmpi(SCG, 'yes')
clc
GQ1 = input('.... river is the longest river in the world. \n', 's');
if strcmpi(GQ1, 'nile')
count = count + 1;
end
clc
GQ2 = input('Which star is the nearest star to earth? \n', 's');
if strcmpi(GQ2, 'sun')
count = count + 1;
end
clc
GQ3 = input('Brown skin caused by being in the sun is the definition of: \n', 's');
if strcmpi(GQ3, 'tan')
count = count + 1;
end
clc
GQ4 = input('If you have one, you want to share it.\nBut once you share it, you do not have it.\nWhat is it?\n', 's');
if strcmpi(GQ4, 'secret')
count = count + 1;
end
clc
GQ5 = input('I start out tall, but the longer I stand, the shorter I grow. What am I?\n', 's');
if strcmpi(GQ5, 'candle')
count = count + 1;
end
clc
disp(['You have reached the end of the general knowledge phase.', newline, 'Bravo!'])
else
disp('All you require is focus. We believe in you! Let us try again!');
end
case 3
clc
disp('If you remember where you were on May 23rd, this game would be a piece of cake.')
disp(['GENERAL INSTRUCTIONS:', newline, '1- The Cards Will Be Shown For 10 Seconds, Don`t Blink Or You Will Miss it!', newline, '2- The Game Has A Timer, Try To Finish As Fast As You Can!'])
disp('3- A Window will Be Shown Every 20 Seconds To Determine If You Finished The Game, Don`t Ignore It Or It Will Consider Cheating!')
MMG = input('Shall we begin? (yes/no): ', 's');
if strcmpi(MMG, 'yes')
clc
% Showing the Cards Before Starting The Game
Image = imshow('Cards.png');
% A Timer To Delete The Image After It Elapsed
Time = timer('StartDelay', 10, 'TimerFcn', 'Stat = false; fprintf(''\n Time!! Here It Goes Player, Give It All You Got!!''); delete(Image)');
start(Time)
Stat=true;
T=1;
while Stat==true
fprintf('\n %d ', T)
pause(1)
T=T+1;
end
% Starting The Game
MemoryGame
M = 5;
S = true;
K=1;
while K < 121
if K == 25|| K == 45 || K == 65 || K == 85 || K == 105
ForP = menu('Did You Finish The Game Player?', 'Yep, Esey As A Piece Of Cake!!', 'Just Need A Few Seconds!');
switch ForP
case 1
fprintf('\n Excellent Memory Player!, Congrats for Finishing The Game!! \n')
break;
case 2
fprintf('\n Take Your Time!!, and Remember To Have Fun!')
end
elseif K == 30 || K == 50 || K == 70 || K == 90 || K == 110
fprintf('\n Keep It Up Player!!')
M = M -1;
else
fprintf('\n %d ', K)
pause(1)
end
K=K+1;
end
fprintf('\n You Finished the Memory Game!! Excellent Work %s', name);
count = count + M;
else
fprintf('\n What A Shame!! Beleive It Or Not The Memory Game Is Super Fun');
end
case 4
clc
disp('Thank you for playing GeniusQuest!')
break;
end
end
% score display
if count == 15
disp(['HORRAY! Congratulations on acing the intelligence test with a perfect score of 15/15!', newline, 'According to GeniusQuest, YOU ARE INTELLIGENT'])
elseif count >= 13 && count < 15
disp(['Congratulations on having a high score!', newline, 'According to GeniusQuest, YOU ARE CLEVER'])
elseif count >= 10 && count < 13
disp(['Congratulations on having an above-average score!', newline, 'According to GeniusQuest, YOU ARE SUPERIOR'])
elseif count >= 7 && count < 10
disp(['You seem to be having an average score.', newline, 'According to GeniusQuest, YOU ARE MODERATE'])
elseif count >= 3 && count < 7
disp(['You seem to be having a below-average score.', newline, 'According to GeniusQuest, YOU ARE UNEXCEPTIONAL'])
else
disp(['You seem to have a poor level of intelligence.', newline, 'According to GeniusQuest, YOU NEED TO IMPROVE YOURSELF,', newline, 'FEAR NOT! Intelligence is a spectrum.', newline, 'While our tested methods may not have worked for you, we are confident that you are wonderful at other things.'])
end

Answers (1)

Divyanshu
Divyanshu on 11 Dec 2023
Hi Sarah,
I understand that you are facing the issue of counter not getting incremented in case 3 of the switch block.
The reason for such a behaviour is because of the 'break' statement you have placed after the following statement: 'fprintf('\n Excellent Memory Player!, Congrats for Finishing The Game!! \n')'
This 'break' statement takes the control out of entire while loop instead of just switch block, and hence the instructions after this 'break' are skipped and the value of count is not updated.
A workaround can be to place the statement 'count = count + M', after this statement:
'fprintf('\n Excellent Memory Player!, Congrats for Finishing The Game!! \n')'
Please refer the following documentation for further details:
" The MATLAB break statement ends execution of a for or while loop, but does not end execution of a switch statement. This behavior is different than the behavior of break and switch in C."
I hope it helps!

Categories

Find more on Just for fun 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!