Echo
Go back to the CLI window and type in the following:
echo Hello
What happened when you hit enter? The echo command is used to send information to the CLI window; in our batch files we will use it as a command to have information printed for the user to see.
In a batch file, by default, echo is turned on, which means that every single line that we put in our batch file will be shown in the output window. We don't want that, we only want the window to show specific information. Therefore, we need to change our program is turn echo off, and this should be the first line in our program.
We can do this by adding the following command to our program:
@echo off
Your batch file should now look like this:
Run your program again, and see what happens.
Labels
We are going to want to repeat parts of our program. As an example we are going to want to ask the user for a guess up to 6 times. We can return to sections of code in two ways, by using loops and by using goto statements.
NOTE: We normally avoid using goto statements in high-level programming languages, but we will use goto statements today, in our batch file, because it is a very simple file.
A goto to statement allows you to change the flow of a program by having the computer skip to another section of the program. You must label a section of a program if you would like to goto (literally "go to") that section. Let's add a comment and a label to the top of our program. You already know how to make a comment, and you make a label by using a colon.
Make the start of your program look like this:
In the file above we have added a new comment which declares the start of the program, and we have created a label called startOfProgram as well. In the future, we will be able to make the computer return to this part of the file by using the statement:
goto startOfProgram
Do not add such a goto statement to your batch file at this time.
Variables
A variable is a name and value pair. For example:
x = 4
says that there exists something name x which has the value of 4. We are going to need 3 variables for our program.
secretNumber -> the number the computer picked
guess -> the number that the player is using as a guess
counter -> the number of guesses the player has made
You can create a variable using the set command. Add the following lines to your program:
set counter=0
set secretNumber=0
set guess=0
What did you just do? What are the names and values of each of your variables?
Sending output to the CLI window
The game will need to ask the player questions and tell the player if their answer is correct or wrong. In short our batch file needs to be able to send information to the screen. We have turned echo off so be default none of the lines in our file will appear in the CLI window when the batch file is run. We can however deliberately send information to the CLI window by using the echo command…. again.
Make your batch file look like this:
Now run your program (batch file) by going to the CLI window and typing mygame.bat
What happened? You should have seen something like this:
Our program first turns of echoing, so that the actual commands we have in our batch file are not printed to the screen. We then have two comments and a label, all of which have no output. Then we create our 3 variables using the set command. What were their names and values again?
Then we have our first echo command. What is printed to the screen is the number 0. Why? The name of our first variable is counter but its value is 0. When we used the echo command we surrounded counter with % signs. Theses % signs tell the computer that we want to echo the value of the variable and not the name.
After echoing our 3 variable values we also echo 5 lines which create a "Title" for our game. Notice the last line which has an echo followed by a period. That line creates an empty echo line. The period literally says echo EXACTLY what is written, and what is written is an empty line. If we use the keyword echo and follow it with an empty line, we will get the value of echo (false) which is not what we want.
We are going to need to echo variable values in our program and we will do that using the % sign (formally the % is then known as an "escape key" or "escape sequence" as it specifies a modifier on what should be displayed). But for now remove the 3 echo lines that print out 0's (the 3 echo variable lines).
Special Functions
Our secret number is currently 0. That won't do. What we need is for our secret number to be a random value between 0 and 100. The windows CLI has a special function called "random" which can generate random numbers, unfortunately it will generate a random number between 0 and 4,294,967,295 ( which equals to 232 ) and we don't need numbers that large. So what we can do is use the special function modulus to take an random numbers that are too large and reduce them so that they are between 0 and 100.
Add the following lines of code to the bottom of your program:
:playAgain
set /a secretNumber = %random% %% 101
set counter=0
echo I am thinking of a number between 0 and 100.
echo Can you guess the number in 6 tries or less?
echo.
It should be clear to you that the first line is simply creating a new label (named playAgain). The 3rd line is setting the counter variable value to 0 and the last 3 lines are simply printing two sentences and an empty line to the screen. The second line may be a bit confusing; it is enough for now to know that it is setting the secretNumber variable to a random number between 0 and 100 (the /a following set, means set the value equal to an arithmetic equation, ask your instructor for help in understanding the rest of the line.)
Getting input from the user
In the last section we set our secretNumber. Now we need to get a guess value from the user. In the last section we used /a (a command line operator) in combination with the set command to make a variable equal to an arithmetic equation. In this section we will use the /p command line operator to set a value equal to the result of input from a prompt.
Add the following lines to your program:
:guess
set /p guess="Enter your number (0-100) : "
What do you think those lines do? Run your program. What happened?
Selection
In the last two sections we created a random value for the variable secretNumber and we asked the user to submit a value and assigned that value to the variable guess. Now we want to compare the secretNumber and guess variables to see if they are the same. If they are the same then the player has won (and we should tell them so). Add the following lines to your program:
if %guess%==%secretNumber% goto :win
goto :guess
:win
echo Congratulation, you win. Thanks for playing!
What does each of the lines above do? What would happen if we didn't put those % symbols around the variable names?
Now run your program. What happened?
NOTE: To stop a running batch program, hold down the CTRL and C buttons at the same time.
Repetition (counter controlled)
It may be clear to you at this point, that the lines we added in the last section implemented repetition. If the users guess is not equal to the secret number then they are sent back to the section of code that has the label guess, and are asked to enter another number again. The program you have now will loop until the player guess the correct number (or you stop it by hitting ctrl+c).
Let's change that so that the player only gets 6 guesses. We already have a variable called counter. What we need to do is add one to the counter variable after each bad guess, and after 6 bad guesses send the player to a "you lose" labeled section of code.
Make the bottom of your program look like this (new lines are added in bold):
if %guess%==%secretNumber% goto :win
set /a counter=%counter%+1
if %counter%==6 goto :lose
goto :guess
:lose
echo Sorry, you lose.
:win
echo Congratulation, you win. Thanks for playing!
What does each of the NEW lines above do? What would happen if we didn't put those % symbols around the variable names?
Now run your program. What happened?
Finishing up
We're almost done. You should have noticed two things when you ran your program: (1) It's very hard to win and (2) After you get the "Sorry, you lose" message then you also get the "Congratulations, you win message".
Let's tackle the second problem first. Remember that the SEQUENCE of a batch file is LRTD, and so after we leap to the lose label, the program continues processing the batch statements that follow, including our "you win" statement. What we need to do is have the program leap to the end of the program after printing you lose. Can you see how to do that?
CHALLENGE 1: Fix your program so that only the one correct message, you win or you lose is displayed when a player wins or loses. Hint: You will need to put a label at the end of the document.
Table I. Comparison operators in"If' statements
|
Operator
|
Meaning
|
EQU
|
equal to
|
NEQ
|
not equal to
|
LSS
|
less than
|
LEQ
|
less than or equal to
|
GTR
|
greater than
|
GEQ
|
greater than or equal to
| Now let's look at the second problem. The game is hard because you aren't told whether or not your guesses are good or bad.
CHALLENGE 2: Fix your program so that if a players guesses wrongly, they are immediately told whether their guess was too high or too low, before they are asked to guess again. Hint: Use the operators from the table on the right. ( if %guess% GTR %secretNumber% echo … )
CHALLENGE 2: Right now the program automatically ends if the player guesses correctly, of if the player runs out of guesses. Modify the program so that the player can "play again" if the wish.
Hint: Create a variable called "playAgain". Use the set /p command to ask the player to enter y or n.
http://bridges.brooklyn.cuny.edu
Share with your friends: |