Subject: text game
Posted By: 132591 Post Date: 10/27/2006 6:17:35 PM
what is wrong with this code?

class.h:
#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();

};

main.cpp:
#include <iostream>
#include <stdlib.h>
//room class
#include "class.h" 

using namespace std;


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";
    dungeon[5].actiondesc(){
        cout <<"You sniff the fould air" <<endl;
    }
    
    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();
            }
            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;
}



"Judge a man by his questions, not by his answers."
-Voltaire
\
Reply By: Geo121 Reply Date: 10/30/2006 7:57:15 AM
Wow . . .

I am really impressed with this idea.

I never thought of having a map like scenario like this.

If you don't mind I think I might use it sometime.

Anyways I found your problem it's very small but it is serious

okay are you ready?

the function actiondesc() in your class thisroom is a good idea except because it is a class you can't declare multiple abstract functions for the values of the dungeon array

UNDERSTAND??

you had

dungeon[5].actiondesc(){
    cout <<"You sniff the fould air" <<endl;
}

now there are two problems with these lines of code

first you are trying to declare a function inside of a function that is just bad programming practice

second you can't create seperately declared functions for each value of the array

instead you should declare the function outside of the main function and it should be similar if not exactly like this

 ~ Geo
Reply By: Geo121 Reply Date: 10/30/2006 7:58:07 AM
void thisroom::actiondesc(string act)
{
     if (act == "sniff")
     {
          cout << "You sniff the fould air" << endl;
     }
}

 ~ Geo
Reply By: Geo121 Reply Date: 10/30/2006 7:59:49 AM
and then change your function declaration inside of your class to

void actiondesc(string);

 ~ Geo
Reply By: Geo121 Reply Date: 10/30/2006 8:00:24 AM
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 By: Geo121 Reply Date: 10/30/2006 8:02:49 AM
Don't worry about the compiler warning if you get one because thats just telling you that NULL isn't an int

 ~ Geo
Reply By: Geo121 Reply Date: 10/30/2006 8:05:43 AM
I am very impressed with this idea

I would like for you to inform of this games completion and see if you could send me the executable or the source and I will compile it

I am also warmed inside by your wanting to use polymorphic ideas

especially after this post some guy with a masters in computer science stated that he didn't undertand polymorphism.

I was very upset after that seeeing how I'm only 17 and haven't even been to college and he has his masters

but you brightened my day and I posted a link to this post for him so he could see how you were doing

=P

 ~ Geo
Reply By: Geo121 Reply Date: 10/30/2006 8:09:34 AM
P.S.

I just thought of this . . .

A good idea for this map like system would be to create a two dimensional array apply letter / number mapping coordinates to it and then have attributes like weather and surroundings and walkable/flyable/swimmable and stuff to create an extremely detailed and interactive text based enviroment

I really wanna see it when you are done

=P

 ~ Geo
Reply By: 132591 Reply Date: 11/17/2006 9:00:52 PM
I will definitely get working on that game. If you want to email me, click http://www.redeyegamez.com/contactus.html

"Judge a man by his questions, not by his answers."
-Voltaire
\
Reply By: 132591 Reply Date: 11/18/2006 2:02:49 PM
Also I need help with something else. How would I keep track of a person's points and kills? I cannot simly do this

class thisroom{
    public:
        int killcount
};
void thisroom::actionsdesc(string act){
    if(act=="kill monster"){
        killcount++;
    }
}
cout <<thisroom::killcount <<endl;


also, how do I set actions that can only take place a fixed amount of times?

"Judge a man by his questions, not by his answers."
-Voltaire
\
Reply By: Geo121 Reply Date: 11/20/2006 7:16:20 AM
That could work but . . .

I would primarily create another class called character where you would store all of the stats

becouse there is an issue with storing that variable in the room

when you move to another room that room will have a different number of kills

technically you could just add all of the values using a for statement but it would be better practice

and easier to access and program along with organize the save feature that you add later =P

You could throw in stuff like skills into the character that can be obtained by entering a certain room

or store attributes at the start of a new game

also I would give rooms the flyable/swimmable/walkable vars to make it interesting

also before you get to far in programming i would take the code in your main function and

put it in another function so the main function will be neater and easy to understand the program flow

and then add a menu to the begining when you are done

 ~ Geo

 ~ You are unique, just like everyone else
Reply By: Geo121 Reply Date: 11/20/2006 9:31:58 AM
another good idea is to add a riddle to each room and a clue to each room

make it like a dungeon where they are trapped trying to escape

there are clues everywhere that are related to the riddles

doing a certain action might cause you to discover a clue

the clue can help you find weapons and new skills and also help solve the riddle

there will be like 20 small riddles that when solved get you a skill or weapon

the answers to like 5 will create a clue to a major riddle and there re 4 major riddles

the 4 major riddles will give you a clue on how to get out

 ~ Geo

 ~ You are unique, just like everyone else
Reply By: Geo121 Reply Date: 11/20/2006 9:33:28 AM
give it a cool story like you were doing research at work then the building like got possessed or something

then out of nowhere everyone turned into monsters

You must get out alive and then like shut down

pretty much a resident evil mock up

only more rpg related

 ~ Geo

 ~ You are unique, just like everyone else
Reply By: 132591 Reply Date: 11/20/2006 10:48:46 AM
thanks geo, you have been really helpful. I will take those ideas into consideration, and you are definitely right, I will move much more code into other functions.

"Judge a man by his questions, not by his answers."
-Voltaire
\
Reply By: 132591 Reply Date: 11/20/2006 7:12:15 PM
I just have a question. Say I create another class that carries the killcount and points integers, how do I add to those in the function containing the if statements telling what to do when an action comes up?

"Judge a man by his questions, not by his answers."
-Voltaire
\
Reply By: Geo121 Reply Date: 11/21/2006 7:23:12 AM
Well . . .

To follow those exact steps you would create the class

declare a pointer of it in the main function

send it's variables into the function and then when the function modifies the value of the pointer

it actually modifies the number globally

NOW ON THE OTHER HAND

this does work but is very sloppy

what I would do is add the function to the class

then you can access all of the variables in your class through that function

so you could make a function that increments each variable

and a function that outputs each variable

and everytime you want to add to that variable just call the function

=P

 ~ Geo

 ~ You are unique, just like everyone else
Reply By: Geo121 Reply Date: 11/21/2006 7:27:24 AM
Thank You for your respect

I was once a beginning C plus plusser myself =P

I could never find any help that was useful

then I found this forum

I've always had a knack for programming but as you have discovered as I did

No one knows everything

So you're gonna need some help along the way

The only thing I can say is keep up the good questions

You sound exceptionally good for a beginner

Remember Pointers are the most confusing things to use

especially before the namespace std and string types came out

Keep up the good work!

 ~ Geo

 ~ You are unique, just like everyone else
Reply By: 132591 Reply Date: 11/21/2006 10:39:08 PM
I just chose the sloppy way because I was not quite clear on the other way. The only reason I might seem good for a beginner is because I have my dad to help me, and he is a big C programming, so he can help me a lot on C++.

"Judge a man by his questions, not by his answers."
-Voltaire
\
Reply By: Geo121 Reply Date: 12/15/2006 8:12:26 AM
Hey How are you coming along on this game?

 ~ Geo

 ~ You are unique, just like everyone else

Go to topic 53679

Return to index page 92
Return to index page 91
Return to index page 90
Return to index page 89
Return to index page 88
Return to index page 87
Return to index page 86
Return to index page 85
Return to index page 84
Return to index page 83