Timed input

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • owestcot
    New Member
    • Feb 2007
    • 7

    Timed input

    I'm working on a simple video game program that will require timed responses. The easiest example I can think of would be something like:

    char ans; // answer to question
    (can't remember the variable type) starttime; //when clock starts, I'm at work and don't have access to my C++
    (same story, when I get home, I'll fix this) timer; // makes sure person does go over time limit
    cout<<"Press choose your answer shown on the screen (A/B/C/D)\n";

    /*not actually what I'm doing, just an easier version with the same idea, basically just a multiple choice question with limited time to answer. */
    Code:
    starttime=time(NULL); //once again, I believe that's right, but am at work
    do
    {
      cout<<"You're in space, you:\nA) Die.\nB) Vomit.\nC) Play.\nD) Fall anyway.\n"; // I want this to stay, so no "CLS," but I want it to disappear after time is up
      timer=time(NULL); //makes timer soon after starttime
      while( (starttime+10>=timer)&&( (ans!='A')||(ans!='a')||(ans!='B')||(ans!='b')||(ans!='C')||(ans!='c')||(ans!='D')||(ans!='d') )
      {
    /*This upcoming part is what is actually confusing me, because I'm not sure how to stop letting the person give input, the other option would be to make sure the value of 'timer' isn't over 10 more than 'starttime', or it switches to 'L', but I'd rather it just stop the input.  Any input?*/
    
    
         cin>>ans; // I want them to be able to enter for limited time, but a while loop doesn't stop until the cin is entered, will it?
         timer=time(NULL); // resets timer in case while loop has more time and person didn't enter a valid answer
      }
    
    
    
      if( (ans!='A')&&(ans!='a')&&(ans!='B')&&(ans!='b')&&(ans!='C')&&(ans!='c')&&(ans!='D')&&(ans!='d') ) // if person doesn't answer in time, they are assigned an answer
      {
        ans='L';
      }
    }
    while( (ans!='A')&&(ans!='a')&&(ans!='B')&&(ans!='b')&&(ans!='C')&&(ans!='c')&&(ans!='D')&&(ans!='d')&&(ans!='L' ); // person really REALLY has an answer
    if( (ans=='a')||(ans=='A') )
    {
      cout<<"Your head a splode."; // gotta have humor
    }
    if( (ans=='b')||(ans=='B') )
    {
      cout<<"\nYou technicolor yawn."; // I'm bored at work, what can I say?
    }
    if( (ans=='c')||(ans=='C') )
    {
      cout<<"You twiddle your thumbs."; // Some jokes aren't meant to be funny.
    }
    if( (ans=='d')||(ans=='D') )
    {
      cout<<"You fall to your doom.\n\n...That's kinda weird, what with the lack of gravity.  You're my hero!"; // Gotta admit, REALLY bored at work
    }
    if(ans=='L')
    {
      cout<<"\nBefore you can react, you get hit by a bus."
    }
    
    /*That's all that I think really relates, but I'll write more when I get home.*/
    Last edited by RedSon; Feb 21 '07, 10:08 PM. Reason: Code Tags
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    One thing you can do is get the current system time and save it off. Then ask the user for input. Then get the system time immediately after they have entered their input. You can subtract the time and see if it is longer then your threshold. If it is then they took to long. If it isnt they they got their answer in, under the time limit.

    Comment

    • owestcot
      New Member
      • Feb 2007
      • 7

      #3
      Originally posted by RedSon
      One thing you can do is get the current system time and save it off. Then ask the user for input. Then get the system time immediately after they have entered their input. You can subtract the time and see if it is longer then your threshold. If it is then they took to long. If it isnt they they got their answer in, under the time limit.
      Sorry, still unfamiliar with these forums, but I will be using them more. I had said "I'm not sure how to stop letting the person give input, the other option would be to make sure the value of 'timer' isn't over 10 more than 'starttime', or it switches to 'L', but I'd rather it just stop the input."

      That is totally an option, but it's more of a plan B, if I can't just stop the input from happening.

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by owestcot
        Sorry, still unfamiliar with these forums, but I will be using them more. I had said "I'm not sure how to stop letting the person give input, the other option would be to make sure the value of 'timer' isn't over 10 more than 'starttime', or it switches to 'L', but I'd rather it just stop the input."

        That is totally an option, but it's more of a plan B, if I can't just stop the input from happening.
        Did you consider the 'sleep()' function in correlation with 'fork()'? (Would be Linux-dependent.)

        It looks like there is a "CreateProcess( )" in the Win32 API, but I haven't looked into if it runs parallel to the other process, etc...

        Comment

        • owestcot
          New Member
          • Feb 2007
          • 7

          #5
          Originally posted by sicarie
          Did you consider the 'sleep()' function in correlation with 'fork()'? (Would be Linux-dependent.)

          It looks like there is a "CreateProcess( )" in the Win32 API, but I haven't looked into if it runs parallel to the other process, etc...

          ...I really have very little understanding of what that means. So it's probably the right stuff for me to know. Murphy's law.

          Anyway, could you please explain in more laymen's terms?

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by owestcot
            ...I really have very little understanding of what that means. So it's probably the right stuff for me to know. Murphy's law.

            Anyway, could you please explain in more laymen's terms?
            What operating system are you using to code this, and what operating systems do you want it to be for? (If you only code for Windows or Linux, it will not be usable on the other type of system)

            Comment

            • owestcot
              New Member
              • Feb 2007
              • 7

              #7
              Originally posted by sicarie
              What operating system are you using to code this, and what operating systems do you want it to be for? (If you only code for Windows or Linux, it will not be usable on the other type of system)
              Well, I use windows (XP), but the C++ program is Dev-C++. I'm trying to make a video game (like almost everyone else) that I'd be able to play in my free time...after using all my free time to make it. So probably the same one.

              I'm not very good with computers, but I am with math and video games, which is why I took a C++ class in HS, but now I'm a few years since and don't remember everything I learned in my mere two trimesters of it...so basically I need computer terminology to be stated like I'm a five year old (great movie).

              Comment

              • sicarie
                Recognized Expert Specialist
                • Nov 2006
                • 4677

                #8
                Originally posted by owestcot
                Well, I use windows (XP), but the C++ program is Dev-C++. I'm trying to make a video game (like almost everyone else) that I'd be able to play in my free time...after using all my free time to make it. So probably the same one.

                I'm not very good with computers, but I am with math and video games, which is why I took a C++ class in HS, but now I'm a few years since and don't remember everything I learned in my mere two trimesters of it...so basically I need computer terminology to be stated like I'm a five year old (great movie).
                Well, this is something a quick google search of createprocess win32 api returned, looks like it has a good explanation of it.

                And to the ends of creating a video game, I'm probably one of the people on this site least able to help you (sorry!). I'm all application stuff - really even prefer hardware to coding, but right now it's where the job market is taking me.

                Can anyone else put up a few good links for video game development?

                (The type of game you're trying to create might help here - MMO/RPG, FPS, etc...)

                Comment

                • AdrianH
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1251

                  #9
                  Try istream::readso me(). It will read from the stream buffer and will return immediatly even if your buffer is not filled.


                  Adrian

                  Comment

                  • owestcot
                    New Member
                    • Feb 2007
                    • 7

                    #10
                    I'm doing this for a complex RPG, this is just one itty-bitty part of it. The problem is that I want people to get the sense of rushed answers, and so after the time limit, I want the answers and such to disappear and the default message to be posted.

                    Basically at this point I can use the time between when they input and when they should have, but I'd rather just stop the input available, like in the keyboarding videogames, where you have limited time and you're supposed to type things, and after the time limit it stops you. But it is only one character that is being input(ed?), so I suppose the difference between the times would work.

                    Also, much more basic question, can the arrow keys be input as characters?

                    Comment

                    • AdrianH
                      Recognized Expert Top Contributor
                      • Feb 2007
                      • 1251

                      #11
                      Originally posted by owestcot
                      I'm doing this for a complex RPG, this is just one itty-bitty part of it. The problem is that I want people to get the sense of rushed answers, and so after the time limit, I want the answers and such to disappear and the default message to be posted.

                      Basically at this point I can use the time between when they input and when they should have, but I'd rather just stop the input available, like in the keyboarding videogames, where you have limited time and you're supposed to type things, and after the time limit it stops you. But it is only one character that is being input(ed?), so I suppose the difference between the times would work.

                      Also, much more basic question, can the arrow keys be input as characters?
                      Then readsome() is what you want, provided you want to be using the ANSI standard C++ library. If this is specific to some platform and it will not be ported to another OS, you could use library calls that are specific to it.

                      If the user has a time limit, I'd put some sort of prompt that changes showing how much time is left.

                      Arrow keys don't add to the input buffer. If you want that, you require using a terminal library, such as ncurses. Again this is a terminal standard. If you are programming for a particular platform and it will not be ported, you can try using that OSs interface library instead.


                      Adrian

                      Comment

                      • nmadct
                        Recognized Expert New Member
                        • Jan 2007
                        • 83

                        #12
                        What exactly is your end product gonna look like? Is this a terminal-based adventure/RPG game? Are there graphics involved? If you want complete control of I/O I'd recommend using ncurses on the terminal or SDL if you're using graphics. Even on the console, the SDL has some convenient features for I/O, timers and sound.

                        Comment

                        • owestcot
                          New Member
                          • Feb 2007
                          • 7

                          #13
                          Originally posted by AdrianH
                          Then readsome() is what you want, provided you want to be using the ANSI standard C++ library. If this is specific to some platform and it will not be ported to another OS, you could use library calls that are specific to it.

                          If the user has a time limit, I'd put some sort of prompt that changes showing how much time is left.

                          Arrow keys don't add to the input buffer. If you want that, you require using a terminal library, such as ncurses. Again this is a terminal standard. If you are programming for a particular platform and it will not be ported, you can try using that OSs interface library instead.


                          Adrian
                          ...(Yay elipsis!!!) Yeah, really don't understand most, so I'll try summarizing, and just tell me when I'm completely wrong.

                          I'm using DevC++, which I was recently told sucks, as to whether or not this has ANSI or how I can tell, I am oblivious.

                          The platform that it will be used on...is that like windows, or would that be the console? I'm really not sure about most of this stuff, and if someone knew of an idiot's dictionary for C++ online, I'd love a link. Most of the webpages I've looked at go from naming the words to using them in a program and use pretty high-end terminology.

                          Don't really know what 'input buffer' is, but I do understand that arrow keys don't do anything.

                          I don't really understand anything in the 'terminal' paragraph except that I need a new library...proba bly.



                          Again, I'm really not all that well taught, and forgot quite a bit, the best path is to treat me like a 5 year old. More info than I need is better than making me ask even more questions.

                          Comment

                          Working...