Archived

This topic is now archived and is closed to further replies.

Happyman2003

Some questions regarding text battle system

Recommended Posts

Hiya all A couple of questions. (1) My output shows my health and enemies health, and the options available to the player. Is there a simple way to clear the screen "before" the necessary code for the selected option has been run. For example, if you select Option 2 to cast a spell, it hasn''t been implemented yet, and I need it to clear the screen, and then inform the player that they could not cast a spell. In reality I need the screen cleared everytime. (2) I am currently using if else loops. Would it be easier, or even possible to set this battle system up using switches. switch (choice) { case 1: cout << "Enter commands for Attack enemy" break; case 2: cout <<" That option is not available" break; Is this code reasonably correct. How does a switch actually know what to use? Does that mean that if the user inputs "1" it will jump to case 1? And, am I correct in guessing that this would be a lot easier to look at and to implement? Thanks in advance Jason

Share this post


Link to post
Share on other sites
a case statement would be easier to read than a long list of ifs, not sure on if there is a performance issue there though (pro or con). It won''t just arbitrarily jump to 1, it is similar to the if statements in that it goes through and checks each condition.
case 1:
break;
case 2:
break;
is almost the same as
if(choice==1) break;
else if(choice==2) break;
I am not sure on exactly how the 2 compare once it is compiled but a switch statement is easier to read IMHO than a long if else chain.

switch( choice )
{
case 1:
cout << "Enter commands for Attack enemy"
break;
default:
cout <<" That option is not available"
break;
}

you could also use #defines to make it more readable:

// somewhere in a .h file
#define BATTLE_CHOICE_ATTACK 1
#define BATTLE_CHOICE_FLEE 2
// then in the .cpp file
switch( choice )
{
case BATTLE_CHOICE_ATTACK:
cout << "Enter commands for Attack enemy"
break;
case BATTLE_CHOICE_FLEE:
cout << "You flee the battle to save your own worthless skin."
break;
default:
cout <<" That option is not available"
break;
}

Share this post


Link to post
Share on other sites