Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Can one repeat a matlab script from the beginning?

  1. Mar 15, 2014 #1
    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
    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
    fprintf('First die: %g\n\n',Die1)
    fprintf('Second die: %g\n\n',Die2)
    %% Determine Win, Loss, or Point
    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
    fprintf('First die: %g\n\n',Die3)
    fprintf('Second die: %g\n\n',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')
    while Roll_2~=Roll_1 | Roll_2~=7
    Keep_Rolling=input('Press 3 to keep rolling!\n\n');
    if Keep_Rolling==3
    fprintf('First die: %g\n\n',Die4)
    fprintf('Second die: %g\n\n',Die5)
    if Rolls==Roll_1
    fprintf('You have won the game of Craps!\n\n')
    elseif Rolls==7
    fprintf('You lose.\n\n')
    %% 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')

    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.
  2. jcsd
  3. Mar 15, 2014 #2
    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.
  4. Mar 15, 2014 #3
    Thanks! Such a simple and now obvious solution haha.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook