-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumber.cpp
111 lines (105 loc) · 2.66 KB
/
Number.cpp
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 <iostream>
#include <stdlib.h>
#include <cstdlib>
#include <iomanip>
#include <limits>
using namespace std;
class numberGame
{
int n, guess, range;
public:
int RandomNumber(int range)
{
n = rand() % range;
cout << n << endl;
return n;
}
bool PlayGame()
{
int lives = 3;
do
{
cout << "Enter your guess >> ";
while (true)
{
cin >> guess;
if (cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please enter a valid number :";
}
else
break;
}
if (n == guess)
{
cout << "You won the game !! " << endl;
return true;
}
else if (guess < n)
cout << "Wrong answer !!\nHINT : The number is higher " << endl;
else if (guess > n)
cout << "Wrong answer !!\nHINT : The number is lower " << endl;
lives--;
if (lives == 0)
{
cout << "\nOut of lives !! The number was " << n << "\nbetter luck next time" << endl;
return false;
}
} while (lives != 0);
}
void play(int n){
RandomNumber(n);
PlayGame();
}
};
int main()
{
system("cls");
srand(time(0));
int choice;
numberGame n;
cout << "~~~ WELCOME TO GAME ~~~" << endl;
do
{
cout << "-----------------------------------------------------" << endl;
cout << " Enter difficulty " << endl;
cout << "1. Easy" << endl;
cout << "2. Moderate" << endl;
cout << "3. Hard" << endl;
cout << "4. exit game" << endl;
cin >> choice;
while (true)
{
if (cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Please enter a valid number : ";
}
else
break;
}
cout << "-----------------------------------------------------" << endl;
switch (choice)
{
case 1:
n.play(10);
break;
case 2:
n.play(100);
break;
case 3:
n.play(1000);
break;
case 4:
cout << "Bye Bye !! " << endl;
break;
default:
cout << "Invalid choice" << endl;
break;
}
} while (choice != 4);
return 0;
}