#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
void wrapUp() {
cout << "-------------------------------------------------" << endl;
system("CLS");
}
int main() {
srand(time(0));
string name;
int player;
int livesTracker = 5;
int x = 1 + (rand() % 10);
cout << "Hello, what is your name?" << endl;
cin >> name;
cout << "Welcome to guess the number, " << name << "!" << endl;
wrapUp;
cout << '\n' << "Number of Lives: " << livesTracker << endl;
for (int round = 1; round <= 3; round++) {
if (livesTracker == 0) {
system("CLS");
cout << "Game Over! Actual Answer: " << x << endl;
system("Color 4");
break;
}
cout << "Guess the number that will come up on the screen from 1 to 10" << endl;
cin >> player;
if (player == x) {
cout << "You won round " << round << "!" << endl;
x = (rand() & 10);
}
else if(player > x){
cout << "Your guess was too high, round" << round << "!" << endl;
wrapUp;
round--;
livesTracker--;
cout << '\n' << "Number of Lives: " << livesTracker << endl;
}
else {
cout << "Your guess was too low, round" << round << "!" << endl;
wrapUp;
round--;
livesTracker--;
cout << '\n' << "Number of Lives: " << livesTracker << endl;
}
if (round == 3) {
cout << "For the hard level, you have to guess a number from 15!" << endl;
for (int roundh = 1; roundh <= 2; roundh++) {
int xh = 1 + (rand() % 15);
cout << "Guess the number that will come up on the screen from 1 to 15" << endl;
cin >> player;
if (player == xh) {
cout << "You won round " << roundh << "!" << endl;
cout << "-------------------------------------------------" << endl;
xh = 1 + (rand() % 15);
}
else if (player > xh) {
cout << "Your guess was too high, round " << round << "!" << endl;
wrapUp;
roundh--;
cout << '\n' << "Number of Lives: " << livesTracker << endl;
}
else {
cout << "Your guess was too low, round " << round << "!" << endl;
wrapUp;
roundh--;
cout << '\n' << "Number of Lives: " << livesTracker << endl;
}
}
}
}
system("pause");
}
This program first creates a random number before going into the loop. A livesTracker
variable is then created to control the player's lives. The first loop runs until the player finishes the first round/ level. If the player gets the randomly generated number correct, a new random number will be assigned to x for the next round to start, until the player runs out of lives or wins the game, this will happen.
Just wanted to explain the program because its probably too messy to read, appreciate any help!
EDIT: I know its not good to clear the screen using system("CLS")
, but couldn't find any other options, any help on that would be helpful as well!