Help again please. :)

K.I.L.E.R

Retarded moron
Veteran
This time I am making a text adventure game. :)
The problem I'm having is that I choose to look at my user statistics but I want to go back to the last point I chose in the play() function.
Thanks
Code:
/*
NAME: Reflections on Reflections
AUTHOR: Krunoslav Saho
DATE STARTED: 7/28/2003
*/
#include <iostream>
#include <cstdlib>
#include <string>
#include <iomanip>
using namespace std;
/////////////////////////////////////////////////////////////////////////////
struct player_properties
{
	string szName;
	int health;
	int maxHealth;
	int strength;
	int dexterity;
}pcName, pcHealth, pcMaxHealth, pcStrength, pcDexterity;
/////////////////////////////////////////////////////////////////////////////
struct pcAction
{
	int action;
}pcOption;
/////////////////////////////////////////////////////////////////////////////
void play();
void story();
void name();
void stats();
void inventory();
/////////////////////////////////////////////////////////////////////////////
int main()
{
	int option=0;

	cout << "1. Play Game" << endl;
	cout << "Enter the corresponding number to continue: ";
	cin >> option;

	if(option == 1)
	{
		name();
	}

	system("Pause");
	return 0;
}
/////////////////////////////////////////////////////////////////////////////
void play()
{
	cout << "It is a dark and stormy night, the wind howls on the trees" << endl 
		 << "like the cry of a banshee. However, something doesn't feel right..." << endl
		 << "You walk over to the window and stare at the window." << endl
		 << "you notice there is a reflection in it, you notice a smell of rum." << endl;
	cout << "What will you do?" << endl;
	cout << "1. Equip Items" << endl;
	cout << "2. Discriminate" << endl;
	cout << "3. Look at player statistics" << endl << "Select Option: ";
	cin >> pcOption.action;

	switch(pcOption.action)
	{
	case 1:
		inventory();
		break;
	case 2:
		cout << "You are in the living room of your mansion." << endl 
			 << "The room reaks of rum. You feel a presence in the kitchen." << endl;
		break;
	case 3:
		stats();
		break;
	}
}
/////////////////////////////////////////////////////////////////////////////
void name()
{
	cout << "Please enter your name: ";
	cin >> pcName.szName;
	cout << "Welcome " << pcName.szName << endl;

	play();
}
/////////////////////////////////////////////////////////////////////////////
void story()
{
}
/////////////////////////////////////////////////////////////////////////////
void stats()
{
	pcHealth.health = 10;
	pcMaxHealth.maxHealth = 10;
	pcStrength.strength = 7;
	pcDexterity.dexterity = 4;

	cout << "Name: " << pcName.szName << endl;
	cout << "Health: " << pcHealth.health << "/" << pcMaxHealth.maxHealth <<endl;
	cout << "Strength: " << pcStrength.strength << endl;
	cout << "Dexterity: " << pcDexterity.dexterity << endl;
}
/////////////////////////////////////////////////////////////////////////////
void inventory()
{
}
/////////////////////////////////////////////////////////////////////////////
 
I fail to understand the problem.
Could you try to make another explanation about what works, what doesn't and how you would want it to work ?
 
Code:
   switch(pcOption.action)
   {
   case 1:
      inventory();
      break;
   case 2:
      cout << "You are in the living room of your mansion." << endl
          << "The room reaks of rum. You feel a presence in the kitchen." << endl;
      break;
   case 3:
      stats();
      break;
   }
}
case 3:
stats();
break;
will go to the stats function but I won't be able to come back from the stats function to this place in the play function:

Code:
  cout << "What will you do?" << endl;
   cout << "1. Equip Items" << endl;
   cout << "2. Discriminate" << endl;
   cout << "3. Look at player statistics" << endl << "Select Option: ";
   cin >> pcOption.action;
 
Try a do-while loop, something like:
Code:
void play()
{
   cout << "It is a dark and stormy night, the wind howls on the trees" << endl
       << "like the cry of a banshee. However, something doesn't feel right..." << endl
       << "You walk over to the window and stare at the window." << endl
       << "you notice there is a reflection in it, you notice a smell of rum." << endl;
   do {
      cout << "What will you do?" << endl;
      cout << "1. Equip Items" << endl;
      cout << "2. Discriminate" << endl;
      cout << "3. Look at player statistics" << endl << "Select Option: ";
      cin >> pcOption.action;

      switch(pcOption.action)
      {
      case 1:
         inventory();
         break;
      case 2:
         cout << "You are in the living room of your mansion." << endl
             << "The room reaks of rum. You feel a presence in the kitchen." << endl;
         break;
      case 3:
         stats();
         break;
      }
   } while( pcOption.action < 4 /* or whatever condition you might want here */ );   
}
 
WOAH! Thanks Arjan de lumens.

I'm still trying to comprehend how that works. Why does it bring me back into the play() function if I have gone out of it?

