### Countdown & timer?

Hello!

I am currently trying to make a little game.

Basically, it picks a number say between 1 and 10, and you have to guess it.

I want to give the user 3 tries per guess with 5 guesses per round, and I want them to have 20 seconds per guess.

After they fail to get 3 guesses either by taking too long or using all their guesses, I want the game to end and to print a score. (in total seconds lasted)

I also want the game to get progressively harder (using a larger scale like 1-20, 1-30, etc.) and to give them either less guesses/less time to get it (randomly)).

I would like to have a countdown at the start that says "starting in 1... 2... 3... go!" to give them a chance to get ready, and also one between rounds.

Thanks!
You can use a sleep function for the timer.

 ``123456789`` ``````#include #include int main(void) { std::cout<<"This program will waste 15 seconds of time."; Sleep(15000); std::cout<
here is my demo for one round, secret number random from 1 to 10, 5 guesses per round, 20 seconds per guess:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172`` ``````#include #include #include // GetAsyncKeyState(), Sleep() #include // getch() #include // time() #include // srand(), rand(), atoi() using namespace std; int get_int(int sec); int main() { srand(time(0)); int comp_num = rand() % 10 + 1; //random 1-10 int guess = 0; int lives = 5; //5 guesses int guess_time = 20; //20 secs / guess while (guess!=comp_num && lives) { guess = get_int(guess_time); if (guess != comp_num) { cout << "Wrong! The number is " << (comp_num>guess ? "higer" : "lower") << " than your guess\n"; --lives; } } cout << "You " << (guess==comp_num ? "win" : "lose") << "!\n" << "The number is " << comp_num << "\n"; cin.ignore(100,'\n'); } int get_int(int sec) { static const int sensitivity = 150; //higher == lesser sensitive time_t now = time(0); string input; while (time(0)-now < sec) { if (GetAsyncKeyState(VK_RETURN)) { // enter/return getch(); Sleep(sensitivity); break; } if (GetAsyncKeyState(VK_BACK)) { // backspace getch(); cout << "\b \b"; if (!input.empty()) input.erase(input.end()-1); } for (int c=0x30; c<=0x39; ++c) // 0-9, NUMPAD_0-NUMPAD_9 if (GetAsyncKeyState(c) || GetAsyncKeyState(c+0x30)) { input += (char)c; cout << (char)c; getch(); } //other printable keys... if (GetAsyncKeyState(0x20)) getch(); // space for (int c=0x41; c<=0x5a; ++c) // A-Z if (GetAsyncKeyState(c)) getch(); for (int c=0x6a; c<=0x6f; ++c) // other NUMPAD keys if (GetAsyncKeyState(c)) getch(); for (int c=0xba; c<=0xc0; ++c) // other US standard keys if (GetAsyncKeyState(c)) getch(); for (int c=0xdb; c<=0xdf; ++c) // other US standard keys if (GetAsyncKeyState(c)) getch(); Sleep(sensitivity); } cout << "\n"; return atoi(input.c_str()); }``````

the hardest part is "to have 20 seconds per guess." I need both windows.h and conio.h (both non-standard) to do this...
@tntxtnt

Wow, that's awesome. Thanks!

I made a version myself without the timed thing, and I'd really appreciate your feedback!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111`` ``````#include "stdafx.h" #include #include #include #include #include using namespace std; double Round3() { cout << "Round 3 - Coming soon!" << endl << endl; cin.get(); cin.get(); exit(0); return 0; } double Round2() { cout << "The rounds will now progressively get more difficult." << endl; cout << "You will have to guess a number between 1 and 20. You will have 5 chances." << endl; cout << "Press enter to start Round 2!" << endl; cin.get(); cin.get(); srand((unsigned int)time(NULL)); int Round2Guess; int Round2Answer = rand() % 20 + 1; for(int Round2GuessCount = 0; Round2GuessCount < 5 || Round2Guess == Round2Answer; Round2GuessCount++) { cout << "Guess a number (1-20): "; cin >> Round2Guess; if(Round2Guess > Round2Answer) { cout << "The answer is lower!" << endl; } else if(Round2Guess < Round2Answer) { cout << "The answer is higher!" << endl; } else if(Round2Guess == Round2Answer) { cout << endl << "Correct!" << endl << endl; cout << "Starting Round 3..." << endl; Round3(); } } cout << endl << "Oops! You exceeded the maximum attempt limit!" << endl; cout << "Your score: 50" << endl; cin.get(); cin.get(); return 0; } double Round1() { srand((unsigned int)time(NULL)); int Round1Guess; int Round1Answer = rand() % 10 + 1; for(int Round1GuessCount = 0; Round1GuessCount < 3 || Round1Guess == Round1Answer; Round1GuessCount++) { cout << "Guess a number (1-10): "; cin >> Round1Guess; if(Round1Guess > Round1Answer) { cout << "The answer is lower!" << endl; } else if(Round1Guess < Round1Answer) { cout << "The answer is higher!" << endl; } else if(Round1Guess == Round1Answer) { cout << endl << "Correct!" << endl << endl; cout << "Starting Round 2..." << endl; Round2(); } } cout << endl << "Oops! You exceeded the maximum attempt limit!" << endl; cout << "Your score: 10" << endl; cin.get(); cin.get(); return 0; } int main() { cout << "Welcome to TrulyRazor's guessing game!" << endl; cout << "Round 1 starts with you guessing a number 1-10 with 3 chances." << endl; cout << "Press enter to start!" << endl; cin.get(); cout << "Good luck!" << endl << endl; Sleep(1000); Round1(); }``````
Last edited on
Topic archived. No new replies allowed.