need help with a batch file

MartinC.

Baseband Member
Messages
56
Okay, I have a batch file that I was making, and I want it to put down a random number, so that I can make it preform a random command. I know you can use the %random%. But I want i to generate either a 1 or a 2.
Not a 1 2 or any other number to 3 thousand something like the random command does. Here is an example of what I want to do. (below).
Now how do I make it either go to pie or cheese (at random).
If you don't understand what I'm asking, please ask me to restate it in
different words. Thanks in advance :)

@ECHO OFF
CLS
:Title
ECHO.
echo
ECHO.
pause
GOTO cheese
GOTO pie

@ECHO OFF
CLS
:cheese
ECHO.
echo
ECHO.
pause

@ECHO OFF
CLS
:pie
ECHO.
echo
ECHO.
pause
 
%random% will always generate a random number between 0 and 32767. If you just want two choices, then split the range and use it like that. So you'd do your set myrand = %random%, then say if myrand<(32767/2) goto cheese, else goto pie.

As an aside, please don't use GOTO unless you really have to... it's not going to do wonders to your coding skills and in any modern language it's very, very frowned upon (in fact in some languages like Java it was considered so awful they took it out altogether!)
 
What else can I use?
Also, why is it frowned upon?
And also, this is a little of topic, but do you know how to make 1 line one color and another line another color. Or is this
even possible in a batch file. Here is an example of what I want to do(below).

------------------------------------
edit---
------------------------------------
echo HI - I want this to be green
echo I AM A COMPUTER -and this to be blue

I applied that technique with my code (below) and it seems like it almost always goes to adder... Am I doing something wrong?

@ECHO OFF
CLS
:NUMBERGROW-setyournumber
SET yournumber= 150

@ECHO OFF
CLS
Color 09
:NUMBERGROW-startscreen
ECHO.
echo Numbergrow -made by MaRtIn C. iN 2oo9
echo Try to grow your number to 5000 to win
echo and make sure it stays above 50 or you lose.
echo You start at 100 and You can type in 25 50 75 100 125 150
echo 175 0r 200. The number you type in will either be added
echo or subtracted from your number.
ECHO.
SET /P command1= Type in s to start the game or c to adjust the color and then press ENTER=
IF %command1%== s GOTO NUMBERGROW-play
IF %command1%== c GOTO NUMBERGROW-color

@ECHO OFF
CLS
:WALL1
GOTO NUMBERGROW-startscreen

@ECHO OFF
CLS
:NUMBERGROW-color
ECHO.
echo Type in 2 letters/numbers and then press ENTER
echo Color Key
echo 0= Black 2= Green 4= Red 6= Yellow 8= Gray
echo 1= Blue 3= Aqua 5= Purple 7= White 9= Light Blue
echo.
echo A= Light Green C= Light Red E= Light Yellow
echo B= Light Aqua D= Light Purple F= Bright Blue
echo The first letter/number of what you type in is the background
echo color, and the second letter/number is the foreground color/text color.
echo Example: 01 makes the background color BLACK and the foreground color BLUE.
ECHO.
SET /P colorcode= Type in 2 letters/numbers and then press ENTER=
GOTO NUMBERGROW-chbsmessage

@ECHO OFF
CLS
:NUMBERGROW-chbsmessage
ECHO.
echo The color has been set.
ECHO.
pause
Color %colorcode%

@ECHO OFF
CLS
:NUMBERGROW-play
ECHO.
echo Your number is%yournumber%
echo You can type in 25 50 75 100 125 150 175 0r 200
echo Try to get to 5000 to win and stay above 50 or you lose.
ECHO.
SET /P toolnumber= Type in a number or letter and then press ENTER=
IF %toolnumber%== 25 GOTO Randomizer
IF %toolnumber%== 50 GOTO Randomizer
IF %toolnumber%== 75 GOTO Randomizer
IF %toolnumber%== 100 GOTO Randomizer
IF %toolnumber%== 125 GOTO Randomizer
IF %toolnumber%== 150 GOTO Randomizer
IF %toolnumber%== 175 GOTO Randomizer
IF %toolnumber%== 200 GOTO Randomizer

@ECHO OFF
CLS
:Invalidtoolnumber
GOTO NUMBERGROW-play

@ECHO OFF
CLS
:Randomizer
myrand = %random% if myrand<(32767/2) goto adder, else goto subtracter

@ECHO OFF
CLS
:adder
SET /A yournumber= %yournumber%+%toolnumber%
GOTO WLC

