Text based game.

Newbie Spellweaver
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.
 
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
 
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:
 
Back