Help Needed - Text Adventure Game

Hi, I need a way to match a word to a string regardless of the spelling.
so a user can input a word and the program will read it and do what they asked.

1
2
3
4
5
6
7
8
//Such as
cout << "Where do you want to go?" << endl;
cin >> string;

if( string == "North" || string == "north" || string == "n" )
{ goto northRoom; }
else if( string == "South" || string == "south" || string == "s" )
{ goto southRoom; }


I started by doing that, and it worked fine, but it takes too much screen space
and time to type.

then i switched to the "switch" function

1
2
3
4
5
6
7
8
9
10
cout << "Which way would you like to go?" << endl;
cin >> string;

switch( string )
{
     case 'n' :
     {     goto northRoom; }
     case 's' :
     {     goto southRoom; }
}

and that works, but only if you input a "n" or a "s"

So I googled around a bit and discovered the find() function
and got an idea

1
2
3
4
5
6
7
8
9
10
string options = "North north n South south s";
string str;

cout << "Which way would you like to go?" << endl;
cin >> string;

found = options.find(string);
if( ( found > 0 ) && ( found < 13 ) )
{     goto northRoom; }

But it doesn't work, and I'm pretty sure i understand why, but I am looking for
a way to do something like these that will just match an input to a string,
and send you to a room if it matches the appropriate rooms direction

1
2
3
4
5
6
7
//Like
directions = "north east south west";

cin >> string;

if( string == a direction )
{ goto the room in that direction }

Also I Am trying to create an advanced combat system and it works, as far as letting the player damage the opponent, but the opponent does 0 damage.

ANY positive/productive feedback is appreciated.

Here's My Code for it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//Advanced Combat - Working. (but not complete)
void advCombat(string opponent)
{
	attacker = opponent;	// Opponents Name
	opp = level * 5;		// Opponents Level			say level 5--->	Enemy level is 25
	enemyHp = level * opp;	// Players Level * Attackers Level			and health is 125
	enemyHpMax = level * opp;	// Opponents Max Health		say level 10-->	Enemy lvl 50 and health is 500		
	enemyDmg = ( ( level / opp ) * str ) * 10;

	cout << attacker << " is level " << opp << endl << endl;
	//Attack System ( With/Without Sword )
	do{
	cout << "What now?";
	cin >> answer;
	
	if( sword == false )
	{
		srand(time(0)); int n = rand() % 10+1;
		if( n > 4 )
		{	//Essentially you do 8.56% of the Opponents health for damage
			double dmg = ( opp / 35.0 ) * str; 				// At level 5 You do (.714 * str) dmg
			enemyHp -= dmg;						// So You'd do 10.7 Damage 
			cout << "You inflicted " << dmg << " to " << attacker		// out of 125 health to the attacker
			<< endl << " has " << enemyHp << "/" << enemyHpMax << endl;	// and 107 if you have the sword
		}								// but there is a greater chance 
		else if( n < 4 )							// that you will miss if you are 
		{								// weilding the mighty sword
			cout << "Oh no! You've Missed!"; cin.ignore(); cin.get();
		}
	}
	else if( sword == true ) 
	{ 	 
		srand(time(0)); int n = rand() % 10+1;
		if( n > 6 )
		{
			double dmg = ( ( opp / 35.0 ) * str ) * 10; 
			enemyHp -= dmg;
			cout << "You dealt " << dmg << " damage to " << attacker 
			<< endl << attacker << " has " << enemyHp << "/" << enemyHpMax << endl;
		}
		else if( n < 6 )
		{
			cout << "Oh no! You've Missed!"; cin.ignore(); cin.get();
		}
	}

	cout << attacker << " retaliated."; cin.ignore(); cin.get();
	
	//Defense system
	if( cloak == true )
	{
		srand(time(0)); int n = rand() % 20+1;
		//Attacker Missed
		if( n < 4 )
		{	
			cout << endl << endl << "You button your Trench coat in a flash, and Vanish!"
			<< endl << attacker << " Missed!"; cin.ignore(); cin.get();
		}
		//Attacker Hit
		else if( n > 4 ) 
		{	srand(time(0)); int n = rand() % 1000000+1;
			//RANDOM CLOAK SHREDDING!! WOO.
			if( n > 999997 && n < 999999 ) { cout << "Oh Dear, you're Cloak has been shredded to bits" << endl; }
			enemyDmg = ( ( level / opp ) * str ) * 10;
			cout << "You were too slow, and " << attacker << " Thwakk'd you."
			<< endl << attacker << " dealt " << enemyDmg << " damage to you!" << endl;
			health -= enemyDmg;
			cout << health << "/" << healthMax << " remaining."; cin.ignore(); cin.get();
		}
	}
	else
	{	srand(time(0)); int n = rand() % 10+1;
		//Attacker Hit
		if( n < 4 ) 
		{	
			enemyDmg = ( ( level / opp ) * str ) * 10;
			health -= enemyDmg; 
			cout << "You've Been Hit! " 
			<< endl << attacker << " dealt " << enemyDmg << " damage to you!"; cin.ignore(); cin.get();
		}
		else if( n > 4 )
		{
			cout << "Hoora! " << attacker << " missed! (Let's celebrate! Champaigne For all)";
		}
	}
	if( enemyHp < 0 ) { cout << "You killed " << attacker << "!"; cin.ignore(); cin.get(); }
	}while( health > 0 && enemyHp > 0 );
}
Last edited on
closed account (D80DSL3A)
I have a solution idea for your "spelling problem".

