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

Author Topic: C++  (Read 3773 times)

0 Members and 1 Guest are viewing this topic.

Dilbert

    Topic Starter
  • Moderator


  • Egghead

  • Welcome to ComputerHope!
  • Thanked: 44
    C++
    « on: August 20, 2006, 08:30:28 PM »
    Instead of shower this forum with a bunch of threads on C++, I'm going to just post all my questions here as they arise. One question at a time, but then you won't have to wade through a bunch of threads started by me. :)

    Variable parsing

    I'm trying to write a simple command-line game to test my ability to write long, complicated programs and to make sure everything I've learned so far is embedded in my skull so I won't forget it.

    Now, I want to have the program accept input (easy), then perform actions based on that command (not quite as easy). OK, it's a bunch of If statements, no problem there. My problem is that I have IF conditions like this:

    Code: [Select]
    if (action == "flip light switch" || action == "turn light on" || action == "light on" || action == "turn on light")
    What a mess! I would rather have the program parse the variable's contents and look for key words, like "light" and "on". This way, I can be a little more concise in my code-writing and therefore delay carpal-tunnel syndrome. :)

    Google isn't revealing much. Maybe I'm just not searching for the right keywords, but all I'm getting is the "reserved" list of things I can't name variables after. So... how can I do this?
    "The geek shall inherit the Earth."

    ghostdog74



      Specialist

      Thanked: 27
      Re: C++
      « Reply #1 on: August 20, 2006, 08:52:08 PM »
      Don't know whether it's what you want, but how about a switch construct for a bit of neatness: (can't remember exactly the syntax, pls check with references/manuals )
      Code: [Select]
      ....
      switch (action) {
         case "flip light switch: do_flip()
         case "turn light on" : do_turnlighton()
         case "light on" : do_lighton()
         case "turn on light" : do_turnonlight()
         default: some statement
      }
      ...

      « Last Edit: August 20, 2006, 10:23:33 PM by ghostdog74 »

      Rob Pomeroy



        Prodigy

      • Systems Architect
      • Thanked: 124
        • Me
      • Experience: Expert
      • OS: Other
      Re: C++
      « Reply #2 on: August 20, 2006, 09:50:59 PM »
      There will be many ways to search through strings in C++.  Probably the "string within string" function is called substr or strstr - see your favourite reference pages.  But for a more powerful solution, you need to learn about regular expressions, and the associated C functions.
      Only able to visit the forums sporadically, sorry.

      Geek & Dummy - honest news, reviews and howtos

      Neil



        Expert
      • Fear me Track. Noone can escape my wrath.
      • Thanked: 3
        Re: C++
        « Reply #3 on: August 21, 2006, 07:24:54 AM »
        Is that something you actually want to do? Or just an example :P

        It would probably be easier to present the user with some options like 1. turn light on 2. bake a cake... and have them pick, rather than design a complex program to interperate their input. If this is part of some larger program, I say keep it simple  ;)

        Unless the program TO interperate all this IS your project, then I say... good luck :P

        Dilbert

          Topic Starter
        • Moderator


        • Egghead

        • Welcome to ComputerHope!
        • Thanked: 44
          Re: C++
          « Reply #4 on: August 21, 2006, 07:52:03 AM »
          Neil, I do indeed want to do this, because yes, it does familiarize me with the strstr command (yep, that's the one :)). That's from an education standpoint.

          I also have another reason for this, and it may have to do with a hefty amount of nostalgia, but...

          ***IGNORE THE BELOW, IT IS THE RAMBLINGS OF A PERSON WITH TOO MUCH TIME ON HIS HANDS***

          See, back when games were command-line, they would have some games do just that; interperet input and act based on it if it could recognize a familiar pattern.

          This seems so stupid, but in spite of all the advances in technology, my favorite game of all time so far has been one I discovered in fourth grade -- on an Apple IIe. It was a game like the one I'm trying to do; you'd be given some background info (not much, though; the player was left to discover a lot of the background, which was more realistic) and dumped in the game.

          In that game, the player was started in their room with some sort of... hangover(?) that made it impossible to move clearly or do much of anything of interest. After solving that problem, you're allowed to do a few things in the room, etc.... but it was all command-line. So there would be a lot of "unknown command" errors doled out to the player. This, in a way, made the game much more challenging, for a very good reason.

          The reason that this method adds challenge is that it requires players to consider their next move. For example, let's take Room #1 in my Game In Progress. There are two things that do anything remotely useful in the very beginning: Turn on the light (actually, this doesn't work, but it does give away the alternative), and turn on the computer (a substitute light source).

          OK, so there's two actions. If both actions are displayed, then it takes away from a lot of potential for challenge. Making the user try things instead of being presented with a list is actually more realistic in the sense that no real human being is ever presented with a list. So it adds to the realistic feel of the scenario, in my opinion. Not to mention one more fact: In command-line games like this, the main character is only as smart as you are. So on some days, he can be pretty clueless.

          OK, yes, it's probably also and mostly nostalgia. But either way, I love this kind of game. :)

          Anyway, back to my example: Through a long chain of events, if you do a few optional commands, you can find out the story, which is that you were evicted and your house is scheduled for demolition on... today... but for some reason you never got the mail. (I get the feeling now that the main character was a druggie) So, there I am, standing there as this bulldozer advances on my house. Well, I never got past this, because the usual command I'd think to enter (like "RUN" or "Wave arms" or "shout" or even "go north") wouldn't work. So, I always lost.

          In retrospect, it's a stupid game from an obsolete computer. And yet, for reasons unknown, I came back to that game 50 times throughout the year. It was a lot of fun.

          </IGNORE>

          OK, back to my original reasons. In summary: 1) I want to, and 2) I can further my education using it. :)
          « Last Edit: August 21, 2006, 07:54:01 AM by Timothy_Bennett »
          "The geek shall inherit the Earth."

          Neil



            Expert
          • Fear me Track. Noone can escape my wrath.
          • Thanked: 3
            Re: C++
            « Reply #5 on: August 21, 2006, 08:00:41 AM »
            Have you ever played the original Monkey Island? Although it was graphics based, there was a "library" of available commands, such as use, pick up, turn on, etc which you could click on to implement. Perhaps you could create a preset library of lots of text commands the player can try out. This might take away the challange of discovering the commands for yourself, but would also remove some of the random guessing, so it might place itself somewhere in the middle of the two methods. Give your player some help... but not too much ;)

            Neil



              Expert
            • Fear me Track. Noone can escape my wrath.
            • Thanked: 3
              Re: C++
              « Reply #6 on: August 21, 2006, 09:57:26 AM »
              How are you creating the engine for this game? I have a suggestion. I'm not too savvy with the object orientated side of c++ so I'll just write the ideas; you'll need to do the code ;)

              Create each room or scene as an object, or class, or whatever. Each room is then subdivided into different objects contained within the room. For example, room1.lightswitch, room1.book. Each object within the room also has a list of actions to be performed depending upon the user's selection. Each action has an ID number which is hardcoded elsewhere in the program. For example room1.lightswitch.use = 32, means that if the player does the use action on the lightswitch, perform action 32. All the other actions will also need to be accounted for. For example, room1.lightswitch.eat = 2. Else where in your program...

              switch(action)

              case: 0 ...
              case: 1 ...
              case: 2
              display message "You cannot eat that" ... so for all other items which cannot be eated, redirect them to this action
              case: 3 ...
              case: 32
              if lightsactivated = true then lightsactivated = false, display message "You turn the lights off"
              else if lightsactivated = false then lightsactivated = true, display message "You turn the lights on"

              Another example.
              useraction = read, object = room1.book
              room1.book.read = 64

              case: 64
              if lightsactivated = true then display message "The book contains a computer password", passwordknown = true
              else display message "It is too dark to read the book"



              And so on.

              Dilbert

                Topic Starter
              • Moderator


              • Egghead

              • Welcome to ComputerHope!
              • Thanked: 44
                Re: C++
                « Reply #7 on: August 21, 2006, 07:46:48 PM »
                Say, I found something interesting that allows me to do what I wanted with substrings. :)

                Code: [Select]
                std::string test = "This is a sample string";
                    int i;
                     i = test.find("sample",0);
                    if(test.rfind("This",0) != string::npos)
                    {
                     //Do something
                     }

                I could create a seperate function to parse through for specific commands and store them. I like this. I could just repeat this algorithm for each possible command keyword(s). Excellent.

                Oh, but I still will use your idea of listing available commands. However, it won't be visible by default; it'll be one of the commands I'll parse (which, on execution, will display all possible command keywords). This way the challenge is preserved, I eliminate the guessing and it doesn't spoil one drop of the game! ;D (Oh, and I'll make it clear in the intro that it is possible to view a list of commands. ;))

                [edit]You know, designing this game is going to be a huge challenge for a beginner, but for what it's worth, I'm learning a lot in the process. :)[/edit]
                « Last Edit: August 21, 2006, 08:06:13 PM by Timothy_Bennett »
                "The geek shall inherit the Earth."