When I press 1 the program does not repeat the process, could you please fix to
ID: 3844558 • Letter: W
Question
When I press 1 the program does not repeat the process, could you please fix to repeat the whole program.
clc;
clear all;
x = ceil(6*rand) + 1;
y = ceil(6*rand) + 1;
while(1)
fprintf('Player rolled %d + %d = %d ', x,y,(x+y))
if((x+y) == 7 || (x+y) == 11)
fprintf('%s ','Player Wins');
break;
elseif((x+y) == 2 || (x+y) == 3 || (x+y) == 12)
fprintf('%s ','Player Loses');
break;
else
%ípí for points
p = (x+y);
fprintf('The Point is %d ',p);
x = ceil(6*rand) + 1;
y = ceil(6*rand) + 1;
while(1)
fprintf('Player rolled %d + %d = %d ', x,y,(x+y));
if((x+y) == p)
fprintf('%s ','Player Wins');
break;
elseif((x+y) == 7)
fprintf('%s ','Player Loses');
break;
else
x = ceil(6*rand) + 1;
y = ceil(6*rand) + 1;
end
end
break;
end
end
%% Prompt to play again
Play_Again=input('Would you like to play again? 1 for yes, 2 for no ');
if Play_Again==1
fprintf('I do not know how to restart, sorry! ')
elseif Play_Again==2
fprintf('Thank you for playing. ')
end
Explanation / Answer
clc;
clear all;
x = ceil(6*rand) + 1;
y = ceil(6*rand) + 1;
while(1)
fprintf('Player rolled %d + %d = %d ', x,y,(x+y))
if((x+y) == 7 || (x+y) == 11)
fprintf('%s ','Player Wins');
elseif((x+y) == 2 || (x+y) == 3 || (x+y) == 12)
fprintf('%s ','Player Loses');
else
%ípí for points
p = (x+y);
fprintf('The Point is %d ',p);
x = ceil(6*rand) + 1;
y = ceil(6*rand) + 1;
while(1)
fprintf('Player rolled %d + %d = %d ', x,y,(x+y));
if((x+y) == p)
fprintf('%s ','Player Wins');
break;
elseif((x+y) == 7)
fprintf('%s ','Player Loses');
break;
else
x = ceil(6*rand) + 1;
y = ceil(6*rand) + 1;
end
end
end
%% Prompt to play again
Play_Again=input('Would you like to play again? 1 for yes, 2 for no ');
if Play_Again==1
fprintf('Playing Again ')
elseif Play_Again==2
fprintf('Thank you for playing. ')
break;
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.