I am having trouble with a problem I am working on. The code works until I get to the next step which is a menu asking if the player wants to play the same game again. I can't seem to get it to loop back to the correct spot.

|i=1 while i>=1; choice = menu('Do you want to play a game?: ','yes','no') if choice == 2 error('Program will terminate'); elseif choice ==1 games = menu('Select a Game','Dunko','Bouncer','Munchies') end
if games ==1
difficulty = menu('Select desired level of difficulty','Easy','Moderate','Hard')
elseif games ==2
difficulty = menu('Select desired level of difficulty','Easy','Moderate','Hard')
elseif games ==3
difficulty = menu('Select desired level of difficulty','Easy','Moderate','Hard')
end
if games == 1 && difficulty == 1
Dunko = Dunko(1)
elseif games == 1 && difficulty == 2
Dunko = Dunko(2)
elseif games == 1 && difficulty == 3
Dunko = Dunko(3)
elseif games == 2 && difficulty == 1
Bouncer = Bouncer(1)
elseif games == 2 && difficulty == 2
Bouncer = Bouncer(2)
elseif games == 2 && difficulty == 3
Bouncer = Bouncer(3)
elseif games == 3 && difficulty == 1
Munchies = Munchies(1)
elseif games == 3 && difficulty == 2
Munchies = Munchies(2)
elseif games == 3 && difficulty == 3
Munchies = Munchies(3)
end
fprintf = ('Thank you for playing')
replay = menu('Do you wish to repeat the game?','yes','no');
if replay == 1
???????????
end
fprintf = ('Thanks for playing')|

5 comentarios

That if-else statement would surely be a lot simpler if you just did:
if games == 1
Dunko( difficulty );
elseif games == 2
Bouncer( difficulty );
else
Munchies( difficulty );
end
to get rid of the needless extra cases.
Equally there is no need for the first series of if statements since every case does the same thing. Just put
difficulty = menu('Select desired level of difficulty','Easy','Moderate','Hard')
without an if statement if there is no difference in action whichever game you choose.
I'm also not sure what you are doing with a line like:
Bouncer = Bouncer(2)
If Bouncer is a function as I assume it is you certainly don't want to assign the output of this to a variable of the same name that will subsequently over-write the function (which could potentially cause the solution in my answer below to not work - I haven't tested it).
Maybe I should include the problem so you could see why I did those codes.
You have written three functions for three different games. The names of the games (and the functions that implement them) are Dunko, Bouncer, and Munchies. Each function will accept an integer between one and three indicating the level of difficulty (1 = easy, 3 = hard), and when play is complete will return a text string, either "won" or "lost", to the program that executed the function. Write a program that will use a menu to ask the user if he or she wants to play a game. If not, the program should terminate. If so, the program should generate another menu to allow the user to select one of the three games by name. After selecting a game, the program should display another menu asking the user for the desired level of difficulty (Easy, Moderate, or Hard), and the game should begin by calling the appropriate function. When the user has finished playing the selected game, a message should be displayed indicating whether the user won or lost the game. A menu should then be generated asking if the user wished to repeat the game just played. (THIS IS WHERE I'M STUCK). If so, the difficulty level menu should be displayed again and the game repeated. If the user does not wish to repeat the same game, the program should display a menu asking if the user wants to play another game. If so, the game selection menu should be generated again, followed by level selection, etc. If the user does not wish to play another game, the program should display a message saying "Thanks for playing" and terminate.
Can you format the top part of your code in the same way as the rest please. It is difficult to see the proper code flow as it is currently.
You probably want a
while true
external loop though. Yours is using i >= 1, but for the code you have shown you never increment i and unless you are going to use i in some way there is no need to have it there for what is effectively an infinite loop until a break condition is met.
i=1
while i>=1;
choice = menu('Do you want to play a game?: ','yes','no')
if choice == 2
error('Program will terminate');
elseif choice ==1
games = menu('Select a Game','Dunko','Bouncer','Munchies')
end
if games ==1
difficulty = menu('Select desired level of difficulty','Easy','Moderate','Hard')
elseif games ==2
difficulty = menu('Select desired level of difficulty','Easy','Moderate','Hard')
elseif games ==3
difficulty = menu('Select desired level of difficulty','Easy','Moderate','Hard')
end
while true
if games == 1 && difficulty == 1
Dunko = Dunko(1)
elseif games == 1 && difficulty == 2
Dunko = Dunko(2)
elseif games == 1 && difficulty == 3
Dunko = Dunko(3)
elseif games == 2 && difficulty == 1
Bouncer = Bouncer(1)
elseif games == 2 && difficulty == 2
Bouncer = Bouncer(2)
elseif games == 2 && difficulty == 3
Bouncer = Bouncer(3)
elseif games == 3 && difficulty == 1
Munchies = Munchies(1)
elseif games == 3 && difficulty == 2
Munchies = Munchies(2)
elseif games == 3 && difficulty == 3
Munchies = Munchies(3)
end
fprintf = ('Thank you for playing')
replay = menu('Do you wish to repeat the game?','yes','no');
if replay ==0
break;
end
end
end fprintf = ('Thanks for playing')

Iniciar sesión para comentar.

Respuestas (1)

Adam
Adam el 12 de Abr. de 2016
Editada: Adam el 12 de Abr. de 2016
You should just be able to do something like:
while true
difficulty = menu('Select desired level of difficulty','Easy','Moderate','Hard')
if games == 1 && difficulty == 1
...
end
fprintf = ('Thank you for playing')
replay = menu('Do you wish to repeat the game?','yes','no');
if replay == 0
break;
end
end
end
editing your existing code in the obvious places to add the new components. This is assuming you mean you want to play the same game and difficulty on answering yes to the question.

2 comentarios

Adam,
I added the code above and the menu pops up but no matter if you press yes or no it asks the same "Do you wish to repeat the game?". If you press yes (1) it should go to the difficulty menu. If you press no (2) it should ask the first menu "choice".
Thank you so much for your help with this
I edited my answer. The first part should be handled just by moving the difficulty menu call inside this inner while loop as my edited answer shows.
If the user chooses 'No' then this inner while loop exits and you are returned to the outer while loop which will do whatever it does next.

Iniciar sesión para comentar.

Categorías

Más información sobre Video games en Centro de ayuda y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Preguntada:

el 12 de Abr. de 2016

Comentada:

el 12 de Abr. de 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by