Advertisement

is there a way to improve this code??????????

Started by February 03, 2001 09:00 AM
9 comments, last by killer333 23 years, 11 months ago
is there a way to improve this code i just dont like the way i structured it but i cant think of a better way this is the code: #include #include #include "class.hpp" #include "functions.hpp" int main() { //declare an instance of fighter named user fighter user; //set all the variables in user user.SetHp(); user.SetBaseHp(); user.SetStrength(); user.SetDefense(5,rand()%10); user.SetBaseDefense(); user.SetSpeed(); user.SetGold(); user.SetExp(); user.SetExpNeeded(10); user.SetLevel(1); user.SetDamage(10); //declare important variables int random=0; int choice=0; //this is the title screen cout <<"\t\t\tQuest For Glory\n\n\n\n\n\n\t\t"; system("PAUSE"); ClearScreen(); //first part of the game-You are told what you are for cout <<"You are awaken by a small man sitting next to your bed.\n\n\n"; cout <<"User> Who are you?\n"; cout <<"Man> My name is of no concern. You on the other hand are very important\n"; cout <<"Man> to us and our cause. If you cooperate you may actually live.\n"; cout <<"User> What am I doing here then? Tell me now and you may live!\n"; cout <<"Man> As you wish. We are part of a rebel group called the Terkels. We\n"; cout <<"Man> need your help to bring down the emperor and gain our freedom! I \n"; cout <<"Man> must leave you for now. You should go out and level up so that we\n"; cout <<"Man> may use your power properly. Good Luck!\n"; system("PAUSE"); ClearScreen(); //first set of choices in the game do { cout <<"You are in the wilderness from what you can see. Giant Oak and ceder\n"; cout <<"trees line the path to the nearest town. As you look around you can see\n"; cout <<"shadows milling about the bushes. You better watch your step lest you be\n"; cout <<"ambushed and killed!\n\n"; cout <<"What will you do?-\n"; cout <<"1)Move up along the road.\n"; cout <<"2)Check your stats.\n"; cin >> choice; switch(choice) { case 1: system("PAUSE"); break; case 2: user.ShowStats(); ClearScreen(); break; } }while(choice!=1); //the second part-user is asked if he wants to fight cout <<"As you walk down the dusty road you begin to smell what seems to be\n"; cout <<"rotten eggs and fire. The farther you walk down the road the stronger\n"; cout <<"the smell becomes. After about 5 minutes of walking you see a sly man\n"; cout <<"trying to conceal himself in the shadows. After about 10 seconds you\n"; cout <<"call him out.\n\n"; cout <<"User> You there! What are you doing hiding in the shadows?\n"; cout <<"Stranger> I am doing nothing but waiting for someone here. I think I\n"; cout <<"Stranger> was waiting for someone such as yourself...\n"; cout <<"User> What!!!!\n"; random=rand()%2; switch(random) { case 1: thief* enemy=new thief; enemy->SetHp(); enemy->SetSpeed(); enemy->SetDefense(rand()%10); cout <<"\nYou have encountered a thief enemy!\n"; do { cout <<"What will you do?-\n"; cout <<"1)Attack\n"; cout <<"2)Defend\n"; cin >> choice; switch(choice) { case 1: if(user.GetSpeed()GetSpeed()) { user.ThiefBattle(); enemy->Battle(user.GetDamage()); break; } else { enemy->Battle(user.GetDamage()); user.ThiefBattle(); break; } case 2: if(user.GetSpeed()GetSpeed()) { user.ThiefBattle(); user.SetGuard(); break; } else { user.SetGuard(); user.ThiefBattle(); break; } } }while(user.GetHp()>1 && enemy->GetHp()>1); } return 0; }
This is a text adventure right? At the moment the code takes the form of a linear script - which means that the code wil get longer and uglier as the story gets longer.

I dont know how comfortable you are with OO, but here are some suggestions:


1. Most text adventures consist of a number of different "rooms" - this is will make place to start. Your CRoom class might look something like this:

      class CRoom{protected:  CRoom* northExit;  CRoom* eastExit;  CRoom* southExit;  CRoom* westExit;public:  virtual void PrintDescription();  virtual CRoom* ChooseDirection();}  


