Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

Text based game.

Initiate Mage
Joined
Dec 17, 2004
Messages
47
Reaction score
0
Hi.
i'm working in visual c++ and im doing a text based game as a school project.
my problem is regarding the options the player have.

lets say each screen gives the player 3 diffrent options to choose from, how am i supposed to deal with them?
surly there is a better way then doing million "if" check every time the player choose one of those options?

in other words i need an idea for a faster way to know which option has been chosen so i could load the approprate screen.

thanks in advence.
 
Junior Spellweaver
Joined
Mar 21, 2006
Messages
129
Reaction score
0
Hi.
i'm working in visual c++ and im doing a text based game as a school project.
my problem is regarding the options the player have.

lets say each screen gives the player 3 diffrent options to choose from, how am i supposed to deal with them?
surly there is a better way then doing million "if" check every time the player choose one of those options?

in other words i need an idea for a faster way to know which option has been chosen so i could load the approprate screen.

thanks in advence.

I'm a VB6 and vb.net developer but I suppose what I'm about to propose also works for C++, just can't help you with a appropriate code.

Perhaps Case Select is an option
 
Custom Title Activated
Member
Joined
Aug 8, 2004
Messages
3,892
Reaction score
20
Write an optionhandler function for this, using a switch or if you're feeling up to it function pointers.

Its been a while since I last coded in C++, but it'd probably look like something like this:

Code:
#include <iostream>

using namespace std;

int main() {
  for(int question = 0; question < 40; question++) {
    handleOption(*showQuestion(question));
  }
  return 0;
}

int showQuestion(int question) {
  switch(question) {
    case 0:
      std::cout << "Question one!";
      break;
    case 1:
      std::cout << "Question two!";
      break;
  }
  int answer;
  cin >> answer;
  return answer;
}

void handleOption(int * answer) {
  switch(*answer) {
    case 1:
      doSomething();
      break;
    case 2:
      doSomethingElse();
      break;
  }
  return 0;
}

This is about the most basic way of doing what you want without resorting to endless if/else loops. There are prettier ways, using objects instead of functions is one of them, but they tend to require a deeper grasp of the language.

Note that the code above won't compile, see it as an indication of a possible sollution rather then the sollution itself. I have no doubt others might give you actual working examples :wink:
 
Divine Celestial
Member
Joined
Jul 7, 2004
Messages
853
Reaction score
5
1. Switch construct (illustrated above)

2. Function lookup table (illustrate it yourself, you'll understand if you've learned enough C)
 
Custom Title Activated
Member
Joined
Aug 8, 2004
Messages
3,892
Reaction score
20
Then you have the great oppertunity to start learning it now! :wink:
 
Back
Top