Can one repeat a matlab script from the beginning?

  • Context: MATLAB 
  • Thread starter Thread starter eulergy
  • Start date Start date
  • Tags Tags
    Beginning Matlab
Click For Summary
SUMMARY

The discussion centers on restarting a MATLAB script for a craps game after a player chooses to play again. The user initially struggles with implementing a restart feature in the script. The solution involves calling the script as a function, allowing the game to restart from the beginning by replacing the final message with a function call to the script file. Additionally, splitting the game into multiple function files is suggested for more complex restart points.

PREREQUISITES
  • Basic understanding of MATLAB scripting
  • Familiarity with MATLAB functions and file structure
  • Knowledge of conditional statements in MATLAB
  • Experience with random number generation in MATLAB
NEXT STEPS
  • Learn how to create and call functions in MATLAB
  • Explore MATLAB's function file structure for better organization
  • Research MATLAB's control flow statements for advanced scripting
  • Investigate modular programming techniques in MATLAB for game development
USEFUL FOR

Students and developers working with MATLAB, particularly those interested in game development and script optimization. This discussion is beneficial for anyone looking to enhance their understanding of function calls and script management in MATLAB.

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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
12K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 11 ·
Replies
11
Views
4K