# Random Number Generator game

#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!

• If you think the code is too messy to read, then you should clean it up to make it readable before posting it for a review. This would make it easier for your code to be reviewed, and avoid comments about the messiness of the code. (However, since you now have an answer, you should not edit the code in the question.) – 1201ProgramAlarm Feb 23 at 3:00
• Hey, this is was my first program that I created in MSX BASIC back in ... uh, let's just say that it was a new VG-8235 :) – Maarten Bodewes Feb 23 at 3:57

I have some suggestions to improve your code:

## Formatting

• You should use empty lines consistently. E.g. you have some ifs/else ifs blocks where you put an empty line at the beginning but not everywhere. I suggest you to not use empty lines there at all.
• You should not use more than two empty lines to seperate things. It's harder to read.
• You should put empty lines between logically grouped things. E.g. your #include statements, the using statement and the wrapUp function are glued together.
• In general try to group things visually that belong together. In the wrapUp function the two empty lines are seperating the ending curly bracket from the function for no reason.
• You should not put a newline as first thing you print in a line, e.g. cout << '\n' << "Number of Lives: " << livesTracker << endl;. And you should not mixup '\n' and endl.

## Naming

• You should name variables more precisely. player should be named playerGuess. This will make it much clearer what the variable is for. You may ask why this is relevant because you know it already but be aware of other developers. Just by putting that Guess at the end will remove the amount of time someone has to put in finding out stuff. Some other variables could use better naming as well, like roundh or livesTracker.
• I think wrapUp does not describe well enough what it's doing. (That one is kinda personal taste.)

## Functional

• It's strange to put the game logic in a for loop and decrement the iterator to continue with the game. I know, I know, it works, but this is not the usual way to use a for loop. A while loop does fit here much better. When you connect the while loop to the amount of lifes left, you will have a much more readable code, e.g. while (amountOfLifes > 0) {// continue with the game}.
• The game logic for the normal game and the hard mode is very similar. You can put it in a method and use parameters, e.g. to individualize the range for the random number to guess.

Here is an example of how the program could look like when you apply my suggestions. It doesn't need to be exactly like that. Please note that I didn't double check everything and I changed some of the logic but I hope you get an overall idea about what is improvable:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

void printSeperator() {
cout << "-------------------------------------------------" << endl;
}

bool guess(int& lifesCounter, int numberToGuess, int round, int upperRandomLimit) {
cout << "Guess the number that will come up on the screen from 1 to " << upperRandomLimit << ": ";
int playerGuess;
cin >> playerGuess;

if (playerGuess == numberToGuess) {
cout << "You won round " << round << "!" << endl;
printSeperator();
return true;
}
else if (playerGuess > numberToGuess) {
cout << "Your guess was too high, round " << round << "!" << endl;
printSeperator();
lifesCounter--;
cout << "Number of Lives: " << lifesCounter << endl;
}
else {
cout << "Your guess was too low, round " << round << "!" << endl;
printSeperator();
lifesCounter--;
cout << "Number of Lives: " << lifesCounter << endl;
}
return false;
}

bool playRounds(int& lifesCounter, int upperRandomLimit) {
int numberToGuess;
for (int round = 1; round < 3; round++) {
numberToGuess = 1 + (rand() % upperRandomLimit);
while (!guess(lifesCounter, numberToGuess, round, upperRandomLimit)) {
if (lifesCounter == 0) {
cout << "Game Over! Actual Answer: " << numberToGuess << endl;
return false;
}
}
}
return true;
}

int main() {
srand(time(0));
string playerNamer;
int lifesCounter = 5;

cout << "Hello, what is your name? ";
cin >> playerNamer;
cout << "Welcome to guess the number, " << playerNamer << "!" << endl;
printSeperator();
cout << "Number of lifes you have: " << lifesCounter << endl;

if (!playRounds(lifesCounter, 10)) {
return 0;
}
lifesCounter += 5;
cout << "You got more lifes! Now you have: " << lifesCounter << endl;
cout << "For the hard level, you have to guess a number from 15!" << endl;
playRounds(lifesCounter, 15);
return 0;
}



P.S.: It's very hard to win when you have just 5 lifes for all 4 guesses. ¯\_(ツ)_/¯

• Ill look into this later, thanks for the help! Oh and btw I changed the 5 lives thing to 10 while playing around with my program :) – Totti Feb 23 at 6:00