99 Bottles of Beer/Basic: Difference between revisions
(moving code from main task-page to sub-page) |
(moving code from main task-page to sub-page) |
||
Line 1: | Line 1: | ||
= |
=Task in Basic(s)= |
||
{{collection|99 Bottles of Beer}} [[implementation of task::99 Bottles of Beer| ]] |
{{collection|99 Bottles of Beer}} [[implementation of task::99 Bottles of Beer| ]] |
||
[[99 Bottles of Beer]] done in any of the BASIC-languages. |
[[99 Bottles of Beer]] done in any of the BASIC-languages. |
||
__toc__ |
|||
=={{header|BASIC}}== |
|||
==={{works with|QuickBASIC|4.5}}=== |
|||
====Sound==== |
|||
This version plays the tune 100 times while printing out the lyrics (not synchronized). |
|||
<lang qbasic>PLAY "<" |
|||
FOR x = 99 TO 0 STEP -1 |
|||
PRINT x; "bottles of beer on the wall" |
|||
PRINT x; "bottles of beer" |
|||
PRINT "Take one down, pass it around" |
|||
PRINT x-1; "bottles of beer on the wall" |
|||
PRINT |
|||
PLAY "e-8e-8e-8<b-8b-8b-8>e-8e-8e-8e-4"'X bottles of beer on the wall |
|||
PLAY "f8f8f8c8c8c8f4"'X bottles of beer |
|||
PLAY "d4d8d8 N0 d8d8d8d4"'take one down, pass it around |
|||
PLAY "<a+8a+8a+8>c8c8d8d+8d+8d+8d+4"'X-1 bottles of beer on the wall |
|||
NEXT x</lang> |
|||
====Text==== |
|||
<lang qbasic>FOR x = 99 TO 1 STEP -1 |
|||
PRINT x; "bottles of beer on the wall" |
|||
PRINT x; "bottles of beer" |
|||
PRINT "Take one down, pass it around" |
|||
PRINT x-1; "bottles of beer on the wall" |
|||
PRINT |
|||
NEXT x</lang> |
|||
==={{header|Applesoft BASIC}}=== |
|||
<lang Applesoft BASIC> |
|||
10 HOME |
|||
20 FOR B=99 TO 1 STEP -1 |
|||
30 PRINT B;" BOTTLE OF BEER ON THE WALL" |
|||
40 PRINT B;" BOTTLE OF BEER ON THE WALL" |
|||
50 PRINT "TAKE ONE DOWN, PASS IT AROUND" |
|||
60 PRINT B-1;" BOTTLE OF BEER ON THE WALL" |
|||
70 NEXT B |
|||
</lang> |
|||
==={{header|BASIC256}}=== |
|||
<lang BASIC256>#length of querter and eight note in ms |
|||
n4 = 1000 * 60 / 80 / 4 |
|||
n8 = n4 / 2 |
|||
#frequency of musical notes in hz |
|||
e = 330 |
|||
ef = 311 |
|||
b = 247 |
|||
bf = 233 |
|||
f = 349 |
|||
c = 262 |
|||
d = 294 |
|||
ds = 311 |
|||
a = 220 |
|||
dim notes(1) |
|||
dim lengs(1) |
|||
# redim is automatic when using a {} list to assign an array |
|||
notes = {ef, ef, ef, bf, bf, bf, ef, ef, ef, ef, f , f , f , c , c , c , f , d , d , d , d , d , d , d , bf, bf, bf, c , c , ef, ef, ef, ef, ef} |
|||
lengs = {n8, n8, n8, n8, n8, n8, n8, n8, n8, n4, n8, n8, n8, n8, n8, n8, n4, n4, n8, n8, n8, n8, n8, n4, n8, n8, n8, n8, n8, n8, n8, n8, n8, n4 } |
|||
for x = 99 to 1 step -1 |
|||
for t = 0 to notes[?]-1 |
|||
if t = 0 then print x + " bottles of beer on the wall" |
|||
if t = 11 then print x + " bottles of beer" |
|||
if t = 18 then print "Take one down, pass it around" |
|||
if t = 25 then print(x-1) + " bottles of beer on the wall" |
|||
sound notes[t], lengs[t] |
|||
pause .002 |
|||
next t |
|||
print |
|||
next x |
|||
</lang> |
|||
==={{header|BBC BASIC}}=== |
|||
<lang bbcbasic> |
|||
N_Bottles = 99 |
|||
beer$ = " of beer" |
|||
wall$ = " on the wall" |
|||
unit$ = "99 bottles" |
|||
WHILE N_Bottles >= 0 |
|||
IF N_Bottles=0 THEN |
|||
PRINT '"No more bottles" beer$ wall$ ", " unit$ beer$ "." |
|||
PRINT "Go to the store and buy some more, "; |
|||
ELSE |
|||
PRINT 'unit$ beer$ wall$ ", " unit$ beer$ "." |
|||
PRINT "Take one down and pass it around, "; |
|||
ENDIF |
|||
N_Bottles -= 1 |
|||
CASE N_Bottles OF |
|||
WHEN 0: |
|||
unit$ = "no more bottles" |
|||
WHEN 1: |
|||
unit$ = "1 bottle" |
|||
OTHERWISE: |
|||
unit$ = STR$((N_Bottles + 100) MOD 100) + " bottles" |
|||
ENDCASE |
|||
PRINT unit$ beer$ wall$ "." |
|||
ENDWHILE |
|||
END |
|||
</lang> |
|||
==={{header|Creative Basic}}=== |
|||
====window version==== |
|||
<lang Creative Basic>DEF Win:WINDOW |
|||
DEF Close:CHAR |
|||
DEF ScreenSizeX,ScreenSizeY:INT |
|||
DECLARE VSpace(Number:UINT) |
|||
DECLARE CLR() |
|||
DEF TheLine$[4],Number$,Erase:STRING |
|||
DEF TheLine,TextHeight,TextWidth:INT |
|||
DEF TextX,TextY:UINT |
|||
TheLine$[0]="bottles" |
|||
TheLine$[1]="of beer on the wall." |
|||
TheLine$[2]="of beer." |
|||
TheLine$[3]="Take one down, pass it around." |
|||
BottlesOfBeer=99 |
|||
TheLine=1 |
|||
GETSCREENSIZE(ScreenSizeX,ScreenSizeY) |
|||
WINDOW Win,0,0,ScreenSizeX,ScreenSizeY,@MINBOX|@MAXBOX|@SIZE,0,"99 Bottles Of Beer",MainHandler |
|||
GETTEXTSIZE(Win,TheLine$[3],TextWidth,TextHeight) |
|||
Erase$=STRING$(TextWidth," ") |
|||
PRINT Win,"Let's sing a song." |
|||
VSpace(2) |
|||
'1.2 seconds. |
|||
STARTTIMER Win,1200 |
|||
GOSUB Sing |
|||
WAITUNTIL Close=1 |
|||
CLOSEWINDOW Win |
|||
END |
|||
SUB MainHandler |
|||
SELECT @CLASS |
|||
CASE @IDCLOSEWINDOW |
|||
Close=1 |
|||
CASE @IDTIMER |
|||
GOSUB Sing |
|||
ENDSELECT |
|||
RETURN |
|||
SUB Sing |
|||
DEF Sing:INT |
|||
Sing=TheLine |
|||
MOVE Win,TextX,TextY |
|||
Number$=STR$(BottlesOfBeer) |
|||
IF BottlesOfBeer=0 |
|||
Number$="No more" |
|||
TheLine$[0]="bottles" |
|||
TheLine$[3]="Go to the store and buy some more." |
|||
ENDIF |
|||
IF BottlesOfBeer=1 |
|||
TheLine$[0]="bottle" |
|||
TheLine$[3]="Take it down, pass it around." |
|||
ENDIF |
|||
IF TheLine=4 THEN Sing=1 |
|||
IF (TheLine=1)|(TheLine=2)|(TheLine=4) |
|||
IF BottlesOfBeer>-1 THEN PRINT Win,Number$+" "+TheLine$[0]+" "+TheLine$[Sing] ELSE GOSUB TheEnd |
|||
ELSE |
|||
PRINT Win,TheLine$[3] |
|||
BottlesOfBeer=BottlesOfBeer-1 |
|||
ENDIF |
|||
TheLine=TheLine+1 |
|||
VSpace(1) |
|||
IF TheLine>4 |
|||
TheLine=1 |
|||
VSpace(1) |
|||
ENDIF |
|||
RETURN |
|||
SUB TheEnd |
|||
PRINT Win,"What's the problem, offishur?" |
|||
STOPTIMER Win |
|||
VSpace(2) |
|||
MOVE Win,TextX,TextY:PRINT Win,"That's all." |
|||
RETURN |
|||
SUB VSpace(Number:UINT) |
|||
TextY=TextY+(TextHeight*Number) |
|||
IF TextY+(TextHeight*8)>ScreenSizeY THEN CLR() |
|||
RETURN |
|||
SUB CLR() |
|||
FOR X=0 TO ScreenSizeY |
|||
MOVE Win,0,X:PRINT Win,Erase$ |
|||
TextY=8 |
|||
NEXT X |
|||
RETURN |
|||
</lang> |
|||
====console only version==== |
|||
Rather quickly written and dirty. |
|||
<lang Creative Basic>REM Using the ESC key to exit will not work in console programs under Windows 95/98 or ME. |
|||
DECLARE SingWallLn() |
|||
DECLARE Delay1() |
|||
DECLARE Delay2() |
|||
'To use ESC Key to exit. |
|||
DECLARE Quit() |
|||
DECLARE TheEnd() |
|||
DEF Bottles:UINT |
|||
DEF Number$,Again$:STRING |
|||
OPENCONSOLE |
|||
PRINT"I'm going to sing a song.":PRINT |
|||
Delay1() |
|||
LABEL StartSong |
|||
Bottles=99 |
|||
DO |
|||
Quit() |
|||
SingWallLn():Delay1() |
|||
PRINT LTRIM$(STR$(Bottles))+Number$+" of beer.":Delay1() |
|||
IF Bottles>0 THEN PRINT"Take one down, pass it around." ELSE PRINT"Take it down, pass it around.":Delay1() |
|||
Bottles=Bottles-1 |
|||
SingWallLn() |
|||
Delay2() |
|||
PRINT:PRINT |
|||
UNTIL Bottles=0 |
|||
Delay2() |
|||
ClS |
|||
LABEL Question |
|||
INPUT"Sing it again (y or n)?",Again$ |
|||
SELECT Again$ |
|||
CASE("y") |
|||
CASE("Y") |
|||
CLS |
|||
GOTO StartSong |
|||
CASE "n" |
|||
CASE "N" |
|||
CLS |
|||
PRINT"Fine, be that way.":Delay2() |
|||
TheEnd() |
|||
ENDSELECT |
|||
PRINT"Sorry, I didn't understand.":PRINT |
|||
GOTO Question |
|||
'Keep from running into subroutines. |
|||
END |
|||
SUB SingWallLn() |
|||
IF Bottles=1 THEN Number$=" bottle" ELSE Number$=" bottles" |
|||
PRINT LTRIM$(STR$(Bottles))+Number$+" of beer on the wall." |
|||
RETURN |
|||
SUB Delay1() |
|||
FOR X=1 TO 7000:NEXT X |
|||
RETURN |
|||
SUB Delay2() |
|||
FOR X=1 TO 1750000:NEXT X |
|||
RETURN |
|||
SUB Quit() |
|||
'Close program by pressing the ESC key. |
|||
'Will not work in console under Windows 95/98 or ME. |
|||
IF GETKEYSTATE(0x1B) THEN TheEnd() |
|||
RETURN |
|||
SUB TheEnd() |
|||
CLOSECONSOLE |
|||
END |
|||
RETURN |
|||
</lang> |
Revision as of 03:10, 21 November 2014
Task in Basic(s)
99 Bottles of Beer done in any of the BASIC-languages.
BASIC
Sound
This version plays the tune 100 times while printing out the lyrics (not synchronized). <lang qbasic>PLAY "<" FOR x = 99 TO 0 STEP -1
PRINT x; "bottles of beer on the wall" PRINT x; "bottles of beer" PRINT "Take one down, pass it around" PRINT x-1; "bottles of beer on the wall" PRINT PLAY "e-8e-8e-8<b-8b-8b-8>e-8e-8e-8e-4"'X bottles of beer on the wall PLAY "f8f8f8c8c8c8f4"'X bottles of beer PLAY "d4d8d8 N0 d8d8d8d4"'take one down, pass it around PLAY "<a+8a+8a+8>c8c8d8d+8d+8d+8d+4"'X-1 bottles of beer on the wall
NEXT x</lang>
Text
<lang qbasic>FOR x = 99 TO 1 STEP -1
PRINT x; "bottles of beer on the wall" PRINT x; "bottles of beer" PRINT "Take one down, pass it around" PRINT x-1; "bottles of beer on the wall" PRINT
NEXT x</lang>
Applesoft BASIC
<lang Applesoft BASIC> 10 HOME 20 FOR B=99 TO 1 STEP -1 30 PRINT B;" BOTTLE OF BEER ON THE WALL" 40 PRINT B;" BOTTLE OF BEER ON THE WALL" 50 PRINT "TAKE ONE DOWN, PASS IT AROUND" 60 PRINT B-1;" BOTTLE OF BEER ON THE WALL" 70 NEXT B </lang>
BASIC256
<lang BASIC256>#length of querter and eight note in ms n4 = 1000 * 60 / 80 / 4 n8 = n4 / 2
- frequency of musical notes in hz
e = 330 ef = 311 b = 247 bf = 233 f = 349 c = 262 d = 294 ds = 311 a = 220
dim notes(1) dim lengs(1)
- redim is automatic when using a {} list to assign an array
notes = {ef, ef, ef, bf, bf, bf, ef, ef, ef, ef, f , f , f , c , c , c , f , d , d , d , d , d , d , d , bf, bf, bf, c , c , ef, ef, ef, ef, ef} lengs = {n8, n8, n8, n8, n8, n8, n8, n8, n8, n4, n8, n8, n8, n8, n8, n8, n4, n4, n8, n8, n8, n8, n8, n4, n8, n8, n8, n8, n8, n8, n8, n8, n8, n4 }
for x = 99 to 1 step -1
for t = 0 to notes[?]-1 if t = 0 then print x + " bottles of beer on the wall" if t = 11 then print x + " bottles of beer" if t = 18 then print "Take one down, pass it around" if t = 25 then print(x-1) + " bottles of beer on the wall" sound notes[t], lengs[t] pause .002 next t print
next x </lang>
BBC BASIC
<lang bbcbasic>
N_Bottles = 99 beer$ = " of beer" wall$ = " on the wall" unit$ = "99 bottles" WHILE N_Bottles >= 0 IF N_Bottles=0 THEN PRINT '"No more bottles" beer$ wall$ ", " unit$ beer$ "." PRINT "Go to the store and buy some more, "; ELSE PRINT 'unit$ beer$ wall$ ", " unit$ beer$ "." PRINT "Take one down and pass it around, "; ENDIF N_Bottles -= 1 CASE N_Bottles OF WHEN 0: unit$ = "no more bottles" WHEN 1: unit$ = "1 bottle" OTHERWISE: unit$ = STR$((N_Bottles + 100) MOD 100) + " bottles" ENDCASE PRINT unit$ beer$ wall$ "." ENDWHILE END
</lang>
Creative Basic
window version
<lang Creative Basic>DEF Win:WINDOW DEF Close:CHAR DEF ScreenSizeX,ScreenSizeY:INT
DECLARE VSpace(Number:UINT) DECLARE CLR()
DEF TheLine$[4],Number$,Erase:STRING DEF TheLine,TextHeight,TextWidth:INT DEF TextX,TextY:UINT
TheLine$[0]="bottles" TheLine$[1]="of beer on the wall." TheLine$[2]="of beer." TheLine$[3]="Take one down, pass it around."
BottlesOfBeer=99 TheLine=1
GETSCREENSIZE(ScreenSizeX,ScreenSizeY)
WINDOW Win,0,0,ScreenSizeX,ScreenSizeY,@MINBOX|@MAXBOX|@SIZE,0,"99 Bottles Of Beer",MainHandler
GETTEXTSIZE(Win,TheLine$[3],TextWidth,TextHeight)
Erase$=STRING$(TextWidth," ")
PRINT Win,"Let's sing a song."
VSpace(2)
'1.2 seconds. STARTTIMER Win,1200
GOSUB Sing
WAITUNTIL Close=1
CLOSEWINDOW Win
END
SUB MainHandler
SELECT @CLASS
CASE @IDCLOSEWINDOW
Close=1
CASE @IDTIMER
GOSUB Sing
ENDSELECT
RETURN
SUB Sing
DEF Sing:INT
Sing=TheLine
MOVE Win,TextX,TextY
Number$=STR$(BottlesOfBeer)
IF BottlesOfBeer=0
Number$="No more"
TheLine$[0]="bottles" TheLine$[3]="Go to the store and buy some more."
ENDIF
IF BottlesOfBeer=1
TheLine$[0]="bottle" TheLine$[3]="Take it down, pass it around."
ENDIF
IF TheLine=4 THEN Sing=1
IF (TheLine=1)|(TheLine=2)|(TheLine=4)
IF BottlesOfBeer>-1 THEN PRINT Win,Number$+" "+TheLine$[0]+" "+TheLine$[Sing] ELSE GOSUB TheEnd
ELSE
PRINT Win,TheLine$[3]
BottlesOfBeer=BottlesOfBeer-1
ENDIF
TheLine=TheLine+1
VSpace(1)
IF TheLine>4
TheLine=1
VSpace(1)
ENDIF
RETURN
SUB TheEnd
PRINT Win,"What's the problem, offishur?"
STOPTIMER Win
VSpace(2)
MOVE Win,TextX,TextY:PRINT Win,"That's all."
RETURN
SUB VSpace(Number:UINT)
TextY=TextY+(TextHeight*Number)
IF TextY+(TextHeight*8)>ScreenSizeY THEN CLR()
RETURN
SUB CLR()
FOR X=0 TO ScreenSizeY
MOVE Win,0,X:PRINT Win,Erase$
TextY=8
NEXT X
RETURN </lang>
console only version
Rather quickly written and dirty. <lang Creative Basic>REM Using the ESC key to exit will not work in console programs under Windows 95/98 or ME.
DECLARE SingWallLn() DECLARE Delay1() DECLARE Delay2() 'To use ESC Key to exit. DECLARE Quit() DECLARE TheEnd()
DEF Bottles:UINT DEF Number$,Again$:STRING
OPENCONSOLE
PRINT"I'm going to sing a song.":PRINT
Delay1()
LABEL StartSong
Bottles=99
DO
Quit()
SingWallLn():Delay1()
PRINT LTRIM$(STR$(Bottles))+Number$+" of beer.":Delay1()
IF Bottles>0 THEN PRINT"Take one down, pass it around." ELSE PRINT"Take it down, pass it around.":Delay1()
Bottles=Bottles-1
SingWallLn()
Delay2()
PRINT:PRINT
UNTIL Bottles=0
Delay2()
ClS
LABEL Question
INPUT"Sing it again (y or n)?",Again$
SELECT Again$
CASE("y") CASE("Y")
CLS
GOTO StartSong
CASE "n" CASE "N"
CLS
PRINT"Fine, be that way.":Delay2()
TheEnd()
ENDSELECT
PRINT"Sorry, I didn't understand.":PRINT
GOTO Question
'Keep from running into subroutines. END
SUB SingWallLn()
IF Bottles=1 THEN Number$=" bottle" ELSE Number$=" bottles"
PRINT LTRIM$(STR$(Bottles))+Number$+" of beer on the wall."
RETURN
SUB Delay1()
FOR X=1 TO 7000:NEXT X
RETURN
SUB Delay2()
FOR X=1 TO 1750000:NEXT X
RETURN
SUB Quit()
'Close program by pressing the ESC key. 'Will not work in console under Windows 95/98 or ME. IF GETKEYSTATE(0x1B) THEN TheEnd()
RETURN
SUB TheEnd()
CLOSECONSOLE
END
RETURN </lang>