Advertisement
Guest User

Untitled

a guest
Aug 13th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 7.14 KB | None | 0 0
  1. @echo off
  2.  
  3. REM The following are comparators
  4.  
  5. REM ==/EQU -    Equal
  6. REM NEQ -       Not Equal
  7. REM LSS -       Less Than
  8. REM LEQ -       Less Than Or Equal To
  9. REM GTR -       Greater Than
  10. REM GEQ -       Greater Than Or Equal To
  11.  
  12.  
  13.  
  14. REM These can be used to compare two values at once, EXAMPLE:
  15.  
  16.  
  17.  
  18.  
  19. REM it's always nice to have a start label, especially if you want to replay the file (Like we give the option to at the end)
  20. :Begining
  21. REM Make sure that the 'Tries' and 'Hints' variables don't carry over from the last game (If they played a game before this)
  22. set /a Tries=0
  23. set /a Hints=0
  24. REM Set the title
  25. title Guessing Game!
  26. REM The 'cls' is to clear the console if they just played a game
  27. cls
  28. REM Make sure the color is white when the game starts (It's good to change colors when the console is clear, otherwise it looks weird)
  29. color 07
  30.  
  31. REM Prompt the user to give a number, then stores it as the variable "TopNumber"
  32. echo Let's play a guessing game, it'll be from 1 to...
  33. set /p TopNumber=
  34.  
  35.  
  36. REM Check to make sure they put in a number, this just sees if it's made of a combination of the numbers 1-9
  37. REM I'm not exactly sure how it works, I just found it online :)
  38. SET "var="&for /f "delims=0123456789" %%i in ("%TopNumber%") do set var=%%i
  39. :NotANumber
  40. if defined var (echo That's not a number, try again. 1 to...
  41.                 set /p TopNumber= )
  42. SET "var="&for /f "delims=0123456789" %%i in ("%TopNumber%") do set var=%%i
  43. if defined var (goto :NotANumber)
  44.  
  45. REM To keep the user from making it too easy, if they set the top number below 20, the game will tell them that's not good enough
  46. REM (Their number is less than 20, then set it again)
  47.  
  48. if %TopNumber% LSS 20 (
  49. :NumberCheck
  50.     echo %TopNumber%? Don't make it too easy, how about from 1 to...
  51.     set /p TopNumber=
  52. )
  53. SET "var="&for /f "delims=0123456789" %%i in ("%TopNumber%") do set var=%%i
  54. if defined var (goto :NotANumber)
  55. REM The next line makes sure their new answer is above 20, if not, we ask them to change it until it is
  56. if %TopNumber% LSS 20 goto :NumberCheck
  57.  
  58.  
  59. REM Now that we have our number range, reset the title with the number range in it
  60. title Guessing Game! 1-%TopNumber%
  61. REM Choose a random number between 1 (That's the +1, otherwise it would start at 0) and the number they gave
  62.  
  63. REM The random number selected is stored as a variable, which is used later
  64. set /a Number=%random% %% (%TopNumber% +1)
  65. REM if you want to be given the number at the start of the game (for testing purposes), remove 'REM' two lines down
  66. REM (It's put in the title!)
  67.  
  68. REM title Guessing Game! 1-%TopNumber% [%Number%]
  69.  
  70.  
  71.  
  72.  
  73. REM This is an equation that sets a range for when the game gives out hints.
  74. REM The hint range is 10% of the total guessing range in either direction.
  75. REM For example, if the number was 48 and they were playing 1-100, the hint range is 38-50. It's randomized later to keep them from just putting the number inbetween
  76. set /a HintRange= %TopNumber% / 10
  77. REM It's just an equation saying "Take our top number and divide it by ten". Farther down (around :Hint), It's added to one variable, and subtracted from another.
  78.  
  79.  
  80. REM Now, tell the user the computer has thought of the number, then stores their guess as a variable
  81. cls
  82. echo Type any number to guess, and type "hint" for a hint.
  83. set /p Guess=I am thinking of a number 1-%TopNumber%, what is it?
  84. cls
  85.  
  86.  
  87.  
  88. REM the following code compares their guess with the answer (or if they typed 'hint')
  89. :Compare
  90. REM To give stats at the end of the game, each time their guess is compared to the number (meaning they JUST guessed), the variable 'Tries' is brought up by one
  91. set /a Tries+=1
  92.  
  93.  
  94. REM If the user wants a hint, goto :Hint
  95. if /i %Guess%==hint goto :Hint
  96.  
  97. REM If the guess is the same as the number, goto :Correct
  98. if %Guess% == %Number% goto :Correct
  99.  
  100. REM IF the guess is greater than the number, goto :Greater
  101. if %Guess% gtr %Number% goto :Greater
  102.  
  103. REM If the guess is less than the number, goto :Less
  104. if %Guess% lss %Number% goto :Less
  105.  
  106.  
  107. REM If they requested a hint, give them a rough estimate and let them guess
  108. :Hint
  109. REM The variable 'Hints' is brought up by one to track how many hints they used
  110. set /a Hints+=1
  111.  
  112. REM These next two lines just make it look a bit better
  113. cls
  114. color 07
  115.  
  116. REM Since they want a hint, not an equation to find the answer, a random value is added to the hint range, between -4 and 4.
  117. set /a HintLow=%Number% - %HintRange% + %random% %%4 -4
  118. set /a HintHigh=%Number% + %HintRange% + %random% %%4 -4
  119. REM The written equation for 'HintLow' is "The thought-of number, minus 10% of the total guessing range, plus a random value of -4 to 4"
  120. REM 'HintHigh' is esentially the same
  121.  
  122.  
  123. REM These lines make sure that the hints don't go outside of the guessing range
  124. if %HintLow% leq 0 (
  125. set /a HintLow=0
  126. )
  127.  
  128. if %HintHigh% geq %TopNumber% (
  129. set /a HintHigh=%TopNumber%
  130. )
  131.  
  132. REM Now give the user an approximation of the general area where the number is
  133. REM After that, have them guess again, which brings them back to :Compare afterwards
  134. echo My number might be higher than %HintLow%, and it might be lower than %HintHigh%. :)
  135. set /p Guess=Guess:
  136. cls
  137. goto :Compare
  138.  
  139.  
  140.  
  141.  
  142.  
  143. REM If they guessed too high, tell them and compare their new answer to the guess
  144. :Greater
  145. REM Set the color red since they got it wrong
  146. color 0C
  147. set /p Guess=You guessed too high! Guess again:
  148. goto :Compare
  149.  
  150.  
  151.  
  152.  
  153.  
  154. REM If they guessed too low, tell them and compare their new answer to the guess
  155. :Less
  156. REM Set the color red since they got it wrong
  157. color 0C
  158. set /p Guess=You guessed too low! Guess again:
  159. goto :Compare
  160.  
  161.  
  162.  
  163.  
  164.  
  165. REM If they guessed it exactly, tell them and ask if they want to play again
  166. :Correct
  167. cls
  168. REM Set the color green since they got it right
  169. color 0A
  170.  
  171.  
  172. REM Tell them they got it right, along with their game stats
  173. REM TriesGrammar and HintsGrammar is to make sure it doesn't say something like "1 Tries" or "10 Hint"
  174.  
  175. REM These two blocks of code use "if else" statements. For this, it means
  176. REM "if 'tries' is greater than 1, set TriesGrammar to 'tries', but if it is not greater than 1 (else), set TriesGrammar to 'try'"
  177.  
  178. REM the parenthesis are to organize the code. At the begining of the statement, an open parenthesis is used. Then at the end,
  179. REM a closed parenthesis is used. Since 'else' is another command to run, we use the parenthesis again.
  180.  
  181. if %Tries% gtr 1 (
  182. set TriesGrammar=tries
  183. ) else (
  184. set TriesGrammar=try
  185. )
  186.  
  187. if %Tries% == 0 set TriesGrammar=tries
  188.  
  189.  
  190. if %Hints% gtr 1 (
  191.  set HintsGrammar=hints
  192. ) else (
  193. set HintsGrammar=hint
  194. )
  195. if %Hints% == 0 set HintsGrammar=hints
  196.  
  197. REM Set the title back
  198. title Guessing Game! 1-%TopNumber%
  199.  
  200.  
  201. echo Yes! It is %Guess%! It only took you %Tries% %TriesGrammar% and %Hints% %HintsGrammar%.
  202.  
  203. REM Ask them if they want to play again, and if they do, go to the begining (Where values are reset and the screen is cleared)
  204. set /p Again=Type yes to play again, type exit to exit.
  205. REM Check if they typed yes or exit
  206. REM Remember, the /i keeps it from being case-sensitive, so they can type "Yes" or "yes"
  207. if /i %Again% == yes goto :Begining
  208. if /i %Again% == exit exit
  209.  
  210.  
  211. REM In case they typed something other than "yes" or "exit", or nothing at all, exit
  212. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement