# Can one repeat a matlab script from the beginning?

• MATLAB
• eulergy
In summary: I appreciate the help!In summary, the conversation discusses a project involving writing a script for a game of craps. The main concern is how to restart the script after the player has won or lost. The solution is to call the function file to restart the game from the beginning. Splitting the game into multiple function files is also suggested as a clean way to restart at a specific point.
eulergy
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.

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.

## Q1: Can I repeat a Matlab script from the beginning?

Yes, you can repeat a Matlab script from the beginning by using the "clear all" command to clear all variables and functions from the workspace, and then running the script again.

## Q2: How can I restart a Matlab script?

To restart a Matlab script, you can use the "clear all" command to clear all variables and functions from the workspace, and then run the script again.

## Q3: Is there a way to automatically repeat a Matlab script?

Yes, you can use the "while" loop or the "for" loop to automatically repeat a Matlab script multiple times.

## Q4: Can I modify a Matlab script and then repeat it?

Yes, you can modify a Matlab script and then repeat it by saving the modified script and running it again.

## Q5: Can I repeat a specific section of a Matlab script?

Yes, you can use the "while" loop or the "for" loop to repeat a specific section of a Matlab script multiple times.

• MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
• Engineering and Comp Sci Homework Help
Replies
5
Views
3K
• MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
11K
• Engineering and Comp Sci Homework Help
Replies
4
Views
3K
• Precalculus Mathematics Homework Help
Replies
11
Views
2K
• Programming and Computer Science
Replies
1
Views
4K
• Set Theory, Logic, Probability, Statistics
Replies
5
Views
2K
• General Math
Replies
4
Views
971
• Math POTW for University Students
Replies
1
Views
2K