@ECHO OFF
CLS
:subtracter
SET /A yournumber= %yournumber%-%toolnumber%
GOTO WLC

@ECHO OFF
CLS
:WLC
IF %yournumber%== 5000 GOTO NUMBERGROW-Win
IF %yournumber%== 5025 GOTO NUMBERGROW-Win
IF %yournumber%== 5050 GOTO NUMBERGROW-Win
IF %yournumber%== 5075 GOTO NUMBERGROW-Win
IF %yournumber%== 5100 GOTO NUMBERGROW-Win
IF %yournumber%== 5125 GOTO NUMBERGROW-Win
IF %yournumber%== 5150 GOTO NUMBERGROW-Win
IF %yournumber%== 5175 GOTO NUMBERGROW-Win
IF %yournumber%== 5200 GOTO NUMBERGROW-Win
IF %yournumber%== 5225 GOTO NUMBERGROW-Win
IF %yournumber%== 5250 GOTO NUMBERGROW-Win
IF %yournumber%== 5275 GOTO NUMBERGROW-Win
IF %yournumber%== 5300 GOTO NUMBERGROW-Win
IF %yournumber%== 50 GOTO NUMBERGROW-Lose
IF %yournumber%== 25 GOTO NUMBERGROW-Lose
IF %yournumber%== 0 GOTO NUMBERGROW-Lose
IF %yournumber%== -25 GOTO NUMBERGROW-Lose
IF %yournumber%== -50 GOTO NUMBERGROW-Lose
IF %yournumber%== -75 GOTO NUMBERGROW-Lose
IF %yournumber%== -100 GOTO NUMBERGROW-Lose
IF %yournumber%== -125 GOTO NUMBERGROW-Lose
IF %yournumber%== -150 GOTO NUMBERGROW-Lose
IF %yournumber%== -200 GOTO NUMBERGROW-Lose
IF %yournumber%== -225 GOTO NUMBERGROW-Lose
IF %yournumber%== -250 GOTO NUMBERGROW-Lose
IF %yournumber%== -275 GOTO NUMBERGROW-Lose
IF %yournumber%== -300 GOTO NUMBERGROW-Lose

@ECHO OFF
CLS
:NUMBERGROW-Continue
GOTO NUMBERGROW-play

@ECHO OFF
CLS
:NUMBERGROW-Win
ECHO.
echo YOU WIN!!!
ECHO.
pause
GOTO EOF

@ECHO OFF
CLS
:NUMBERGROW-Lose
ECHO.
echo YOU LOSE
ECHO.
pause
GOTO EOF
 
What else can I use?
Also, why is it frowned upon?

I'm not particularly sure with batch files, it might be your only option for some things. That said I really think you'd be far better investing your time in a modern language that'll have far more use behind it!

It's frowned upon in most languages simply because it's hideous, awful and just shouldn't be used. All the uses of goto in any modern language can be replaced by methods, loops and recursion (and sometimes a mixture of all 3.) These structure code much more nicely than goto, make code easier to read and help keep your prorgam in overall better shape. If you find yourself wanting to use goto in a modern language instead of one of the above 3, then in 99.99% of cases it's because you've made a silly design decision elsewhere (and therefore you need to go back and do some refactoring.)

As for what's wrong with your code - it's a long time since I've done any work with batch files, and I don't know of anyone these days that spends any large amount of time with them. That said, looks like you might be missing a "set" on myrand? Not sure if that'll change anything though.

As for the colour thing, you change colour using the "color" command. So:
Code:
echo HI - I want this to be green
echo I AM A COMPUTER -and this to be blue

Would be:

Code:
color 2
echo HI
color 1
echo I AM A COMPUTER

However, can I please reiterate - WHY are you trying to spend lots of time getting things working in batch files?! Unless you've got a very specific task in mind that needs to be accomplished with a batch file, it seems rather pointless... you'd be far better going for a more modern, more widely used and far more useful language!
 
I have 2 reasons
1- I need to download something (The JDK) for the java, and I haven't done that yet because the download takes along time to complete (more than it says it will, it says 3 hours but is taking more than that). So, at the time I can only use batch... unless there is another one that I don't need to download anything for. Is there?
2- It is quick and easy to do, so I like to mess around with it at school sometimes.
 
This is in regard to MartinC. code.

all it does is add it doesn't subtract. so I had to win :(
 
Yeah, I know, I'm not sure why it's doing that. But I've given up on making it
work.
 
Why I was looking forward to spending countless hours playing it :-(
Please continue.
 
Back
Top Bottom