Thread: text game
View Single Post
  #5 (permalink)  
Old October 30th, 2006, 09:00 AM
Geo121 Geo121 is offline
Friend of Wrox
 
Join Date: Jan 2006
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Geo121
Default

after a few changes your overall code should look like this :




#include <iostream>
#include <stdlib.h>

using namespace std;

class thisroom{
        public:
            string description; //description of the room

            string act;
            int action; //variable that determines whether an action can take place in the room

            //room pointers
            thisroom *north;
            thisroom *south;
            thisroom *east;
            thisroom *west;

            void actiondesc(string);

};

int main(int argc, const char *argv[]){
    string direction;
    int i;


    thisroom dungeon[6];

    thisroom *saveroom;
    thisroom *curroom;

    //sets descriptions
    dungeon[0].description = "center room";
    dungeon[1].description = "north room";
    dungeon[2].description = "west room";
    dungeon[3].description = "south room";
    dungeon[4].description = "east room";
    dungeon[5].description = "really north room";


    //sets default pointers to NULL
    for(i = 0; i < 6; i++){
        dungeon[i].east = NULL;
        dungeon[i].west = NULL;
        dungeon[i].north = NULL;
        dungeon[i].south = NULL;
    }

    //sets correct pointer values
    dungeon[0].north = &dungeon[1];
    dungeon[0].west = &dungeon[2];
    dungeon[0].south = &dungeon[3];
    dungeon[0].east = &dungeon[4];

    dungeon[1].south = &dungeon[0];
    dungeon[1].north = &dungeon[5];

    dungeon[2].east = &dungeon[0];

    dungeon[3].north = &dungeon[0];

    dungeon[4].west = &dungeon[0];

    dungeon[5].south = &dungeon[1];


    //sets action capabilities
    for(i = 0; i < 6; i++){
        dungeon[i].action = NULL;
    }
    dungeon[5].action = 1;

    dungeon[5].act = "sniff";

    curroom = &dungeon[0];

    while(direction != "bye"){
        cout << curroom->description <<endl; //echoes the room description
        cin >>direction; //gets direction

        saveroom = curroom; //in case we need to call the room again

        if(curroom->action == 1){
            if(direction == curroom->act){
                curroom->actiondesc(curroom->act);
            }
            else{
                cout <<"You cannot doeth that." <<endl;
            }
        }

        if(direction == "west"){
            curroom = curroom->west;
        }
        else if(direction == "north"){
            curroom = curroom->north;
        }
        else if(direction == "east"){
            curroom = curroom->east;
        }
        else if(direction == "south"){
            curroom = curroom->south;
        }
        if(curroom == NULL){
            cout <<"You cannot goeth that way." <<endl;
            curroom = saveroom; //sets curoom to the room you were last in
        }
    }

    return 0;
}

void thisroom::actiondesc(string act)
{
     if (act == "sniff")
     {
          cout << "You sniff the fould air" << endl;
     }
}


 ~ Geo
Reply With Quote