Advertisement
Guest User

Untitled

a guest
Jan 29th, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. // Sandbox1.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <cstdlib>
  6. #include <iostream>
  7. #include <ctime>
  8. using namespace std;
  9. int _tmain(int argc, _TCHAR* argv[])
  10. {
  11. srand(time(0));
  12. int guess = 3; //Amount of attemps the player gets to guess (Default: 3)
  13. int answer = rand() % 10 + 1; //Generates randomnumber between 1-10
  14. cout << "============= Welcome ==============" << endl;
  15. cout << "This is a game made by Jimmy, aka Sweez." << endl;
  16. cout << "Your job is to guess the correct number," << endl;
  17. cout << "which will be randomgenerated between 1-10." << endl;
  18. cout << "You have a total of 3 guesses before you lose." << endl;
  19. cout << "============ Game Starts ===========\n" << endl;
  20. bool lopa1 = true; //Sets the variable lopa1 as TRUE
  21. while (lopa1 = true && guess >= 1) //Runs the code WHILE lopa1 is true AND the GUESS value is atleast 1
  22. {
  23. int nr; //It is the userinput where the user guess the number
  24. cout << "Guess what number I am thinking of between 1-10: "; //Output on the screen
  25. cin >> nr; //User Input based on the previous Output
  26.  
  27. if (nr == answer)//IF Number is equal, aka the same, as the answer(Randomized 1-10) of the question THEN
  28. {
  29. cout << "Congratulations! You managed to guess the correct number! " << endl; //Outputs the text on the screen saying that you won
  30. cout << endl << "Please press enter to continue... ";//Output
  31. cin.get();
  32. cin.get();//Waits for the user to press ENTER to continue
  33. break;//Breaks the loop after the user have pressed ENTER and exits the program
  34. }
  35. else if (nr == 1337) //Allows you to check the answer
  36. {
  37. cout << answer << endl; //Prints the answer on the screen
  38. }
  39. else if (nr >= 1 && nr != answer && nr <= 10) //ELSE IF the number(Userinput) is between 1-10 but NOT the answer THEN
  40. {
  41. guess--; //Substracts 1 from your GUESS and once it hits 0 you lose
  42. cout << "Wrong number! Please try again " << endl; //Output stating that its the wrong number
  43. if (guess >= 2) //If the amount of guesses you have left is equal or less than 2 THEN
  44. {
  45. cout << "You now have " << guess << " guesses left" << endl << endl; //Output
  46. }
  47. else if (guess == 1) //If the amount of guesses you have left is equal to 1 THEN
  48. {
  49. cout << "Warning! You now have 1 more guess before you lose" << endl << endl; //Output
  50. if (answer >= 1 && answer <= 5) //IF answer is between the interval (1-5) THEN
  51. {
  52. cout << "Hint: The answer is between 1-5" << endl; //Output
  53. }
  54. else if (answer > 5 && answer <= 10) //IF answer is between the interval (6-10) THEN
  55. {
  56. cout << "Hint: The answer is between 6-10" << endl; //Output
  57. }
  58. }
  59. else if (guess == 0)//If the amount of guesses you have left is equal to 0 THEN
  60. {
  61. cout << endl << "=====You lose=====" << endl; //Output
  62. cout << "The program will now close, please restart it to try again" << endl << endl; //Output
  63. cout << endl << "Please press ENTER to continue... "; //Output
  64. cin.get();
  65. cin.get();//Waits for the user to press ENTER to continue
  66. }
  67. }
  68. else //IF the UserInput is something not within the 1-10 range THEN
  69. {
  70.  
  71. guess--; //Substracts 1 from your GUESS and once it hits 0 you lose
  72. cout << "Invalid number! It has to be between 1-10!" << endl; //Output
  73.  
  74. if (guess >= 2) //If the amount of guesses you have left is equal or less than 2 THEN
  75. {
  76. cout << "You now have " << guess << " guesses left" << endl << endl; //Output
  77. }
  78. else if (guess == 1) //If the amount of guesses you have left is equal to 1 THEN
  79. {
  80. cout << "Warning! You now have 1 more guess before you lose" << endl << endl; //Output-Warns about having just 1 more try
  81. if (answer >= 1 && answer <= 5) //IF answer is between the interval (1-5) THEN
  82. {
  83. cout << "Hint: The answer is between 1-5" << endl; //Output
  84. }
  85. else if (answer > 5 && answer <= 10) //IF answer is between the interval (6-10) THEN
  86. {
  87. cout << "Hint: The answer is between 6-10" << endl; //Output
  88. }
  89. }
  90. else if (guess == 0)//If the amount of guesses you have left is equal to 0 THEN
  91. {
  92. cout << endl << "=====You lose=====" << endl; //Output stating that you lose, the program will now close due to the(Next line)
  93. //requirements for the loop is now no longer met
  94. cout << "The program will now close, please restart it to try again" << endl; //Output
  95. cout << "This program has been made by Jimmy Aka Sweez!" << endl; //Output
  96. cout << endl << "Please press ENTER to continue... "; //Output
  97. cin.get();
  98. cin.get();
  99. }
  100. }
  101. }//end lopa1
  102. return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement