Hey everyone. I'm quite new to programming and need some help. I'm not going to my exact code to prevent getting in trouble.
I'm trying to make a game-type program. The computer creates a random number between 1 and 100 (randNum). The player enters their guess (guessNum). If/when the user enters the computer's number, then the player loses (yeah, backwards logic.) The computer asks the player for another guess and adjusts the limits.
If the computer states a limit that is the actual randNum, then the player wins. (Below, this would mean if the computer said "Enter a number between 38 and 40:" Since it had to use the randNum, the player wins.)
Example (if the randNum was 38):
Enter a number between 1 and 100.
16
Enter a number between 17 and 100:
75
Enter a number between 17 and 74:
.....and so on and so on.....
As you can see, the limit changes, +1 or -1, depending on the guess.
Problem: The game needs to keep going until the player wins or the computer wins, meaning it's not count-controlled. I can't figure out which type of loop/s to use. I know I need at least 1 loop, but probably 2 or more.
-Loop back to beginning if the player wants to play again
-Loop used to keep asking the player to enter a number, check validity, etc...
*The code below probably isn't in the right order. I know it will change order depending on which kind of loop/s it needs.
Here is the stuff I know I need. I used "if" statements because that's my logic behind it. I don't know how to tie it all together using a loop or multiple loops:
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
|
if (guess == randNum) //player guesses the randNum. Player loses.
{
cout << "That's my number! Play again? [Y/N]" << endl;
cin >> playAgain;
}
if (lowLimit > guessNum || guessNum > highLimit) //if player enters a number outside of the limits
{
cout << "You must enter a number between 1 and 100!" << endl;
cout << "Press any key to continue..." << endl;
return 1; //Kicks them from the program
}
if (guess < randNum)
{
cout << "Enter a number between " << guess + 1 << " and " << highLimit << endl;
cin >> guess;
lowNum = guess + 1;
}
if (guess > randNum)
{
cout << "Enter a number between " << lowLimit << " and " << guess - 1 << endl;
highNum = guess - 1;
}
if (guess == highLimit || guess == low) //player's guess is one of the limits. The computer wins
{
cout << randNum << " is my number. You win!" << endl;
}
|
Thank you in advance! I apologize it's so long and if I need to specify anything, then just ask.