Resources For IT Professionals

# Basic Computer Games - Small Basic Edition: Basketball

 This program simulates a game of basketball between Dartmouth College and an opponent of your choice. You are the Dartmouth captain and control the type of shot and defense during the course of the game. There are four types of shots: 1. Long Jump Shot (30 ft.), 2. Short Jump Shot (15 ft), 3. Lay Up, and 4. Set Shot. Both teams use the same defense, but you may call it: Press (6), Man-to-man (6.5), Zone (7), or None (7.5). To change defense, type 0 as your next shot. Note: The game is biased slightly in favor of Dartmouth. The average probability of a Dartmouth shot being good is 62.95% compared to a probability of 61.85% for their opponent (This makes the sample run slightly remarkable in that Cornell won by a score of 45 to 42. Hooray for the Big Red!) Charles Bacheller of Dartmouth College was the original author of this game. Code Listing (Small Basic File: basketbl.sb): 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.

 ``S[1] = 0 S[0] = 0 TextWindow.CursorLeft = 31 TextWindow.WriteLine("BASKETBALL") TextWindow.CursorLeft = 15 TextWindow.WriteLine("BIBLEBYTE BOOKS, MAPLE VALLEY, WASHINGTON") TextWindow.WriteLine("") TextWindow.WriteLine("") TextWindow.WriteLine("") TextWindow.WriteLine("THIS IS DARTMOUTH COLLEGE BASKETBALL. YOU WILL BE DARTMOUTH") TextWindow.WriteLine(" CAPTAIN AND PLAYMAKER. CALL SHOTS AS FOLLOWS: 1. LONG") TextWindow.WriteLine(" (30 FT.) JUMP SHOT; 2. SHORT (15 FT.) JUMP SHOT; 3. LAY") TextWindow.WriteLine(" UP; 4. SET SHOT.") TextWindow.WriteLine("BOTH TEAMS WILL USE THE SAME DEFENSE. CALL DEFENSE AS") TextWindow.WriteLine("FOLLOWS: 6. PRESS; 6.5 MAN-TO MAN; 7. ZONE; 7.5 NONE.") TextWindow.WriteLine("TO CHANGE DEFENSE, JUST TYPE 0 AS YOUR NEXT SHOT.") TextWindow.Write("YOUR STARTING DEFENSE WILL BE ") D = TextWindow.ReadNumber() If D<6 Then Goto LN2010 EndIf TextWindow.WriteLine("") TextWindow.Write("CHOOSE YOUR OPPONENT ") OD = TextWindow.Read() LN370: TextWindow.WriteLine("CENTER JUMP") If Math.GetRandomNumber(100)> 60 Then Goto LN420 EndIf TextWindow.WriteLine(OD+" CONTROLS THE TAP.") Goto LN3000 LN420: TextWindow.WriteLine("DARTMOUTH CONTROLS THE TAP.") LN425: TextWindow.WriteLine("") LN430: TextWindow.Write("YOUR SHOT ") Z = TextWindow.ReadNumber() P=0 If Z<>Math.Floor(Z) Then Goto LN455 EndIf If Z<0 OR Z>4 Then Goto LN455 EndIf Goto LN460 LN455: TextWindow.Write("INCORRECT ANSWER. RETYPE IT. ") Goto LN430 LN460: If math.GetRandomNumber(100)<50 Then Goto LN1000 EndIf If T<100 Then Goto LN1000 EndIf TextWindow.WriteLine("") If S[1]<>S[0] Then Goto LN510 EndIf TextWindow.WriteLine("") TextWindow.WriteLine(" ***** END OF SECOND HALF *****") TextWindow.WriteLine("") TextWindow.WriteLine("SCORE AT END OF REGULATION TIME:") TextWindow.WriteLine(" DARTMOUTH: "+S[1]+" "+OD+": "+S[0]) TextWindow.WriteLine("") TextWindow.WriteLine("BEGIN TWO MINUTE OVERTIME PERIOD") T=93 Goto LN370 LN510: TextWindow.WriteLine(" ***** END OF GAME *****") TextWindow.WriteLine("FINAL SCORE: DARTMOUTH: "+S[1]+" "+OD+": "+S[0]) Program.End() LN1000: If (Z = 1) Then Goto LN1040 elseif (Z = 2) then Goto LN1040 endIf Goto LN1300 LN1040: T=T+1 If T=50 Then Goto LN8000 EndIf If T=92 Then Goto LN1046 EndIf Goto LN1050 LN1046: Sub600() LN1050: TextWindow.WriteLine("JUMP SHOT") If Math.GetRandomNumber(100)>34.1*D/8 Then Goto LN1090 EndIf TextWindow.WriteLine("SHOT IS GOOD.") Sub7000() Goto LN3000 LN1090: If Math.GetRandomNumber(100)>68.2*D/8 Then Goto LN1200 EndIf TextWindow.WriteLine("SHOT IS OFF TARGET.") If D/6*Math.GetRandomNumber(100)>45 Then Goto LN1130 EndIf TextWindow.WriteLine("DARTMOUTH CONTROLS THE REBOUND.") Goto LN1145 LN1130: TextWindow.WriteLine("REBOUND TO "+OD) Goto LN3000 LN1145: If Math.GetRandomNumber(100)>40 Then Goto LN1158 EndIf Goto LN1300 LN1158: If D=6 Then Goto LN5100 EndIf LN1160: TextWindow.Write("BALL PASSED BACK TO YOU. ") Goto LN430 If Math.GetRandomNumber(100)>90 Then Goto LN1190 EndIf TextWindow.WriteLine("PLAYER FOULED, TWO SHOTS.") Sub4000() Goto LN3000 LN1190: TextWindow.WriteLine("BALL STOLEN. "+OD+"'S BALL.") Goto LN3000 LN1200: If Math.GetRandomNumber(100)>78.2*D/8 Then Goto LN1250 EndIf TextWindow.Write("SHOT IS BLOCKED. BALL CONTROLLED BY ") If Math.GetRandomNumber(100)>50 Then Goto LN1242 EndIf TextWindow.WriteLine("DARTMOUTH.") Goto LN430 LN1242: TextWindow.WriteLine(Od+".") Goto LN3000 LN1250: If Math.GetRadians(100)>84.3*D/8 Then Goto LN1270 EndIf TextWindow.WriteLine("SHOOTER IS FOULED. TWO SHOTS.") Sub4000() Goto LN3000 LN1270: TextWindow.WriteLine("CHARGING FOUL. DARTMOUTH LOSES BALL.") Goto LN3000 LN1300: T=T+1 If T=50 Then Goto LN8000 EndIf If T=92 Then Goto LN1304 EndIf Goto LN1305 LN1304: Sub600() LN1305: If Z=0 Then Goto LN2010 EndIf If Z>3 Then Goto LN1700 EndIf TextWindow.WriteLine("LAY UP.") LN1330: If 7/D*Math.GetRandomNumber(100)>40 Then Goto LN1360 EndIf TextWindow.WriteLine("SHOT IS GOOD. TWO POINTS.") Sub7000() Goto LN3000 LN1360: If 7/D*Math.GetRandomNumber(100)>70 Then Goto LN1500 EndIf TextWindow.WriteLine("SHOT IS OFF THE RIM.") If Math.GetRandomNumber(100)>200/3 Then Goto LN1415 EndIf TextWindow.WriteLine(Od+" CONTROLS THE REBOUND.") Goto LN3000 LN1415: TextWindow.WriteLine("DARTMOUTH CONTROLS THE REBOUND.") If Math.GetRandomNumber(100)>40 Then Goto LN1440 EndIf Goto LN1300 LN1440: TextWindow.Write("BALL PASSED BACK TO YOU.") Goto LN430 LN1500: If 7/D*Math.GetRandomNumber(100)>87.5 Then Goto LN1600 EndIf TextWindow.WriteLine("SHOOTER FOULED. TWO SHOTS.") Sub4000() Goto LN3000 LN1600: If 7/D*Math.GetRandomNumber(100)>92.5 Then Goto LN1630 EndIf TextWindow.WriteLine("SHOT BLOCKED. "+Od+"'S BALL.") Goto LN3000 LN1630: TextWindow.WriteLine("CHARGING FOUL. DARTMOUTH LOSES THE BALL.") Goto LN3000 LN1700: TextWindow.WriteLine("SET SHOT.") Goto LN1330 LN2010: TextWindow.Write("YOUR NEW DEFENSIVE ALLIGNMENT IS ") D = TextWindow.ReadNumber() If D<6 Then Goto LN2010 EndIf Goto LN425 LN3000: P=1 T=T+1 If T=50 Then Goto LN8000 EndIf Goto LN3018 Sub600() LN3018: TextWindow.WriteLine("") Z1=10/4*(Math.GetRandomNumber(100)/100)+1 If Z1>2 Then Goto LN3500 EndIf TextWindow.WriteLine("JUMP SHOT.") If 8/D*Math.GetRandomNumber(100)>35 Then Goto LN3100 EndIf TextWindow.WriteLine("SHOT IS GOOD.") Sub6000() Goto LN425 LN3100: If 8/D*Math.GetRandomNumber(100)>75 Then Goto LN3200 EndIf TextWindow.WriteLine("SHOT IS OFF RIM.") LN3110: If D/6*Math.GetRandomNumber(100)>50 Then Goto LN3150 EndIf TextWindow.WriteLine("DARTMOUTH CONTROLS THE REBOUND.") Goto LN425 LN3150: TextWindow.WriteLine(OD+" CONTROLS THE REBOUND.") If D=6 Then Goto LN5000 EndIf LN3165: If Math.GetRandomNumber(100)>50 Then Goto LN3175 EndIf TextWindow.WriteLine("PASS BACK TO "+Od+" GUARD.") Goto LN3000 LN3175: Goto LN3500 LN3200: If 8/D*Math.GetRandomNumber(100)>90 Then Goto LN3310 EndIf TextWindow.WriteLine("PLAYER FOULED. TWO SHOTS.") Sub4000() Goto LN425 LN3310: TextWindow.WriteLine("OFFENSIVE FOUL. DARTMOUTH'S BALL.") Goto LN425 LN3500: If Z1>3 Then Goto LN3800 EndIf TextWindow.WriteLine("LAY UP.") LN3520: If 7/D*Math.GetRandomNumber(100)>41.3 Then Goto LN3600 EndIf TextWindow.WriteLine("SHOT IS GOOD.") Sub6000() Goto LN425 LN3600: TextWindow.WriteLine("SHOT IS MISSED.") Goto LN3110 LN3800: TextWindow.WriteLine("SET SHOT.") Goto LN3520 Sub Sub4000 ' FOUL SHOOTING If Math.GetRandomNumber(100)>49 Then TextWindow.WriteLine("SHOOTER MAKES BOTH SHOTS.") S[1-P]=S[1-P]+2 Else If Math.GetRandomNumber(100)>75 Then TextWindow.WriteLine("SHOOTER MAKES ONE SHOT AND MISSES ONE.") S[1-P]=S[1-P]+1 Else TextWindow.WriteLine("BOTH SHOTS MISSED.") EndIf EndIf Sub6010() EndSub LN5000: If Math.GetRandomNumber(100)>75 Then Goto LN5010 EndIf Goto LN3165 LN5010: TextWindow.WriteLine("BALL STOLEN. EASY LAY UP FOR DARTMOUTH.") Sub7000() Goto LN3000 LN5100: If Math.GetRandomNumber(100)>60 Then Goto LN5120 EndIf Goto LN1160 LN5120: TextWindow.WriteLine("PASS STOLEN BY "+Od+" EASY LAYUP.") Sub6000() Goto LN425 Sub Sub6000 S[0]=S[0]+2 Sub6010() EndSub Sub Sub6010 TextWindow.WriteLine("SCORE: "+S[1]+" TO "+S[0]) EndSub Sub Sub7000 S[1]=S[1]+2 Sub6010() EndSub LN8000: TextWindow.WriteLine("") TextWindow.WriteLine(" ***** END OF FIRST HALF *****") TextWindow.WriteLine("") TextWindow.WriteLine("SCORE: DARTMOUTH: "+S[1]+" "+Od+": "+S[0]) TextWindow.WriteLine("") TextWindow.WriteLine("") Goto LN370 Sub Sub600 TextWindow.WriteLine("") TextWindow.WriteLine(" *** TWO MINUTES LEFT IN THE GAME ***") TextWindow.WriteLine("") EndSub``

Sample Run:

Next Chapter > >

Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful