Corral.BAS
Click the gameplay window to enter commands.
Each round you can take 1-5 steps towards your horse to catch it. It'll bolt if you cover more than half the distance at once, and may kick if you get within 2 steps. Try not to get kicked.
The Listing
PRINT TAB(11); "**********************************************************"
PRINT TAB(11); "**********************************************************"
PRINT TAB(11); "** **"
PRINT TAB(11); "** CORRAL **"
PRINT TAB(11); "** **"
PRINT TAB(11); "** As seen in David Ahl's More BASIC Computer Games **"
PRINT TAB(11); "** Converted and refactored by Michael Coorlim **"
PRINT TAB(11); "** **"
PRINT TAB(11); "**********************************************************"
PRINT TAB(11); "**********************************************************"
PRINT:PRINT:PRINT
DIM A(21)
DIM S(2,9)
FOR I=1 to 2: FOR J = 0 to 9
Read S(I,J): NEXT J : NEXT I
DATA 0,1,2,3,3,2,2,1,0,-1
DATA 1,2,3,4,5,4,3,2,1,0
We initialize here. DIM sets the size of our arrays. The READ statement reads in our DATA, populating S's first two dimensions.
input "Do you want instructions?";F$
if lcase$(left$(F$,1))="y" then
? "You're a cowboy, and you need to catch your horse in the corral. Each turn you"
? "can take 1-5 steps towards your horse. If you move more than halfway towards him,"
? "he will bolt. He may also bolt when close to the rail."
?
? "And if you get within 2 steps, he may kick... so watch out!"
end if
? "When prompted, enter from 1 to 5 for your next move's steps."
I've rewritten the instructions to be more concise.
INIT:
C=1 :REM ## The Cowboy's X position
L=1
K=0 : REM ## Kick Counter
H=0 :REM ## The Horse's X position
N=0 : REM ## Turn Number
GOSUB HORSE_MOVES
More initialization. These variables will be reset if we play again.
if R>5 then Q=-Q
H=13+Q: GOSUB HORSE_CLAMP : REM #Set the Horse's positon
T=2+P: ?
Horse CLAMP makes sure the horse doesn't exit our playfield.
RESET_STATUS:
B$ = " "
POPULATE_A:
for J = 1 to 21: A(J)=32: NEXT J
A(C)=67:A(H)=72
? N,"I"; :REM ## Left Rail
For J = 1 to 21: ? CHR$(A(J));: NEXT J : REM ## Prints the playfield.
? "I",B$, :REM ## Right Rail and Status Message
X = ABS(H-C): L = SGN(H-C) : REM ## get the distance and direction of cowboy to horse
N=N+1: IF K>0 GOTO 640
IF N>100 THEN GOTO OUTATIME
Our primary loop begins with drawing our playfield, reading each element of the A array looking for a match to our Horse or Cowboy positions.
PROMPT:
input"Steps";D
if D<1 or D>5 THEN
? "You can take between 1 and 5 steps.",
GOTO PROMPT
END IF
E = C+L*D
IF E<1 OR E>21 THEN
? "You cannot leave the corral.",
GOTO PROMPT
END IF
Get player input and ensure that it's legal.
C = E: GOSUB HORSE_MOVES
G=P: H=H+L*G: GOSUB HORSE_CLAMP
IF X<2*D AND D >1 GOTO 570
IF H>1 and H<20 THEN 600
GOSUB HORSE_MOVES
IF R>2 GOTO 600
IF X>7 GOTO RESET_STATUS
570 G=9+2*P: H=H-L*G:L=-L: GOSUB HORSE_CLAMP
IF ABS(H-C)<2 THEN H=H-3*L:GOSUB HORSE_CLAMP
B$="BOLTED "
GOTO POPULATE_A
600 IF ABS(H-C)>2 GOTO RESET_STATUS
GOSUB HORSE_MOVES
IF R>3 GOTO CATCH_CHECK
GOSUB HORSE_MOVES
K=P+2:M=M+1:H=H-5*L:GOSUB HORSE_CLAMP
B$="KICKED": GOTO POPULATE_A
640 IF M>T GOTO HOSPITALIZED
K=K-1: ? : GOSUB HORSE_MOVES
H=H+L*(P+1):GOSUB HORSE_CLAMP: GOTO RESET_STATUS
More of our loop, checking for kicks and bolting if we get too close too fast.
CATCH_CHECK:
IF H=C THEN GOTO GOTTEM
GOTO RESET_STATUS
Did we catch the horse?
HORSE_MOVES:
R = int(rnd(1)*10)
P=S(1,R)
Q=S(2,R)
RETURN
HORSE_CLAMP:
REM ## MAKES SURE THE HORSE STAYS IN THE PLAYFIELD ##
IF H<1 THEN H = 1
if H>21 THEN H =21
RETURN
Give our horse its movement for the round.
HOSPITALIZED:
?:? "That last kick lands you in the hospital!"
? " Get well soon, champ!"
GOTO PLAY_AGAIN:
OUTATIME:
?:?"That darn horse done give you the slip!"
GOTO PLAY_AGAIN
GOTTEM:
FOR J=1 to 21: A(J)=32: NEXT J:A(C)=35
? "I";
FOR J=1 to 21: ? CHR$(A(J));:NEXT J
?"I"
?:?"Congrats! You caught him! Now see if you can do it in fewer moves!"
PLAY_AGAIN:
input "Another roundup";F$
if lcase$(left$(F$,1))="y" then GOTO Init
END
Game over states and the play again query.