Write a function which takes the string entered by the user for direction, makes comparisons to a list of spellings maintained in the function, and returns either 'n', 's', 'e' 'w' or 'x' (say, for unrecognized spellings).

Such a function would work well with the approach you outlined here:
1
2
3
4
5
6
7
8
9
10
cout << "Which way would you like to go?" << endl;
cin >> str;//was wrong. string=name of type;

switch( string )
{
     case 'n' :
     {     goto northRoom; }// EVIL goto. CALL a function instead, then return to here.
     case 's' :
     {     goto southRoom; }
}


Instead you would call the function, identifyEntry say, then switch on the return value, like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
cout << "Which way would you like to go?" << endl;
cin >> str;

switch( identifyEntry(str) )
{
     case 'n' :
     {     northRoomLogic();// MUCH better.
            break;
     }
     case 's' :
     {     southRoomLogic();
            break;
      }
}

Here's an idea for a function which may work for you:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char identifyEntry(string str)
{
    static string northSpellings[]  = {"north", "North", "n", "N"};
    static string southSpellings[] = {"south", "South", "s", "S"};
    static string eastSpellings[]   = {"east", "East", "e", "E"};
    static string westSpellings[]  = {"west", "West", "w", "W"};

    // assuming size = 4 for all lists. 
    // If the list lengths vary then this simple approach won't work
    int size = 4;
    int i = 0;// for looping
    for(i=0; i<size; ++i) if( str == northSpelling[i] ) return 'n';
    for(i=0; i<size; ++i) if( str == southSpelling[i] ) return 's';
    for(i=0; i<size; ++i) if( str == eastSpelling[i] ) return 'e';
    for(i=0; i<size; ++i) if( str == westSpelling[i] ) return 'w';
    return 'x';
}


As for your other issue re. enemy attacks doing no damage, more info is needed.
How do you know that no damage is done?
Do you get the output from line 66 and it says " ...dealt 0 damage to you!"?

Maybe enemyDmg = 0. Place a cout to test for this following line 8 (where the value is assigned). I can't tell what value enemyDmg would have from examining lines 5-8.
OK, thanks for the feedback.
This is my game in a nutshell.

I realized that ( level / opp ) doesn't work because the enemy's level is 5x your level. haha.

But I got the combat down.

The function you suggested tries to convert a "string" to an "int".
I don't know what to change.
closed account (D80DSL3A)
The function you suggested tries to convert a "string" to an "int".

I hope it's because you passed an integer to it when you called it.
The function takes a string as its one argument, not an integer.
Thanks For The Help. Problem Solved
Last edited on
Topic archived. No new replies allowed.