Of course, empty rooms arent very interesting, so you can derive more interesting rooms from this base class - adding CMonster classes and CObject classes to represent bad guys and useful objects. Once you have done that, you can build up your map by simply connecting the room pointers together. You can then define your player something like this...

  class CPlayer{  private:     CRoom* currentRoom;    int health;    // etc...  public:    CPlayer(CRoom* start)    {       currentRoom = start;    }    void Explore()    {      CRoom* nextRoom;      currentRoom->PrintDescription();      nextRoom = currentRoom->ChooseDirection();      if(nextRoom != NULL)      {         // the player picked a valid direction, so         // move him into the new room.         currentRoom = nextRoom;       }       // if the player didnt pick a valid direction,        // leave him where he is.    }}      


now your main loop will look something like...

        CPlayer player(startRoom);while(1){  player->Explore();}  




Edited by - Sandman on February 3, 2001 10:48:59 AM
Advertisement
dude you are so freakin great
that was just wat i was lookin for
i luv u
lol

im gonna spend some time disecting the code u put up
do u think u can put up some more?
please

thanx
Ah, yet another has seen the virtual light

Magmai Kai Holmlor
- The disgruntled & disillusioned
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Well, I dont know how much more code you want, but ill write up a sample room using the interface i wrote earlier (note that this interface isnt complete, you will need additional functions for fighting/talking to any creatures that you see in the room, picking up objects etc - but ill leave that to you....

    class CGoblinTunnel : public CRoom{private:   CMonster* goblin;public:   void PrintDescription()   {     cout << "You notice the tunnel widening ahead. As you\n";     cout << "approach, you smell something unusual up ahead...";     if(goblin)     {       cout << "peering through the darkness, you see a goblin\n";       cout << "lurking in the shadows."     }     else     {       // the player has killed the goblin, obviously.       cout << "As you creep forward, you stumble over something.\n";       cout << "looking down with horror, you see the \n";             cout << "dismembered corpse of a goblin.\n" ;     }     if(northExit)        cout << "A tunnel leads to the north.\n";     if(eastExit)        cout << "A tunnel leads to the east.\n";     if(southExit)        cout << "A tunnel leads to the south.\n";     if(westExit)        cout << "A tunnel leads to the west.\n";  }  CRoom* ChooseDirection()  {     char dir;     cin >> dir;     switch(tolower(dir))     {     case 'n':       if(northExit)         return northExit;       break;     case 'e':       if(eastExit)         return eastExit;       break;     case 's':       if(southExit)          return southExit;       break;     case 'w':       if(westExit)           return westExit;       break;     }     // if we got here, the user either typed a silly character     // or tried to follow a route which doesnt exist.     cout << "You cannot go that way\n";     return NULL;  }}    


You can then have as many tunnels with goblins in as you like, you just create a CGoblinTunnel and link it to the desired point of your map.

This can obviously be improved a lot, I'd recommend thinking about all the different things that you might want to do in a room, implement these as functions in the CRoom class, also think about how the player actually interacts with the room etc....

As for building the map, you could do a simple text file map building routine to load the map in. lets say you assign the character 'A' to be the start, 'G' to be a CGoblinTunnel, 'T' to be a CEmptyTunnel, 'S' to be a goblin stronghold, and perhaps 'X' to be a treasure room, you can then make a simple map in notepad in about 10 seconds:

          ATTTTTTTTGTT        T     TTGTGTTT     GTTGTTT     T   T        T  TGTTTTG   TG   GX        TGTT     T    TGTGSG                 TTTGTT     



Edited by - Sandman on February 3, 2001 3:28:27 PM
I would move all of the data to seperate source files. An example would be placing all the CRooms in a seperate source file. As the number of rooms grows you might want to group rooms into areas and place each area in it''s own file. If you wanted to reuse the code for another adventure you could then compile those data files into a dll where a differant adventure would use a differant dll.
Keys to success: Ability, ambition and opportunity.
Advertisement
what does the virtual word do?
it tells the prog that the function will become the base function for another class
i think....

for sandman:

i tried ure code out and it aint work
now im super confused
i was never good with these type of things
i was always better programming in windows...
well, if you just cut and pasted it, it wouldnt work - it was never intended to be complete - things like constructors and destructors havent been defined properly etc......

I meant to give an idea on how to structure it, not write the game for you
i know
i fidled wit it and did some stuff
i cant get a workin version
DAMN!!!!

This topic is closed to new replies.

Advertisement