0

I want to make a Gomoku game with a time restriction mode, so is there any way to do it only using basic knowledge? Sleep() function cause the program to freeze but i want to make a timer and the program run at the same time.

  • Games are typically event-driven, with a "main loop" which handles different events like input. If your design is like that then you could add regular checks in the main loop for the current time, and use it in your countdown timer. Or depending on the system you use (framework, operating system) there could be timers which generate events once they expire. Note that doing something like that with a text-based and synchronous program (using e.g. the standard input and output functions of C++) makes this much harder since waiting for input isn't event-driven. – Some programmer dude Dec 06 '21 at 07:57
  • Is your problem the same as this? https://stackoverflow.com/questions/54019545/making-a-countdown-timer-in-c – SNORLAX Dec 06 '21 at 07:57
  • @Someprogrammerdude so i should create a function that decrease time for every second passes like: void countdown(int time){ sleep(1) time-- ; } Is this okay? – zzsolozz12 Dec 06 '21 at 08:19
  • @SNORLAX not really, i want both the game and the countdown to run at the same time, not wait for the countdown to finish then the game run. – zzsolozz12 Dec 06 '21 at 08:20
  • No like that. For a simple timer you get the current time in your event loop, and if one second has passed since last time you got the current time (save in a variable) you decrease the countdown timer variable. When it reaches zero you do whatever action you need to do. – Some programmer dude Dec 06 '21 at 09:05
  • @Someprogrammerdude sorry for my little knowledge but can you please explain it in a more detailed way? – zzsolozz12 Dec 06 '21 at 09:36
  • Not without any more context. How do you create your game? Is it using a framework? Does it use plain standard C++ text input and output? Does it have a GUI? What operating system do you target? Does it even *have* an event loop (possibly though a framework)? Please [edit] your question to enlighten us. – Some programmer dude Dec 06 '21 at 09:41
  • And please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Dec 06 '21 at 09:41
  • And have you read the duplicate questions? Do they solve your problem, or can the answers in them be modified to suit your program and solve the problem that way? – Some programmer dude Dec 06 '21 at 09:42

0 Answers0