-1

I'm writing a terminal based game similar to that of an old school racer where you can only change lanes to avoid obstacles. I've been able to code everything else except the part where it asks for input in order to change lanes. The function for this is supposed to wait 2 seconds for an input and if there is an input, it determines which lane to change to. cin>>input; makes the program wait indefinitely and for some reason getchar(); doesn't work.

I've already searched and found this: Wait for input for a certain time

My problem is that I don't have <conio.h>, <curses.h>, and <ncurses.h> so I wouldn't be able to use getch(); Is there any way I could make the computer do this without those library files?

I also need help decreasing the time needed for waiting in order to increase the game's difficulty. sleep(); isn't working because it's library file isn't included and I'm having trouble using nanosleep();but I want to be able to decrease time interval by .1 sec each time the player has to input until there is only .5 secs to input. Is there a way I can do this as well?

Code related to what I have now:

this is the function in its current state

this is what i'm using in order to use kbhit();

Community
  • 1
  • 1
  • 1
    There is no platform-independent way of doing this. Which platform are you on? –  Mar 06 '16 at 17:39
  • 2
    You don't have the headers? Easy get them :) – Joel Mar 06 '16 at 17:40
  • 1
    In that case you want this answer: http://stackoverflow.com/questions/5852385/console-keyboard-hit-detection-and-interpretting –  Mar 06 '16 at 17:42
  • 1
    You can use the technique here: http://stackoverflow.com/q/10522277/951890 That answer is for a serial port, but it works the same for a terminal window. – Vaughn Cato Mar 06 '16 at 17:44
  • I don't think I can install the header files because I'm running off the university's network with puttty – user2426191 Mar 06 '16 at 17:48
  • "I am on windows" and "using universities computer with putty", together with "nanosleep" would be that you are "Using windows to connect to a Linux/Unix system at the university" - which means the code you are writing is for Linux/Unux, which is what we care about. If you are using carrier pigeon, steam locomotives and pieces of scaffolding to enter your code doesn't really matter for the answer to the question. Most Linux systems allow you to install things in your a local directory, so even if you [most likely] don't have rights to install globall, you should be able to install things. – Mats Petersson Mar 06 '16 at 17:58
  • Of course, assuming you are doing this as a hobby project. If you have a sadist as a teacher that tells you that you have to solve such problems without libraries, then I would consider asking the teacher for help on WHAT KIND of solution he/she is looking for, as the links suggested above may be exactly what they are looking for, or NOT. – Mats Petersson Mar 06 '16 at 18:00
  • lol, I apologize i'm complete beginner a c++ so I'm still learning. Basically, in class, we only covered up to creating different functions but I do have some previous experience. This for a project but our professor said that the our project ideas should "include random numbers and functions and be reasonably hard to make." Hence, I tried making this but I've hit obvious roadblocks. – user2426191 Mar 06 '16 at 18:10
  • Seriously: I would pick a simpler task. Game of hangman, guess a number or some such. – Mats Petersson Mar 06 '16 at 18:22
  • I can't use any idea another group has already done, including the ones you've suggested, unfortunately. I'm going to use for `getch();` but in any case i will try to come up another idea. – user2426191 Mar 06 '16 at 18:56

2 Answers2

2

if you want to code it your way , you could create another thread that handles the input , the main program kills that thread after x time and checks if an entry has been made .

GhaziBenDahmane
  • 548
  • 3
  • 15
  • And I take it you are willing to hit enter or each bit too? – Mats Petersson Mar 06 '16 at 17:55
  • I am willing as long as the program waits a certain amount of time for input. However, I don't know where to start with doing this solution, as I haven't learned how to do this yet. Can you point me to a link so I could try to do it? – user2426191 Mar 06 '16 at 18:16
0

The C++ standard does not have a "wait for input for X units of time". As such, you will need to use some library or OS-specific functionality to achieve this. Since you already mention nanosleep, you probably are using Linux, so install ncurses and use that.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227