-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
140 lines (126 loc) · 3.64 KB
/
main.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
int randomNumber, guess, chanceLimit, endNum, startNum;
char numChoice, chanceChoice, yesNo;
startNum = 1;
cout << "Welcome to the Number Guessing Game (v1.0.2)!" <<endl;
cout << "Guess a random number between your selected range within the chances you select to win the game!" <<endl;
cout << endl << "Project by Sampreet Roy (I use Arch Linux BTW)" <<endl <<endl;
cout << "Are you ready to play (y/n): ";
cin >> yesNo;
if (yesNo=='y') {
cout << endl << "Game settings:" <<endl <<endl;
cout << "Number ranges for the game" <<endl <<endl;
cout << "[a] 1 to 5" <<endl;
cout << "[b] 1 to 10" <<endl;
cout << "[c] 1 to 20" <<endl;
cout << "[d] 1 to 69" <<endl;
cout << "[e] Custom" <<endl;
cout << "Enter your choice (eg: a, b): ";
cin >> numChoice;
if (numChoice=='a')
{
startNum=1;
endNum=5;
}
else if (numChoice=='b')
{
startNum=1;
endNum=10;
}
else if (numChoice=='c')
{
startNum=1;
endNum=20;
}
else if (numChoice=='d')
{
startNum=1;
endNum=69;
}
else if (numChoice=='e')
{
cout << "Enter the starting number of your custom range: ";
cin >> startNum;
cout << "Enter the ending number of your custom range: ";
cin >> endNum;
}
else {
cout << "Invalid input, please re-run the game with a valid choice" << endl;
cout << "Press enter to exit (if you are on a Windows machine)" << endl;
return 0;
}
cout << endl << "Chances for the game" <<endl <<endl;
cout << "[a] 1" <<endl;
cout << "[b] 2" <<endl;
cout << "[c] 3" <<endl;
cout << "[d] 4" <<endl;
cout << "[e] Custom" <<endl;
cout << "Enter your choice (eg: a, b): ";
cin >>chanceChoice;
if (chanceChoice=='a')
{
chanceLimit=1;
}
else if (chanceChoice=='b')
{
chanceLimit=2;
}
else if (chanceChoice=='c')
{
chanceLimit=3;
}
else if (chanceChoice=='d')
{
chanceLimit=4;
}
else if (chanceChoice=='e')
{
cout << "Enter your custom chance limit: ";
cin >> chanceLimit;
}
else {
cout << "Invalid input, please re-run the game with a valid choice" << endl;
cout << "Press enter to exit (if you are on a Windows machine)" << endl;
return 0;
}
srand((unsigned) time(0));
randomNumber = (rand() % endNum) + startNum;
cout << endl << "Now the game starts!" <<endl;
for ( int chances = 1; chances <= chanceLimit; chances++ )
{
cout<<endl<<"Enter your guess: ";
cin>>guess;
if (guess>endNum || guess<startNum)
{
cout<<"Invalid input, please enter a valid guess in the range you chose"<<endl<<"Please re-run the game"<<endl;
break;
}
if (guess==randomNumber && chances<=chanceLimit)
{
cout<<"That was correct! You won! (Chances used: "<<chances<<"/"<<chanceLimit<<")"<<endl;
break;
}
else if (guess!=randomNumber && chances<chanceLimit)
{
cout<<"Wrong, please retry (Chances used: "<<chances<<"/"<<chanceLimit<<")"<<endl;
}
else if (guess!=randomNumber && chances<=chanceLimit)
{
cout<<"Sorry, out of chances! You Lost! The number was: "<<randomNumber<<endl;
}
}
}
else if (yesNo=='n') {
cout << "Closed game" <<endl;
}
else {
cout << "Invalid input, please retry with 'y' or 'n'" <<endl;
}
cout << "Press enter to exit (if you are on a Windows machine)" << endl;
getch();
}