4 Replies - 622 Views - Last Post: 12 April 2012 - 09:03 PM Rate Topic: -----

#1 bennigan88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 07-April 12

How to Make Menu Navigation More Efficient?

Posted 11 April 2012 - 04:25 PM

Ok I wrote this program to navigate among 3 castles, and go into different wings within the castle, but I was hoping that someone could direct me to a system to make this whole setup more streamlined. I feel like the way I'm doing is using way more code than is necessary. Any insight or discussion, or direction to different control structures would be well appreciated.

#include <iostream>
#include <cstdlib>

using namespace std;

int castle_menu(void);
int wing_menu(void);
int room_menu(void);

void castle_name(int choice_par);
void wing_name(int choice_par);

int room;
int wing;
int castle;





int main()
{
   bool repeat1=1;
   do
   {
       system("CLS");
       castle = castle_menu();
       system("CLS"); // at this point, a castle has been chosen

       if (castle!=4)
       {
           cout << "Welcome to the ";
           castle_name(castle);
           cout << "!" << endl << endl;
       }
       else
           break; //break out of castle loop, go up to exit

       do
       {
           system("CLS");
           wing = wing_menu();
           system("CLS");

           if (wing!=4)
           {
               cout << "Welcome to the ";
               wing_name(wing);
               cout << " of the ";
               castle_name(castle);
               cout << "!" << endl << endl;
               system("PAUSE");
           }
           else
               break; // break out of wing loop, go up to castle
        } while (wing!=4);

       cout << "You are now leaving the ";
       castle_name(castle);
       cout << endl << endl;
       system("PAUSE");

   } while (castle!=4);


   system("CLS");
   cout << "Thank you for playing." << endl;
   return 0;
}

int wing_menu(void)
{
   cout << "Please choose a wing in the ";
   castle_name(castle);
   cout << ": " << endl << endl
   << "1. East Wing" << endl << "2. North Wing" << endl << "3. West Wing" << endl
   << "4. Leave "; castle_name(castle); cout << endl << endl;
   cin >> wing;
   return (wing);
}

int castle_menu(void) // asks for castles 1-3, or 4 or escape, returns user's choice
{
   system("CLS");
   cout << "Please choose a castle: " << endl << endl
   << "1. Red Castle" << endl << "2. Blue Castle" << endl << "3. Green Castle"
   << endl << "4. Escape!" << endl << endl;
   cin >> castle;
   return (castle);
}

int room_greeting(int choicepar) // "welcome to the ___ Castle" based on int choice 1-3
{
   switch (choicepar)
       {
           case 1:
               cout << "Welcome to the Red Castle!";
               break;
           case 2:
               cout << "Welcome to the Blue Castle!";
               break;
           case 3:
               cout << "Welcome to the Green Castle!";
               break;

       }
       cout << endl << endl;
       system("PAUSE");
}

void wing_name(int choice_par)
{
   switch (choice_par)
   {
       case 1:
           cout << "East Wing";
           break;
       case 2:
           cout << "North Wing";
           break;
       case 3:
           cout << "West Wing";
           break;
   }
}

void castle_name(int choice_par) // int into castle name
{
   switch (choice_par)
   {
       case 1:
           cout << "Red Castle";
           break;
       case 2:
           cout << "Blue Castle";
           break;
       case 3:
           cout << "Green Castle";
           break;
       default:
           cout << "Castle variable does not have either 1, 2, or 3 stored.";
           break;
   }
}



Is This A Good Question/Topic? 0
  • +

Replies To: How to Make Menu Navigation More Efficient?

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 979
  • View blog
  • Posts: 3,397
  • Joined: 19-February 09

Re: How to Make Menu Navigation More Efficient?

Posted 11 April 2012 - 07:37 PM

You could separate the data from the functions to a greater degree. One way is to use arrays. The name function is more useful if it just returns a string.


#include <iostream>
#include <string>
using namespace std;

string castle_names[] = {
     "Red Castle",
     "Blue Castle",
     "Green Castle",
     "Escape"
     };


string wing_names[] = {
     "Red Wing",
     "Blue Wing",
     "Green Wing",
     "Leave Castle"
     };

const int MAXCASTLENAME = 4;
const int MAXWINGNAME = 4;


int menu(string names[], int max, string areatype)
{
  int choice;
  cout << "Please choose a " << areatype << ": " << endl << endl;

  for(int i=0; i<max; i++)
  {
    cout << i+1 << ". " << names[i] << endl;
  }

  cin >> choice;
  return (choice);
}


string name(string names[], int max, int choice)
{
  int index = choice - 1;

  if(index >= 0 && index < max)
    return names[index];

  return "error";
}


int main()
{
  int castle, wing;

  do
  {
    castle = menu(castle_names, MAXCASTLENAME, "castle");

    wing   = menu(wing_names, MAXWINGNAME, "wing");

    cout << "You chose the "
         << name(wing_names, MAXWINGNAME, wing)
         << " in the "
         << name(castle_names, MAXCASTLENAME, castle)
         << endl;
  } while (castle != 4);
  
  cin.ignore();
  cin.get();
  return(0);
}


Was This Post Helpful? 0
  • +
  • -

#3 bennigan88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 07-April 12

Re: How to Make Menu Navigation More Efficient?

Posted 11 April 2012 - 10:03 PM

I don't want the user to be able to go to another castle without first leaving a wing, like a layered system. In order to go from the west wing of the red castle, you must first leave the west wing, so you are now at the castle wing, THEN you can change to a different castle.

I like how you used a loop to print out the menus, and I def. need to get more comfortable with using strings. We haven't yet covered them in my class, but I look forward to their usefulness. I wonder how most text based RPGs do navigation? Are there many games even done in C++?
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 869
  • View blog
  • Posts: 3,996
  • Joined: 09-June 09

Re: How to Make Menu Navigation More Efficient?

Posted 12 April 2012 - 12:15 AM

I recommend creating a structure to keep track of the current state of the player. Then base which castles are avaliable depending on which wing they are in.

example
struct Castle {
	int wing;
	int color;
};

int main() {
   Castle location;



read this: http://www.cplusplus...ial/structures/

This post has been edited by jjl: 12 April 2012 - 12:15 AM

Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 979
  • View blog
  • Posts: 3,397
  • Joined: 19-February 09

Re: How to Make Menu Navigation More Efficient?

Posted 12 April 2012 - 09:03 PM

View Postbennigan88, on 12 April 2012 - 07:03 AM, said:

I wonder how most text based RPGs do navigation?


Often the user is asked the direction they wish to move - north, east, west and south (news).


View Postbennigan88, on 12 April 2012 - 07:03 AM, said:

Are there many games even done in C++?


Sometimes - "the most popular game development language is C++".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1