MATLAB Can one repeat a matlab script from the beginning?

  • Thread starter Thread starter eulergy
  • Start date Start date
  • Tags Tags
    Beginning Matlab
AI Thread Summary
The discussion centers on creating a script for a craps game that prompts the player to either quit or play again after a round. The main issue is how to restart the script if the player chooses to continue. A solution is provided where the script can be structured as a function. By calling the function again, the game restarts from the beginning. Additionally, for more complex scenarios, splitting the game into multiple function files is suggested for better organization and control over the restart point. This approach simplifies the process and enhances the script's functionality.
eulergy
Messages
3
Reaction score
0
For one of my classes, our project is to write a script that simulates the game of craps. I have no problem with the actual game, but one of the criteria is that after the player wins or loses, we are supposed to prompt them to quit or play again (this is where I'm stuck).

Is there a command or function that can repeat the script from the beginning?

Here's the script:

%% Display Name
clear;
clc;
fprintf('Welcome to the game of craps!\n\n')
%% Rules of the game
fprintf('***Rules of the Game***\n\n')
fprintf('**On the first roll, if a sum of 7 or 11 is rolled, you win the game!\n')
fprintf('**If the sum of the first roll is 2, 3, or 12, you lose!\n')
fprintf('**If the sum of the first roll is none of those, keep rolling until you match the sum of the first roll.\n')
fprintf('**However, if you roll a sum of 7 before you reach your point value, you lose!\n\n')
%% Display message to start
Start=input('Press 1 to Start!:\n\n');
%% Display first and second die values
Die1=randi(6,1);
Die2=randi(6,1);
fprintf('First die: %g\n\n',Die1)
fprintf('Second die: %g\n\n',Die2)
%% Determine Win, Loss, or Point
Roll_1=Die1+Die2;
if Roll_1==7 | Roll_1==11
fprintf('You have won the game of craps!\n\n')
elseif Roll_1==2 | Roll_1==3 | Roll_1==12
fprintf('You have lost!\n\n')
else fprintf('You now have %g points!\n\n',Roll_1)
Continue=input('Press 2 to roll again\n\n');
if Continue==2
Die3=randi(6,1);
Die4=randi(6,1);
fprintf('First die: %g\n\n',Die3)
fprintf('Second die: %g\n\n',Die4)
Roll_2=Die3+Die4;
if Roll_2==Roll_1
fprintf('You have won the game of Craps!\n\n')
elseif Roll_2==7
fprintf('You have lost!\n\n')
else
while Roll_2~=Roll_1 | Roll_2~=7
Keep_Rolling=input('Press 3 to keep rolling!\n\n');
if Keep_Rolling==3
Die4=randi(6,1);
Die5=randi(6,1);
fprintf('First die: %g\n\n',Die4)
fprintf('Second die: %g\n\n',Die5)
Rolls=Die4+Die5;
if Rolls==Roll_1
fprintf('You have won the game of Craps!\n\n')
break
elseif Rolls==7
fprintf('You lose.\n\n')
break
end
end
end
end
end
end
%% Prompt to play again
Play_Again=input('Would you like to play again? 1 for yes, 2 for no\n\n');
if Play_Again==1
fprintf('I do not know how to restart, sorry!\n\n')
elseif Play_Again==2
fprintf('Thank you for playing.\n\n')
end



The problem lies in the %%Prompt to play again section where I have
if Play_Again==1

Is there a way I could make it so that if the user presses 1 the script restarts itself?

It has really been bugging me, and I've been trying to research how to fix this but I haven't found anything definitive.

Thanks in advance.
 
Physics news on Phys.org
The m files work as functions, so all you need to do is to call that function. I.e., if you called the file game.m, just replace the fprintf('I do not know how to restart, sorry!\n\n') by game

EDIT: That will restart the script right from the beginning. If you want it to restart somewhere else, splitting the game into several function files is a clean way of doing it.
 
Thanks! Such a simple and now obvious solution haha.
 
Back
Top