/me has some logic to comprehend.
 
Shouldn't you read a good book about C/C++ rather than toying with the language to see what you can get ?

I suppose programming is rather easy to me cause I practice it since I'm 12.
 
Don't worry. I figured it out.

Experience is what makes a good programmer, not good books. ;)
I am taking a 2 year software development course.

Code:
/*
NAME: Reflections on Reflections
AUTHOR: Krunoslav Saho
DATE STARTED: 7/28/2003
*/
#include <iostream>
#include <cstdlib>
#include <string>
#include <iomanip>
using namespace std;
/////////////////////////////////////////////////////////////////////////////
struct player_properties
{
	string szName;
	int attack;
	int health;
	int maxHealth;
	int strength;
	int dexterity;
	int defense;
}pcName, pcStats, pcAttack;
/////////////////////////////////////////////////////////////////////////////
struct pcAction
{
	int action;
//EXTRA SIDE CALCULATIONS IE: TO HIT
	int toHit;
	int round;
//ENEMY CALCS
	int npcAttack;
}pcOption, battleRound;
/////////////////////////////////////////////////////////////////////////////
struct npcEnemies
{
};
/////////////////////////////////////////////////////////////////////////////
struct npcFriends
{
	string archadon;
	int attack;
	int health;
	int maxHealth;
	int strength;
	int dexterity;
	int defense;
}npcArchadon, npcStats, npcOption;
/////////////////////////////////////////////////////////////////////////////
void play();
void story();
void name();
void stats();
void inventory();
void bossBattle();
/////////////////////////////////////////////////////////////////////////////
int main() //MENU
{
	int option=0;
	pcStats.health = 10;
	pcStats.maxHealth = 10;
	pcStats.strength = 7;
	pcStats.dexterity = 4;

	cout << "1. Play Game" << endl;
	cout << "Enter the corresponding number to continue: ";
	cin >> option;

	switch(option)
	{
	case 1:
		name();
		break;
	case 2:
		system("CLS");
		cout << "EXITING" << endl;
		break;
	default:
		cout << "Invalid Entry, please press key '1' or '2'." << endl;
		system("PAUSE");
		system("CLS");
		main();
		break;
	}

	system("Pause");
	return 0;
}
/////////////////////////////////////////////////////////////////////////////
void play() //THE GAME PLAY
{
	system("CLS");
	do{
		cout << endl;
		cout << "It is a dark and stormy night, the wind howls on the trees" << endl 
			 << "like the cry of a banshee. However, something doesn't feel right..." << endl
			 << "You walk over to the window and stare at the window." << endl
			 << "you notice there is a reflection in it, you notice a smell of rum." << endl << endl;

			cout << "What will you do?" << endl;
			cout << "1. Move on" << endl;
			cout << "2. Look at player statistics" << endl << "Select Option: ";
			cin >> pcOption.action;
			cout << endl;

			switch(pcOption.action)
			{
			case 1:
				break;
			case 2:
				stats();
				break;
			}
	} 
	while ( pcOption.action == 2 );

	do{
		system("CLS");

		cout << "You slowly walk over to the kitchen, you here a loud noise." << endl
		<< "CLAP! Like a blinding flash of thunder drops down a man with eight legs" << endl
		<< "What is it you want? " << pcName.szName << " shrieks." << endl
		<< "My name is Archadon. I am looking for a warrior called " << pcName.szName << ". " << endl
		<< "My name is " << pcName.szName << ". What is it you want from me Archadon?" << endl
		<< "Before I can believe you I need to make sure you truly are " << endl
		<< "the warrior called " << pcName.szName << endl << endl;

			cout << "What will you do?" << endl;
			cout << "1. Move on" << endl;
			cout << "2. Look at player statistics" << endl << "Select Option: ";
			cin >> pcOption.action;
			cout << endl;

		switch(pcOption.action)
		{
		case 1:
			bossBattle();
			break;
		case 2:
			stats();
			break;
		default:
			break;
		}
	}
	while(pcOption.action == 1);
}
/////////////////////////////////////////////////////////////////////////////
void name() //CHARACTERS NAME CHOICE
{
	cout << "Please enter your name: ";
	cin >> pcName.szName;
	cout << endl << "Welcome " << pcName.szName << endl << endl;
	system("Pause");
	play();
}
/////////////////////////////////////////////////////////////////////////////
void story()
{
}
/////////////////////////////////////////////////////////////////////////////
void stats() //PLAYER STATS
{
	system("CLS");
	cout << "Player Statistics" << endl << endl;

	cout << "Name: " << pcName.szName << endl;
	cout << "Health: " << pcStats.health << "/" << pcStats.maxHealth <<endl;
	cout << "Strength: " << pcStats.strength << endl;
	cout << "Dexterity: " << pcStats.dexterity << endl << endl;
}
/////////////////////////////////////////////////////////////////////////////
void inventory()
{
}
/////////////////////////////////////////////////////////////////////////////
void bossBattle()
{
	system("CLS");
	npcArchadon.archadon = "Archadon";

	int roundCount=0;

	npcStats.maxHealth = 15;
	npcStats.health = 15;

	npcStats.strength = 4;

	npcStats.defense = 5;

			cout << "Battling " << npcArchadon.archadon << endl << endl;
			cout << "Battle Options:" << endl
			<< "1. Attack" << endl
			<< "2. Use Inventory Items" << endl
			<< "HP:" << pcStats.maxHealth << "/" << pcStats.health << endl
			<< "ENEMY HP: " << npcStats.maxHealth << "/" << npcStats.health << endl
			<< "ROUND: " << roundCount << endl << endl;
				cout << "ENTER: ";
				cin >> pcOption.action;

		while(pcStats.maxHealth > 0 && npcStats.maxHealth > 0 && pcOption.action == 1)
		{
			pcOption.round = 0;

			while(pcOption.round == 0 && pcStats.maxHealth > 0)
			{
				pcAttack.attack = pcStats.strength;
				npcStats.maxHealth = npcStats.maxHealth - (pcAttack.attack-npcStats.defense);
				pcOption.round++;
				system("CLS");
			}

			while(pcOption.round == 1 && npcStats.maxHealth > 0)
			{
				npcOption.attack = npcStats.strength;
				pcStats.maxHealth = pcStats.maxHealth - (npcOption.attack-pcStats.defense);
				pcOption.round--;
				system("CLS");
			}

			roundCount++;

			//if(npcStats.maxHealth > 0 || pcStats.maxHealth > 0)

			cout << "Battling " << npcArchadon.archadon << endl << endl;
			cout << "Battle Options:" << endl
			<< "1. Attack" << endl
			<< "2. Use Inventory Items" << endl
			<< "HP:" << pcStats.maxHealth << "/" << pcStats.health << endl
			<< "ENEMY HP: " << npcStats.maxHealth << "/" << npcStats.health << endl
			<< "ROUND: " << roundCount << endl << endl;
				cout << "ENTER: ";
				cin >> pcOption.action;
		}
			if(pcStats.maxHealth <= 0)
			{
				system("CLS");
				cout << "You have died" << endl;
				system("pause");
				system("CLS");
				main();
			}
		system("pause");
}
 
