Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @echo off
- REM The following are comparators
- REM ==/EQU - Equal
- REM NEQ - Not Equal
- REM LSS - Less Than
- REM LEQ - Less Than Or Equal To
- REM GTR - Greater Than
- REM GEQ - Greater Than Or Equal To
- REM These can be used to compare two values at once, EXAMPLE:
- 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)
- :Begining
- REM Make sure that the 'Tries' and 'Hints' variables don't carry over from the last game (If they played a game before this)
- set /a Tries=0
- set /a Hints=0
- REM Set the title
- title Guessing Game!
- REM The 'cls' is to clear the console if they just played a game
- cls
- 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)
- color 07
- REM Prompt the user to give a number, then stores it as the variable "TopNumber"
- echo Let's play a guessing game, it'll be from 1 to...
- set /p TopNumber=
- 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
- REM I'm not exactly sure how it works, I just found it online :)
- SET "var="&for /f "delims=0123456789" %%i in ("%TopNumber%") do set var=%%i
- :NotANumber
- if defined var (echo That's not a number, try again. 1 to...
- set /p TopNumber= )
- SET "var="&for /f "delims=0123456789" %%i in ("%TopNumber%") do set var=%%i
- if defined var (goto :NotANumber)
- 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
- REM (Their number is less than 20, then set it again)
- if %TopNumber% LSS 20 (
- :NumberCheck
- echo %TopNumber%? Don't make it too easy, how about from 1 to...
- set /p TopNumber=
- )
- SET "var="&for /f "delims=0123456789" %%i in ("%TopNumber%") do set var=%%i
- if defined var (goto :NotANumber)
- REM The next line makes sure their new answer is above 20, if not, we ask them to change it until it is
- if %TopNumber% LSS 20 goto :NumberCheck
- REM Now that we have our number range, reset the title with the number range in it
- title Guessing Game! 1-%TopNumber%
- REM Choose a random number between 1 (That's the +1, otherwise it would start at 0) and the number they gave
- REM The random number selected is stored as a variable, which is used later
- set /a Number=%random% %% (%TopNumber% +1)
- REM if you want to be given the number at the start of the game (for testing purposes), remove 'REM' two lines down
- REM (It's put in the title!)
- REM title Guessing Game! 1-%TopNumber% [%Number%]
- REM This is an equation that sets a range for when the game gives out hints.
- REM The hint range is 10% of the total guessing range in either direction.
- 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
- set /a HintRange= %TopNumber% / 10
- 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.
- REM Now, tell the user the computer has thought of the number, then stores their guess as a variable
- cls
- echo Type any number to guess, and type "hint" for a hint.
- set /p Guess=I am thinking of a number 1-%TopNumber%, what is it?
- cls
- REM the following code compares their guess with the answer (or if they typed 'hint')
- :Compare
- 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
- set /a Tries+=1
- REM If the user wants a hint, goto :Hint
- if /i %Guess%==hint goto :Hint
- REM If the guess is the same as the number, goto :Correct
- if %Guess% == %Number% goto :Correct
- REM IF the guess is greater than the number, goto :Greater
- if %Guess% gtr %Number% goto :Greater
- REM If the guess is less than the number, goto :Less
- if %Guess% lss %Number% goto :Less
- REM If they requested a hint, give them a rough estimate and let them guess
- :Hint
- REM The variable 'Hints' is brought up by one to track how many hints they used
- set /a Hints+=1
- REM These next two lines just make it look a bit better
- cls
- color 07
- 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.
- set /a HintLow=%Number% - %HintRange% + %random% %%4 -4
- set /a HintHigh=%Number% + %HintRange% + %random% %%4 -4
- 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"
- REM 'HintHigh' is esentially the same
- REM These lines make sure that the hints don't go outside of the guessing range
- if %HintLow% leq 0 (
- set /a HintLow=0
- )
- if %HintHigh% geq %TopNumber% (
- set /a HintHigh=%TopNumber%
- )
- REM Now give the user an approximation of the general area where the number is
- REM After that, have them guess again, which brings them back to :Compare afterwards
- echo My number might be higher than %HintLow%, and it might be lower than %HintHigh%. :)
- set /p Guess=Guess:
- cls
- goto :Compare
- REM If they guessed too high, tell them and compare their new answer to the guess
- :Greater
- REM Set the color red since they got it wrong
- color 0C
- set /p Guess=You guessed too high! Guess again:
- goto :Compare
- REM If they guessed too low, tell them and compare their new answer to the guess
- :Less
- REM Set the color red since they got it wrong
- color 0C
- set /p Guess=You guessed too low! Guess again:
- goto :Compare
- REM If they guessed it exactly, tell them and ask if they want to play again
- :Correct
- cls
- REM Set the color green since they got it right
- color 0A
- REM Tell them they got it right, along with their game stats
- REM TriesGrammar and HintsGrammar is to make sure it doesn't say something like "1 Tries" or "10 Hint"
- REM These two blocks of code use "if else" statements. For this, it means
- REM "if 'tries' is greater than 1, set TriesGrammar to 'tries', but if it is not greater than 1 (else), set TriesGrammar to 'try'"
- REM the parenthesis are to organize the code. At the begining of the statement, an open parenthesis is used. Then at the end,
- REM a closed parenthesis is used. Since 'else' is another command to run, we use the parenthesis again.
- if %Tries% gtr 1 (
- set TriesGrammar=tries
- ) else (
- set TriesGrammar=try
- )
- if %Tries% == 0 set TriesGrammar=tries
- if %Hints% gtr 1 (
- set HintsGrammar=hints
- ) else (
- set HintsGrammar=hint
- )
- if %Hints% == 0 set HintsGrammar=hints
- REM Set the title back
- title Guessing Game! 1-%TopNumber%
- echo Yes! It is %Guess%! It only took you %Tries% %TriesGrammar% and %Hints% %HintsGrammar%.
- 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)
- set /p Again=Type yes to play again, type exit to exit.
- REM Check if they typed yes or exit
- REM Remember, the /i keeps it from being case-sensitive, so they can type "Yes" or "yes"
- if /i %Again% == yes goto :Begining
- if /i %Again% == exit exit
- REM In case they typed something other than "yes" or "exit", or nothing at all, exit
- exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement