Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old October 27th, 2006, 06:17 PM
Authorized User
 
Join Date: Sep 2006
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default text game

what is wrong with this code?

class.h:
Code:
#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:
Code:
#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
\
__________________
\"Judge a man by his questions, not by his answers.\"
-Voltaire
Reply With Quote
  #2 (permalink)  
Old October 30th, 2006, 08:57 AM
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

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 With Quote
  #3 (permalink)  
Old October 30th, 2006, 08:58 AM
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

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

 ~ Geo
Reply With Quote
  #4 (permalink)  
Old October 30th, 2006, 08:59 AM
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

and then change your function declaration inside of your class to

void actiondesc(string);

 ~ Geo
Reply With Quote
  #5 (permalink)  
Old October 30th, 2006, 09:00 AM
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
  #6 (permalink)  
Old October 30th, 2006, 09:02 AM
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

Don't worry about the compiler warning if you get one because thats just telling you that NULL isn't an int

 ~ Geo
Reply With Quote
  #7 (permalink)  
Old October 30th, 2006, 09:05 AM
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

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 With Quote
  #8 (permalink)  
Old October 30th, 2006, 09:09 AM
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

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 With Quote
  #9 (permalink)  
Old November 17th, 2006, 10:00 PM
Authorized User
 
Join Date: Sep 2006
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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 With Quote
  #10 (permalink)  
Old November 18th, 2006, 03:02 PM
Authorized User
 
Join Date: Sep 2006
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Also I need help with something else. How would I keep track of a person's points and kills? I cannot simly do this
Code:
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 With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Game programming. Magxtopher BOOK: Beginning JavaScript 3rd Ed. ISBN: 978-0-470-05151-1 3 September 11th, 2008 09:49 AM
Pong Game Help -XM- BOOK: Professional XNA Game Programming: For Xbox 360 and Windows ISBN: 978-0-470-12677-6 4 June 20th, 2007 02:23 PM
Old game review guti Need help with your homework? 0 March 20th, 2007 11:28 AM
How to Open the Game Mamatha Classic ASP Basics 0 May 17th, 2004 02:09 AM
STRATEGIE - GAME! WarKrife C++ Programming 1 August 21st, 2003 08:10 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.