Argh!
You'd definitely be better off with a good book than writing random programs and see how it fails. You need the basics before you can try to work on the experience on your own.

Btw, you didn't figure it out completely. Why are you calling "main" in "main"?
 
DOH! Old code. :)

I was trying to set up flags so I can come back to a certain point in another function. Didn't know how so I had done some funny things.

Anyway I'm having fun with what I'm doing. :)
I have 3 books on C++ plus many tutorials. Not that I enjoy reading them, I like to get my hands dirty. :D
 
Arjan de lumens, I have found an even better way to continue without using do/while or whatnot. :)

I'm getting better. YAY!

I've been practicing making battle systems and other games and such. Just doing the framework.
I've been able to make random names and HP and stats for monsters and such.

Now all I need is some real art work and knowledge on DirectX or Gl. :LOL:
 
Instead of constructing your game in the code like this, you really want to be designing it as a state machine which can then be executed by your program.

I.e. some text file which describes each room/area, with the actions which trigger moves to the next area. You can attach a probability of encountering someone to fight, or finding some kind of treasure.

You code would then parse this and construct the game as a number of nodes which can all be connected to each other, and the game engine simply traverses these as they player dictates.


CC
 
Hey, you're right. I had planned that but first I wanted to be good enough to make a simple text adventure game. :)

So far I have a different function for every little thing possible.

My play() function has been significantly shortened by using OOP. :)

Code:
void play()
{
	storyInteraction();

	cout << "story" << endl;

	storyInteraction();

	cout << "story" << endl;

	storyInteraction();

	cout << "story" << endl;

	storyInteraction();

	cout << "story" << endl;

	storyInteraction();

	cout << "story" << endl;
}

Story interaction is heavily filled with other functions which in turn are filled with other functions. :)
 
YAY!

I'm proud of making a text game.

http://members.optusnet.com.au/ksaho/Designs/game.zip

The source and exe are included. If anyone has anything to add or show me what I could do better and the like then please go ahead.

If you want to play with the source make sure you have a Microsoft C++ compiler if not then you can port it to another compiler by just changing void main(void) into int main(void) and make sure a return value is specificed.

I can't think of a story so maybe one of you guys can make one and just follow the same format under the play function as I have to maintain consistency unless you have a better way to do it.

What this game demonstrates:

Basic battle system

Random Battles

Random Monster statistics (2 monsters total)

Basic Inventory

Basic use of potions

Very Basic GUI

ALL CONSOLE!!!

The console is a win32 console environment so all you guys with 486s and whatnot will not be able to run it under DOS.

My first game. :LOL:
 
Back
Top