134 lines
4.5 KiB
Plaintext
134 lines
4.5 KiB
Plaintext
100 rem 21 game
|
|
110 rem for rosetta code
|
|
120 '
|
|
130 rem initialization
|
|
140 l$ = chr$(157) : rem left cursor
|
|
150 dim p$(2),hc(2),ca(4) : hc(1) = 0 : hc(2) = 0 : rem players
|
|
160 ca(0) = 1 : ca(1) = 1 : ca(2) = 3 : ca(3) = 2 : rem computer answers
|
|
170 dim cn$(6) : for i = 1 to 6 : read cn$(i) : next i : rem computer names
|
|
180 def fnm(X)=(X-(INT(X/4))*4):REM modulo function
|
|
190 '
|
|
200 rem optionally set screen colors here
|
|
210 cls
|
|
220 print " 21 GAME"
|
|
230 print : print " The goal of this game is to take turns"
|
|
240 print " adding the value of either 1, 2, or 3"
|
|
250 print " to a running total. The first player"
|
|
260 print " to bring the total to 21..."
|
|
270 print : print " ... WINS THE GAME!"
|
|
280 print : gosub 1110
|
|
290 for p = 1 to 2
|
|
300 '
|
|
310 rem game setup and get players
|
|
320 for p = 1 to 2
|
|
330 print : print "Player ";p;l$;", [H]uman or [C]omputer? ";
|
|
340 k$ = inkey$ : if k$ <> "c" and k$ <> "h" then 340
|
|
350 print k$ : hc(p) = (k$ = "c")
|
|
360 print : print "Player";p;l$ "," : print "Enter your name"; : if hc(p) then goto 400
|
|
370 input p$(p)
|
|
380 next p
|
|
390 goto 420
|
|
400 gosub 1340 : print "? ";p$(p)
|
|
410 next p
|
|
420 print
|
|
430 for p = 1 to 2 : print p;l$;". ";p$(p) : next p
|
|
440 print : print "Is this correct (y/n)? ";
|
|
450 k$ = inkey$ : if k$ <> "y" and k$ <> "n" then 450
|
|
460 print k$ : if k$ = "n" then goto 290
|
|
470 print : print "Who will play first (1 or 2)? ";
|
|
480 k$ = inkey$ : if k$ < "1" or k$ > "2" then 480
|
|
490 fp = asc(k$)-48 : print k$ : print
|
|
500 print "Okay, ";p$(fp);" will play first." : print : gosub 1110
|
|
510 cls
|
|
520 '
|
|
530 rem start main game loop
|
|
540 pi = fp : rt = 0
|
|
550 print "Total so far:";rt
|
|
560 print : print p$(pi);"'s turn."
|
|
570 if hc(pi) then gosub 1240
|
|
580 if not hc(pi) then gosub 1170
|
|
590 rt = rt+ad
|
|
600 if rt = 21 then goto 680
|
|
610 if rt > 21 then print : print p$(pi);" loses by going over 21!!" : goto 700
|
|
620 pi = pi+1 : if pi > 2 then pi = 1
|
|
630 goto 550
|
|
640 print : print " ... WINS THE GAME!"
|
|
650 print : gosub 1110
|
|
660 '
|
|
670 for p = 1 to 2
|
|
680 rem winner winner chicken dinner
|
|
690 print : print "21! ";p$(pi);" wins the game!!!"
|
|
700 print : print "Would you like to play again? ";
|
|
710 k$ = inkey$ : if k$ <> "n" and k$ <> "y" then 710
|
|
720 print k$
|
|
730 if k$ = "n" then print : print "Okay, maybe another time. Bye!" : end
|
|
740 goto 200
|
|
750 print k$ : hc(p) = (k$ = "c")
|
|
760 print : print "Player";p;l$ "," : print "Enter your name"; : if hc(p) then goto 800
|
|
770 input p$(p)
|
|
780 next p
|
|
790 goto 820
|
|
800 gosub 1340 : print "? ";p$(p)
|
|
810 next p
|
|
820 print : for p = 1 to 2 : print p;l$;". ";p$(p) : next p
|
|
830 print : print "Is this correct (y/n)? ";
|
|
840 k$ = inkey$ : if k$ <> "y" and k$ <> "n" then 840
|
|
850 print k$ : if k$ = "n" then goto 660
|
|
860 print : print "Who will play first (1 or 2)? ";
|
|
870 k$ = inkey$ : if k$ < "1" or k$ > "2" then 870
|
|
880 fp = asc(k$)-48 : print k$ : print
|
|
890 print "Okay, ";p$(fp);" will play first." : print : gosub 1110
|
|
900 '
|
|
910 rem start main game loop
|
|
920 pi = fp : rt = 0
|
|
930 print chr$(147);"Total so far: ";rt
|
|
940 print : print p$(pi);"'s turn."
|
|
950 if hc(pi) then gosub 1240
|
|
960 if not hc(pi) then gosub 1170
|
|
970 rt = rt+ad
|
|
980 if rt = 21 then goto 1030
|
|
990 if rt > 21 then print : print p$(pi);" loses by going over 21!!" : goto 1050
|
|
1000 pi = pi+1 : if pi > 2 then pi = 1
|
|
1010 goto 930
|
|
1020 '
|
|
1030 rem winner winner chicken dinner
|
|
1040 print : print "21! ";p$(pi);" wins the game!!!"
|
|
1050 print : print "Would you like to play again? ";
|
|
1060 k$ = inkey$ : if k$ <> "n" and k$ <> "y" then 1060
|
|
1070 print k$
|
|
1080 if k$ = "n" then print : print "Okay, maybe another time. Bye!" : end
|
|
1090 goto 490
|
|
1100 '
|
|
1110 rem pause for keypress
|
|
1120 z$ = " Press a key to continue. "
|
|
1130 print z$
|
|
1140 k$ = inkey$ : if k$ = "" then 1140
|
|
1150 return
|
|
1160 '
|
|
1170 rem human player move
|
|
1180 print : print "How much to add,"
|
|
1190 print "1, 2, or 3 (0 to quit)"; : input ad
|
|
1200 if ad < 0 or ad > 3 then print : print "Illegal amount. Try again." : goto 1180
|
|
1210 if ad = 0 then print : print "Game was ended by ";p$(pi);"." : end
|
|
1220 return
|
|
1230 '
|
|
1240 rem computer player move
|
|
1250 print : print "Thinking...";
|
|
1260 tt = int(rnd(1)*10)
|
|
1270 for t = 1 to tt : print "."; : for i = 1 to 250 : next i : next t : print
|
|
1280 rm = fn m(rt)
|
|
1290 ad = ca(rm)
|
|
1300 print : print p$(pi);" adds ";ca(rm);l$;"."
|
|
1310 for t = 1 to 1000 : next t
|
|
1320 return
|
|
1330 '
|
|
1340 rem pick a computer name
|
|
1350 pn = int(rnd(1)*6)+1 : t$ = cn$(pn)
|
|
1360 if t$ = p$(p-1) then goto 1350
|
|
1370 p$(p) = t$
|
|
1380 return
|
|
1390 '
|
|
1400 rem some computer names to pick from
|
|
1410 data "Commodore 64","VIC-20","Commodore 128"
|
|
1420 data "PET","Plus/4","Commodore 16"
|