Random number game in a class.

What do you think?, something random I thought of doing lol..

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
#include <iostream>
#include <cstdlib>
#include <ctime>

class RandomNumberGame{
	int random_number, attempts, guess, key;
public:
	RandomNumberGame();
	void Rules(), AttemptHints();
	bool HandleGame();
};

RandomNumberGame::RandomNumberGame(){ 
	srand(time(0)); 
	random_number=(0), attempts=(0), guess=(0), key=(0);
}

void RandomNumberGame::Rules(){
	std::cout<<"Guess a number between 1 - 10, you have 3 attempts..."<<std::endl;
}

void RandomNumberGame::AttemptHints(){
	switch(attempts){
	case 1:
		std::cout<<attempts<<"st try!"<<std::endl;
		break;
	case 2:
		std::cout<<attempts<<"nd try!"<<std::endl;
		break;
	case 3:
		std::cout<<attempts<<"rd try!"<<std::endl;
	default:break;
	};
}

bool RandomNumberGame::HandleGame(){
	random_number = (1 + rand() % 10);
	do{
		std::cin>>guess;
		
		if(guess == random_number){
			std::cout<<"Congratulations, you guessed the right number! ["<<guess<<']'<<std::endl;
			return true;
		}else if(guess > random_number){
			++attempts;
			std::cout<<"Your guess is to high ";

			AttemptHints();
		}else{
			++attempts;
			std::cout<<"Your guess is to low ";

			AttemptHints();
		}
	}while(attempts < 3);

	if(attempts == 3)
		std::cout<<"You lost! ahaha, hit any key & Press <ENTER> to exit..."<<std::endl;
		std::cin>>key; 
		return false;
}

int main(int argc, char* argv[]){

	RandomNumberGame *rng = new RandomNumberGame;
	rng->Rules(); //Function Rules() is optional, not necessary to apply. Blah w.e 
	rng->HandleGame(); 
	delete rng;

	return 0;
}
Last edited on
Nice, works pretty good. Except that hitting "ENTER" does not exit the program as told by the computer.

I did something similar, but much simpler.

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    srand(time(0));

    int answer = 1+(rand()%100);
    int number;

    cout << "What number am I thinking of between 1 and 100?\n\n";

    do
    {
        cin >> number;

        if(number>answer)
        {
            cout << "Too high!\n\n";
        }

        if(number<answer)
        {
            cout << "Too low!\n\n";
        }
    } while(number!=answer);

    if(number==answer)
    {
        cout << "CORRECT! You win!\n\n";
    }

    cin.get();
}


While we're on the topic, can you explain how I would include an option of exiting the program by hitting a certain key (ex: the enter key)? I've never done that before.
Actually here's a modified version. This one gives user the option to play again by entering Y or N, and also outputs how many tries it took user to guess the correct number.

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
   label:
    srand(time(0));

    int answer = 1+(rand()%100);
    int number, tries, cnthigh=0, cntlow=0;
    char reply;

   
    cout << "What number am I thinking of between 1 and 100?\n\n";

    do
    {
        cin >> number;

        if(number>answer)
        {
            cout << "Too high!\n\n";
            cnthigh++;
        }

        if(number<answer)
        {
            cout << "Too low!\n\n";
            cntlow++;
        }
    } while(number!=answer);

tries = cnthigh+cntlow+1;

    if(number==answer)
    {
        cout << "CORRECT! You win!\n";
        cout << "It took you " << tries << " tries\n\n";
        cout << "Play again? {Y/N)\n";
        cin >> reply;
        if(reply=='y' || reply == 'Y')
        {
            cout << "\n\n";
            goto label;
        }
        else return 0;
    }

    cin.get();
}
Last edited on
Topic archived. No new replies allowed.