Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sandbox1.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <cstdlib>
- #include <iostream>
- #include <ctime>
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[])
- {
- srand(time(0));
- int guess = 3; //Amount of attemps the player gets to guess (Default: 3)
- int answer = rand() % 10 + 1; //Generates randomnumber between 1-10
- cout << "============= Welcome ==============" << endl;
- cout << "This is a game made by Jimmy, aka Sweez." << endl;
- cout << "Your job is to guess the correct number," << endl;
- cout << "which will be randomgenerated between 1-10." << endl;
- cout << "You have a total of 3 guesses before you lose." << endl;
- cout << "============ Game Starts ===========\n" << endl;
- bool lopa1 = true; //Sets the variable lopa1 as TRUE
- while (lopa1 = true && guess >= 1) //Runs the code WHILE lopa1 is true AND the GUESS value is atleast 1
- {
- int nr; //It is the userinput where the user guess the number
- cout << "Guess what number I am thinking of between 1-10: "; //Output on the screen
- cin >> nr; //User Input based on the previous Output
- if (nr == answer)//IF Number is equal, aka the same, as the answer(Randomized 1-10) of the question THEN
- {
- cout << "Congratulations! You managed to guess the correct number! " << endl; //Outputs the text on the screen saying that you won
- cout << endl << "Please press enter to continue... ";//Output
- cin.get();
- cin.get();//Waits for the user to press ENTER to continue
- break;//Breaks the loop after the user have pressed ENTER and exits the program
- }
- else if (nr == 1337) //Allows you to check the answer
- {
- cout << answer << endl; //Prints the answer on the screen
- }
- else if (nr >= 1 && nr != answer && nr <= 10) //ELSE IF the number(Userinput) is between 1-10 but NOT the answer THEN
- {
- guess--; //Substracts 1 from your GUESS and once it hits 0 you lose
- cout << "Wrong number! Please try again " << endl; //Output stating that its the wrong number
- if (guess >= 2) //If the amount of guesses you have left is equal or less than 2 THEN
- {
- cout << "You now have " << guess << " guesses left" << endl << endl; //Output
- }
- else if (guess == 1) //If the amount of guesses you have left is equal to 1 THEN
- {
- cout << "Warning! You now have 1 more guess before you lose" << endl << endl; //Output
- if (answer >= 1 && answer <= 5) //IF answer is between the interval (1-5) THEN
- {
- cout << "Hint: The answer is between 1-5" << endl; //Output
- }
- else if (answer > 5 && answer <= 10) //IF answer is between the interval (6-10) THEN
- {
- cout << "Hint: The answer is between 6-10" << endl; //Output
- }
- }
- else if (guess == 0)//If the amount of guesses you have left is equal to 0 THEN
- {
- cout << endl << "=====You lose=====" << endl; //Output
- cout << "The program will now close, please restart it to try again" << endl << endl; //Output
- cout << endl << "Please press ENTER to continue... "; //Output
- cin.get();
- cin.get();//Waits for the user to press ENTER to continue
- }
- }
- else //IF the UserInput is something not within the 1-10 range THEN
- {
- guess--; //Substracts 1 from your GUESS and once it hits 0 you lose
- cout << "Invalid number! It has to be between 1-10!" << endl; //Output
- if (guess >= 2) //If the amount of guesses you have left is equal or less than 2 THEN
- {
- cout << "You now have " << guess << " guesses left" << endl << endl; //Output
- }
- else if (guess == 1) //If the amount of guesses you have left is equal to 1 THEN
- {
- cout << "Warning! You now have 1 more guess before you lose" << endl << endl; //Output-Warns about having just 1 more try
- if (answer >= 1 && answer <= 5) //IF answer is between the interval (1-5) THEN
- {
- cout << "Hint: The answer is between 1-5" << endl; //Output
- }
- else if (answer > 5 && answer <= 10) //IF answer is between the interval (6-10) THEN
- {
- cout << "Hint: The answer is between 6-10" << endl; //Output
- }
- }
- else if (guess == 0)//If the amount of guesses you have left is equal to 0 THEN
- {
- cout << endl << "=====You lose=====" << endl; //Output stating that you lose, the program will now close due to the(Next line)
- //requirements for the loop is now no longer met
- cout << "The program will now close, please restart it to try again" << endl; //Output
- cout << "This program has been made by Jimmy Aka Sweez!" << endl; //Output
- cout << endl << "Please press ENTER to continue... "; //Output
- cin.get();
- cin.get();
- }
- }
- }//end lopa1
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement