Share via


Blackjack

This is a simulation of the card game of Blackjack or 21, Las Vegas style. This rather comprehensive version allows for up to seven players. On each hand each player may get another card (a hit), stand, split a hand in the event two identical cards were received, or double down. Also, the dealer will ask for an insurance bet if he has an exposed ace. 

Cards are automatically reshuffled as the 51st card is reached. For greater realism, you may wish to change this to the 41st card by changing the 51 to 41 in this code:

If C<51 Then Goto LN230 EndIf

 

Actually, fanatical purists will want to modify the program so it uses three decks of cards instead of just one. 

This program originally surfaced at Digital Equipment Corp.; the author is unknown. 

Code Listing (Small Basic File: blackjck.sb):

 

TextWindow.CursorLeft = 31 TextWindow.WriteLine("BLACKJACK") TextWindow.CursorLeft = 15 TextWindow.WriteLine("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") TextWindow.WriteLine("") TextWindow.WriteLine("") TextWindow.WriteLine("") Goto LN1500 Sub Sub100 ' --SUBROUTINE TO GET A CARD. RESULT IS PUT IN X. If (gs120) then Goto ln120 endif If C<51 Then Goto LN230 EndIf ln120: gs120 = "false" TextWindow.WriteLine("RESHUFFLING") For D=D To 1 Step -1 C=C-1 CArray[C]=DArray[D] EndFor For C1=52 To C Step -1 C2=Math.GetRandomNumber(C1 - C + 1) + (C - 1) C3=CArray[C2] CArray[C2]=CArray[C1] CArray[C1]=C3 EndFor LN230: X=CArray[C] C=C+1 EndSub Sub Sub300 ' --SUBROUTINE TO EVALUATE HAND I. TOTAL IS PUT INTO ' --QArray[I]. TOTALS HAVE THE FOLLOWING MEANING ' -- 2-10...HARD 2-10 ' -- 11-21...SOFT 11-21 ' -- 22-32...HARD 11-21 ' -- 33+....BUSTED Q=0 For Q2=1 To RArray[I] X=PArray[I][Q2] Sub500() EndFor QArray[I]=Q EndSub Sub Sub500 ' --SUBROUTINE TO ADD CARD X TO TOTAL Q. X1=X If X1>10 Then X1=10 EndIf ' SAME AS X1=10 MIN X Q1=Q+X1 If Q>=11 Then Goto LN590 EndIf If X>1 Then Goto LN570 EndIf Q=Q+11 Goto ln620 LN570: Q=Q1 If (Q1>=11) then Q = Q1 +11 endif Goto ln620 LN590: Q=Q1 If (Q<=21 AND Q1>21) then Q = q1+1 endif If Q<33 Then Goto LN620 EndIf Q=-1 LN620: EndSub Sub Sub700 'CARD PRINTING SUBROUTINE) ' D$ DEFINED ELSEWHERE TextWindow.Write(Text.GetSubText(DD,3*X-2,3)) TextWindow.Write(" ") EndSub Sub Sub750 'ALTERNATIVE PRINTING ROUTINE) TextWindow.Write(" "+Text.GetSubText(DD,3*X-1,2)) TextWindow.Write(" ") EndSub Sub Sub800 ' --SUBROUTINE TO PLAY OUT A HAND. ' --NO SPLITTING OR BLACKJACKS ALLOWED If gs860 then Goto ln860 endif If gs950 then Goto ln950 endif H1=5 LN830: Sub1410() H1=3 If (H = 1) Then Goto LN950 ElseIf (H = 2) Then Goto LN930 EndIf ln860: gs860 = "false" Sub100() BArray[I]=BArray[I]*2 TextWindow.WriteLine("RECEIVED A") Sub700() Sub1100() If Q>0 Then gs1300="true" Sub1200() EndIf Goto ln1010 LN930: gs1320 = "true" Sub1200() LN940: Goto ln1010 ln950: Sub100() TextWindow.Write("RECEIVED A") Sub700() Sub1100() If Q<0 Then Goto LN940 EndIf TextWindow.Write("HIT? ") Goto LN830 ln1010: EndSub Sub Sub1100 ' --SUBROUTINE TO ADD A CARD TO ROW I RArray[I]=RArray[I]+1 PArray[I][RArray[I]]=X Q=QArray[I] Sub500() QArray[I]=Q If Q>=0 Then Goto LN1190 EndIf TextWindow.WriteLine("...BUSTED") Sub1200() LN1190: EndSub Sub Sub1200 ' --SUBROUTINE TO DISCARD ROW I If gs1300 then Goto ln1300 endif If gs1320 then Goto ln1320 endif LN1210: If RArray[I]<>0 Then Goto LN1230 EndIf Goto ln1330 LN1230: D=D+1 DArray[D]=PArray[I][RArray[I]] RArray[I]=RArray[I]-1 Goto LN1210 ln1300: ' PRINTS TOTAL OF HAND I ") TextWindow.WriteLine("") ln1320: GS1320="FALSE" AA=QArray[I] Sub3400() TextWindow.WriteLine("TOTAL IS "+AA) ln1330: EndSub ' --SUBROUTINE TO READ REPLY Sub Sub1410 ' I$ DEFINED ELSEWHERE LN1420: HD = TextWindow.Read() HD=Text.GetSubText(HD, 1, 1) For H=1 To H1 Step 2 If HD=Text.GetSubText(ID,H,1) Then Goto LN1480 EndIf EndFor TextWindow.Write("TYPE "+Text.GetSubText(ID,1,H1-1)+" OR "+Text.GetSubText(ID,H1,2)+" PLEASE ") Goto LN1420 LN1480: H=(H+1)/2 EndSub LN1500: ' --PROGRAM STARTS HERE ' --INITIALIZE DD="N A 2 3 4 5 6 7N 8 9 10 J Q K" ID="H,S,D,/," For I=1 To 13 For J=4*I-3 To 4*I DArray[J]=I EndFor EndFor D=52 C=53 TextWindow.Write("DO YOU WANT INSTRUCTIONS? ") HD = TextWindow.Read() If Text.GetSubText(HD,1,1)="N" OR Text.GetSubText(HD,1,1)="n" Then Goto LN1760 EndIf TextWindow.WriteLine("THIS IS THE GAME OF 21. AS MANY AS 7 PLAYERS MAY PLAY THE") TextWindow.WriteLine("GAME. ON EACH DEAL, BETS WILL BE ASKED FOR, AND THE") TextWindow.WriteLine("PLAYERS' BETS SHOULD BE TYPED IN. THE CARDS WILL THEN BE") TextWindow.WriteLine("DEALT, AND EACH PLAYER IN TURN PLAYS HIS HAND. THE") TextWindow.WriteLine("FIRST RESPONSE SHOULD BE EITHER 'D', INDICATING THAT THE") TextWindow.WriteLine("PLAYER IS DOUBLING DOWN, 'S', INDICATING THAT HE IS") TextWindow.WriteLine("STANDING, 'H', INDICATING HE WANTS ANOTHER CARD, OR '/',") TextWindow.WriteLine("INDICATING THAT HE WANTS TO SPLIT HIS CARDS. AFTER THE") TextWindow.WriteLine("INITIAL RESPONSE, ALL FURTHER RESPONSES SHOULD BE 'S' OR") TextWindow.WriteLine("'H', UNLESS THE CARDS WERE SPLIT, IN WHICH CASE DOUBLING") TextWindow.WriteLine("DOWN IS AGAIN PERMITTED. IN ORDER TO COLLECT FOR") TextWindow.WriteLine("BLACKJACK, THE INITIAL RESPONSE SHOULD BE 'S'.") LN1760: TextWindow.Write("NUMBER OF PLAYERS? ") N = TextWindow.ReadNumber() TextWindow.WriteLine("") If N<1 OR N>7 OR N>Math.Floor(N) Then Goto LN1760 EndIf For I=1 To 8 TArray[I]=0 EndFor D1=N+1 LN1810: If 2*D1+C>=52 Then 'enter middle of sub GS120 = "true" Sub100() EndIf If C=2 Then C=C-1 EndIf For I=1 To N Z[I]=0 EndFor For I=1 To 15 BArray[I]=0 EndFor For I=1 To 15 QArray[I]=0 EndFor For I=1 To 7 SArray[I]=0 EndFor For I=1 To 15 RArray[I]=0 EndFor LN1880: TextWindow.WriteLine("BETS: ") For I=1 To N TextWindow.Write("#"+I+" ") Z[I] = TextWindow.ReadNumber() EndFor For I=1 To N If Z[I]<=0 OR Z[I]>500 Then Goto LN1880 EndIf BArray[I]=Z[I] EndFor TextWindow.Write("PLAYER ") For I=1 To N TextWindow.Write(I + " ") EndFor TextWindow.WriteLine("DEALER ") For J=1 To 2 TextWindow.CursorLeft = 5 For I=1 To D1 Sub100() PArray[I][J]=X If J=1 OR I<=N Then Sub750() EndIf EndFor TextWindow.WriteLine("") EndFor For I=1 To D1 RArray[I]=2 EndFor ' --TEST FOR INSURANCE If PArray[D1][1]>1 Then Goto LN2240 EndIf TextWindow.Write("ANY INSURANCE? ") HD= TextWindow.Read() If text.GetSubText(HD,1,1)<>"Y" Then Goto LN2240 EndIf LN2160: TextWindow.WriteLine("INSURANCE BETS") For I=1 To N TextWindow.Write("#"+I+" ") Z[I] = TextWindow.ReadNumber() EndFor For I=1 To N If Z[I]<0 OR Z[I]>BArray[I]/2 Then Goto LN2160 EndIf EndFor For I=1 To N If PArray[D1][2]>=10 Then SArray[I]=Z[I]*(3*(-(-1))-1) Else SArray[I]=Z[I]*(3*(-(0))-1) EndIf EndFor LN2240: ' --TEST FOR DEALER BLACKJACK L1=1 L2=1 If PArray[D1][1]=1 AND PArray[D1][2]>9 Then L1=0 EndIf L2=0 If PArray[D1][2]=1 AND PArray[D1][1]>9 Then L1=0 EndIf L2=0 If L1<>0 OR L2<>0 Then Goto LN2320 EndIf TextWindow.WriteLine("") TextWindow.Write("DEALER HAS A"+Text.GetSubText(DD,3*PArray[D1][2]-2,3)+" IN THE HOLE ") TextWindow.WriteLine("FOR BLACKJACK") For I=1 To D1 Sub300() EndFor Goto LN3140 LN2320: ' --NO DEALER BLACKJACK If PArray[D1][1]>1 AND PArray[D1][1]<10 Then Goto LN2350 EndIf TextWindow.WriteLine("") TextWindow.WriteLine("NO DEALER BLACKJACK.") LN2350: ' --NOW PLAY THE HANDS For I=1 To N LN2370: TextWindow.Write("PLAYER "+I+"? ") H1=7 Sub1410() If (H = 1) Then Goto LN2550 ElseIf (H = 2) Then Goto LN2410 ElseIf (H = 3) Then Goto LN2510 ElseIf (H = 4) Then Goto LN2600 EndIf LN2410: ' --PLAYER WANTS TO STAND Sub300() If QArray[I]<>21 Then Goto LN2490 EndIf TextWindow.WriteLine("BLACKJACK") SArray[I]=SArray[I]+1.5*BArray[I] BArray[I]=0 Sub1200() Goto LN2900 LN2490: gs1320="true" Sub1200() Goto LN2900 LN2510: ' --PLAYER WANTS TO DOUBLE DOWN Sub300() gs860 = "true" Sub800() Goto LN2900 LN2550: ' --PLAYER WANTS TO BE HIT Sub300() H1=3 gs950="true" Sub800() Goto LN2900 LN2600: ' --PLAYER WANTS TO SPLIT L1=PArray[I][1] If PArray[I][1]>10 Then L1=10 EndIf L2=PArray[I][2] If PArray[I][2]>10 Then L2=10 EndIf If L1=L2 Then Goto LN2640 EndIf TextWindow.WriteLine("SPLITTING NOT ALLOWED.") Goto LN2370 LN2640: ' --PLAY OUT SPLIT I1=I+D1 RArray[I1]=2 PArray[I1][1]=PArray[I][2] BArray[I+D1]=BArray[I] Sub100() TextWindow.Write("FIRST HAND RECEIVES A") Sub700() PArray[I][2]=X Sub300() TextWindow.WriteLine("") Sub100() TextWindow.Write("SECOND HAND RECEIVES A") I=I1 Sub700() PArray[I][2]=X Sub300() TextWindow.WriteLine("") I=I1-D1 If PArray[I][1]=1 Then Goto LN2900 EndIf ' --NOW PLAY THE TWO HANDS LN2850: If I > D1 Then TextWindow.Write("HAND 2 ") Else TextWindow.Write("HAND 1 ") EndIf Sub800() I=I+D1 If I=I1 Then Goto LN2850 EndIf I=I1-D1 LN2900: EndFor Sub300() ' --TEST FOR PLAYING DEALER'S HAND For I=1 To N If RArray[I]>0 OR RArray[I+D1]>0 Then Goto LN3010 EndIf EndFor TextWindow.Write("DEALER HAD A ") X=PArray[D1][2] Sub700() TextWindow.WriteLine(" CONCEALED.") Goto LN3140 LN3010: TextWindow.Write("DEALER HAS A"+Text.GetSubText(DD,3*PArray[D1][2]-2,3)+" CONCEALED ") I=D1 AA=QArray[I] Sub3400() TextWindow.WriteLine("FOR A TOTAL OF "+AA) If AA>16 Then Goto LN3130 EndIf TextWindow.Write("DRAWS") LN3060: Sub100() Sub750() Sub1100() AA=Q Sub3400() If Q>0 AND AA<17 Then Goto LN3060 EndIf If Q < 0 then QArray[I]=Q-(-1)/2 else QArray[I]=Q-(0)/2 endif If Q<0 Then Goto LN3140 EndIf AA=Q Sub3400() TextWindow.WriteLine("---TOTAL IS "+AA) LN3130: TextWindow.WriteLine("") LN3140: ' --TALLY THE RESULT ' ZD="LOSES PUSHES WINS " TextWindow.WriteLine("") For I=1 To N AA=QArray[I] Sub3400() AB=QArray[I+D1] Sub3410() AC=QArray[D1] Sub3420() If AA > AC Then SArray[i] = SArray[i] + BArray[i] ElseIf AA < AC then SArray[i] = SArray[i] - BArray[i] EndIf If AB > AC Then SArray[i] = SArray[i] +BArray[i+d1] ElseIf AC < AB then SArray[i] = SArray[i] -BArray[i+d1] EndIf BArray[I+D1]=0 TextWindow.Write("PLAYER "+I+" ") If SArray[i] > 0 Then TextWindow.Write(Text.GetSubText(ZD,(1)*6+7,6)+" ") ElseIf SArray[i] =0 then TextWindow.Write(Text.GetSubText(ZD,(0)*6+7,6)+" ") Else TextWindow.Write(Text.GetSubText(ZD,(-1)*6+7,6)+" ") EndIf If SArray[I]<>0 Then Goto LN3250 EndIf TextWindow.Write(" ") Goto LN3260 LN3250: TextWindow.Write(Math.Abs(SArray[I])) LN3260: TArray[I]=TArray[I]+SArray[I] TextWindow.WriteLine(" TOTAL= "+TArray[I]) Sub1200() TArray[D1]=TArray[D1]-SArray[I] I=I+D1 Sub1200() I=I-D1 EndFor TextWindow.WriteLine("DEALER'S TOTAL= "+TArray[D1]) TextWindow.WriteLine("") Sub1200() Goto LN1810 Sub Sub3400 If (AA>=22) Then AA = AA - 11 EndIf EndSub Sub Sub3410 If (AB>=22) Then AB = AB - 11 EndIf EndSub Sub Sub3420 If (AC>=22) Then AC = AC - 11 EndIf EndSub

This chapter is adapted from the book Basic Computer Games Small Basic Edition published by BibleByte Books.

To purchase this book in its entirety, please see the Computer Science For Kids web site.

 

Sample Run:

BlackJack

Next Chapter > >

© Copyright 2010 By BibleByte Books. All Rights Reserved. BibleByte Books, the BibleByte Books Logo, Computer Science For Kids, the Computer Science For Kids logo, and related trade dress are trademarks or registered trademarks of BibleByte Books.