Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 12-02-2009, 09:44 PM   #1
Baseband Member
 
MartinC.'s Avatar
 
Join Date: Nov 2009
Posts: 56
Question need help with a batch file

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
ie
ECHO.
echo
ECHO.
pause
__________________

__________________
all your base are belong to us
MartinC. is offline   Reply With Quote
Old 12-03-2009, 04:20 AM   #2
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: need help with a batch file

%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!)
__________________

berry120 is offline   Reply With Quote
Old 12-03-2009, 08:53 PM   #3
Baseband Member
 
MartinC.'s Avatar
 
Join Date: Nov 2009
Posts: 56
Default Re: need help with a batch file

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
__________________
all your base are belong to us
MartinC. is offline   Reply With Quote
Old 12-04-2009, 04:59 AM   #4
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: need help with a batch file

Quote:
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!
berry120 is offline   Reply With Quote
Old 12-04-2009, 08:50 PM   #5
Baseband Member
 
MartinC.'s Avatar
 
Join Date: Nov 2009
Posts: 56
Default Re: need help with a batch file

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.
__________________
all your base are belong to us
MartinC. is offline   Reply With Quote
Old 12-23-2009, 03:05 PM   #6
Baseband Member
 
thevil1's Avatar
 
Join Date: Dec 2009
Posts: 36
Default Re: need help with a batch file

This is in regard to MartinC. code.

all it does is add it doesn't subtract. so I had to win
__________________
An optimist is someone who falls off the Empire State Building, and after 50 floors says, ''So far so good!''
thevil1 is offline   Reply With Quote
Old 12-23-2009, 04:47 PM   #7
Baseband Member
 
MartinC.'s Avatar
 
Join Date: Nov 2009
Posts: 56
Default Re: need help with a batch file

Yeah, I know, I'm not sure why it's doing that. But I've given up on making it
work.
__________________
all your base are belong to us
MartinC. is offline   Reply With Quote
Old 12-23-2009, 05:08 PM   #8
Baseband Member
 
thevil1's Avatar
 
Join Date: Dec 2009
Posts: 36
Default Re: need help with a batch file

Why I was looking forward to spending countless hours playing it :-(
Please continue.
__________________
An optimist is someone who falls off the Empire State Building, and after 50 floors says, ''So far so good!''
thevil1 is offline   Reply With Quote
Old 12-23-2009, 05:27 PM   #9
Fully Optimized
 
vampist's Avatar
 
Join Date: Oct 2008
Location: USA
Posts: 2,404
Default Re: need help with a batch file

SET /A x=%RANDOM%%%2%

Outputs 1 or 0


Enjoy.
__________________
Everyone's Favorite Turd xD
ET: "Phone home!"
Geek: "ping 127.0.0.1"

"If that guy knew half the $h*t that I know, his fuzzy little head would explode. " - Matthew Farrell
vampist is offline   Reply With Quote
Old 12-23-2009, 08:04 PM   #10
Baseband Member
 
thevil1's Avatar
 
Join Date: Dec 2009
Posts: 36
Default Re: need help with a batch file

wait were did you put it I wanna play
__________________

__________________
An optimist is someone who falls off the Empire State Building, and after 50 floors says, ''So far so good!''
thevil1 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 10:34 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0