A switch case...
I have this very simple game that is run by a switch case and in one of the choices I wanted there to be a little mini-game type thing that requires another switch, My qustion is, is that a alright to do? is it bad technique or habit? and if so what would you guys suggest?
...I'd say start off by making the minigame a self contained function()
and later you may want to look into Object Oriented programming styles...
and later you may want to look into Object Oriented programming styles...
For a simple game, the main loop being a switch statement isn't at all a far-fetched thing. When you get into nesting them, though, you should probably wrap up that inner switch into a function. This'll make the code infinitely easier to read and maintain, as well as prepare you for nastier coding down the road. So:
becomes:
Instead of the buy/sell menus, you'd have another function for your minigame(s).
Best of luck!
-jouley
{// main loop: switch (choice) { case 0: // buy menu cin >> buyWhat; switch (buyWhat) { case 0: // buy thing 0 break; case 1: // buy thing 1 break; // ... } break; case 1: // sell menu cin >> sellWhat; switch (sellWhat) { case 0: // sell thing 0 break; case 1: // sell thing 1 break; // ... } }}
becomes:
{// main loop switch (choice) { case 0: buyMenu( ); break; case 1: sellMenu( ); break; // ... }}void buyMenu(void){ // buy menu int buyWhat; cin >> buyWhat; switch (buyWhat) { case 0: // buy thing 0 break; case 1: // buy thing 1 break; }}void sellMenu(void){ // etc}
Instead of the buy/sell menus, you'd have another function for your minigame(s).
Best of luck!
-jouley
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement