Countdown & timer?

Jun 27, 2013 at 11:19pm
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.

I'm not sure how to go about this, so any help will be greatly appreciated!

Thanks!
Jun 28, 2013 at 1:03am
closed account (18hRX9L8)
You can use a sleep function for the timer.

1
2
3
4
5
6
7
8
9
#include <windows.h>
#include <iostream>

int main(void) {
        std::cout<<"This program will waste 15 seconds of time.";
        Sleep(15000);
        std::cout<<std::endl<<"Thanks for letting us waste your time...";
        std::cin.ignore();
}
Jun 28, 2013 at 4:30am
here is my demo for one round, secret number random from 1 to 10, 5 guesses per round, 20 seconds per guess:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <string>
#include <windows.h> // GetAsyncKeyState(), Sleep()
#include <conio.h>   // getch()
#include <ctime>     // time()
#include <cstdlib>   // 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...
Jun 28, 2013 at 5:44am
@tntxtnt

Wow, that's awesome. Thanks!

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include <ctime>
#include <stdio.h>
#include <Windows.h>
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 Jun 28, 2013 at 5:45am
Topic archived. No new replies allowed.