Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: House Escape -- an old-style Adventure Game  (Read 8603 times)

0 Members and 1 Guest are viewing this topic.

Dilbert

    Topic Starter
  • Moderator


  • Egghead

  • Welcome to ComputerHope!
  • Thanked: 44
    Re: House Escape -- an old-style Adventure Game
    « Reply #15 on: February 23, 2007, 01:22:15 PM »
    Thanks for the info. I'm rewriting the first class now, and will be sure to test them before I go on. I can avoid embarrassing mistakes this way. ;)

    EDIT: Okay, this is annoying. I've got this in my class:

    Code: [Select]
    class game
    {
        . . .
        void inventory(tBedroom tBed);
        . . .
    };

    And it's giving me the error:

    error C2061: syntax error : identifier 'tBedroom' escape.cpp(14) : error C2511: 'void game::inventory(tBedroom)' : overloaded member function not found in 'game'

    Among a slew of other errors. I don't get it; I'm following syntax of member functions to the letter, so why is the compiler spitting errors at me?

    EDIT: I've managed to get the number of errors down by a factor of 10. The remaining 3 errors are all surrounding the game::inventory function. For some odd reason, the compiler doesn't like me using tBedroom as an identifier. I'm trying to pass tBedroom tBed as a parameter, like so:

    void inventory(tBedroom tBed);

    However, the compiler doesn't like that. Later, in the tBedroom class, I am able to use "game engine" as a parameter with no problems. Is this because class game appears before class tBedroom? If so, how can I correct this? I can't just rearrange the two classes, because if the problem lies with the order they are in, then tBedroom::getDoorLocked(game engine) and tBedroom openDoor(game engine) are going to have problems.
    « Last Edit: February 23, 2007, 07:04:30 PM by Timothy_Bennett »
    "The geek shall inherit the Earth."

    Dilbert

      Topic Starter
    • Moderator


    • Egghead

    • Welcome to ComputerHope!
    • Thanked: 44
      Re: House Escape -- an old-style Adventure Game
      « Reply #16 on: February 23, 2007, 10:43:20 PM »
      Fixed the above problem; I made a prototype of class tBedroom above game. However, I'm having other trouble, and I can't explain this.

      I'm testing the first two classes, as you know. However, I'm having an interesting problem: A public variables refuses to change value. Or rather, it does, then it goes back. I'm testing all my commands for how well they work. First, I try to do all the commands before it should be possible to see the items. Then, make it so they can be seen but no items are taken. Then, get the items and try to get out the room. It all passes the tests, until the last part: Opening the door with the proper tools.

      The variable I'm trying to modify is in class game, called gamestate. It is a USHORT (typedef'd as Unsigned Short Integer). The chain of functions is as follows for the last test:

      1. int main calls tBed.openDoor, passing engine (the game class) as a parameter.

      2. tBed.openDoor sees that tbed.DoorLocked, a bool variable, is false, so it calls engine.win().

      3. engine.win sets gamestate to 1, then cout's the variable's value. (It changed to 1, as expected)

      4. int main() does an if-then to check engine.gamestate's value. If it's 0, the test fails.

      5. As it happens, gamestate is 0, and the game fails the test.

      WHAT!?! Here's the code, in order of how it's called:

      In int main():
      Code: [Select]
      tBed.openDoor(engine);
      Code: [Select]
      void tBedroom::openDoor(game engine)
      {
            if(doorLocked == true)
            {
                  cout << "LOCKED.\n";
            }
            else
            {
                  engine.win();
            }
      }


      Code: [Select]
      void game::win()
      {
            cout << "GAMESTATE IS " << gamestate << endl;
            gamestate = 1;
            cout << "GAMESTATE IS NOW " << gamestate << endl;
      }

      Back to int main():
      Code: [Select]
           if(engine.gamestate == 0)
            {
                  cout << "TEST FAILED. DOOR CLOSED.\n";
              cout << "GAMESTATE IS " << engine.gamestate << endl;
            }
            else
            {
                  cout << "SUCCESS. ALL TESTS PASSED.\n"; //Passed final test
                  system("pause");
            }

      My output is:

      Quote
      SUCCESS. DOOR UNLOCKED.
      GAMESTATE IS 0
      GAMESTATE IS NOW 1
      TEST FAILED. DOOR CLOSED.
      GAMESTATE IS 0

      WHAT THE *censored*???
      « Last Edit: February 23, 2007, 11:02:44 PM by Timothy_Bennett »
      "The geek shall inherit the Earth."

      Neil



        Expert
      • Fear me Track. Noone can escape my wrath.
      • Thanked: 3
        Re: House Escape -- an old-style Adventure Game
        « Reply #17 on: February 24, 2007, 09:17:14 AM »
        This is why you should avoid changing public variables directly as much as possible. You should set it to private and create a public function to change its value, eg

        private: int blah;
        public: void changeblah(int newvalue);

        At first glance that might seem silly and pointless but if you used this way you could simply add into the changeblah function some kind of output of when it is being changed so you know exactly when it's changed and to what value in all cases.

        I suggest you use the search feature to go through all instances of this variable and make a note of when it is being changed and by what. You seem to have some rouge code changing the variable when it shouldn't be.

        Dilbert

          Topic Starter
        • Moderator


        • Egghead

        • Welcome to ComputerHope!
        • Thanked: 44
          Re: House Escape -- an old-style Adventure Game
          « Reply #18 on: February 24, 2007, 06:10:19 PM »
          Thanks. I did that, but it got no change. Then I figured it out. I was passing "engine" by value! I changed it to a reference and it worked fine. :)
          "The geek shall inherit the Earth."