Share via


Battle

BATTLE is based on the popular game Battleship which is primarily played to familiarize people with the location and designation of points on a coordinate plane. 

BATTLE first randomly sets up the bad guys' fleet disposition on a 6 by 6 matrix or grid. The fleet consists of six ships: Two destroyers (ships number 1 and 2) which are two units long, two cruisers (ships number 3 and 4) which are three units long and two aircraft carriers (ships number 5 and 6) which are four units long. The program then prints out this fleet disposition in a coded or disguised format (see the sample computer print-out). You then proceed to sink the various ships by typing in the coordinates (two digits, X and Y, each from 1 to 6) of the place where you want to drop a bomb, if you'll excuse the expression. The computer gives the appropriate responses (splash, hit, etc.) which you should record on a 6 by 6 matrix. You are thus building a representation of the actual fleet disposition which you will hopefully use to decode the coded fleet disposition printed out by the computer. Each time a ship is sunk, the computer prints out which ships have been sunk so far and also gives you a "SPLASH/HIT RATIO." 

The first thing you should learn is how to locate and designate positions on the matrix, and specifically the difference between X=3, Y=4 and X=4, Y=3. Our method corresponds to the location of points on the coordinate plane rather than the location of numbers in a standard algebraic matrix: the X value gives the column counting from left to right and the Y value gives the row counting from bottom to top. 

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.

The second thing you should learn about is the splash/hit ratio. "What is a ratio?" A good reply is "It's a fraction or quotient." Specifically, the splash/hit ratio is the number of splashes divided by the number of hits. If you had 9 splashes and 15 hits, the ratio would be 9/15 or 3/5, both of which are correct. The computer would give this splash/hit ratio as .6. 

The main objective and primary educational benefit of BATTLE comes from attempting to decode the bad guys' fleet disposition code. To do this, you must make a comparison between the coded matrix and the actual matrix which you construct as you play the game. 

The original author of both the program and these descriptive notes is Ray Westergard of the Lawrence Hall of Science, Berkeley, California. 

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

TextWindow.CursorLeft = 33 TextWindow.WriteLine("BATTLE") TextWindow.CursorLeft = 15 TextWindow.WriteLine("BIBLEBYTE BOOKS, MAPLE VALLEY, WASHINGTON") ' -- BATTLE WRITTEN BY RAY WESTERGARD 10/70 ' COPYRIGHT 1971 BY THE REGENTS OF THE UNIV. OF CALIF. ' PRODUCED AT THE LAWRENCE HALL OF SCIENCE, BERKELEY LN50: For X=1 To 6 For Y=1 To 6 FArray[X][Y]=0 EndFor EndFor For I=1 To 3 N=4-I For J=1 To 2 LN90: A=Math.GetRandomNumber(6) B=Math.GetRandomNumber(6) D=Math.GetRandomNumber(4) If FArray[A][B]>0 Then Goto LN90 EndIf M=0 If (D = 1) Then Goto LN150 ElseIf (D = 2) Then Goto LN340 ElseIf (D = 3) Then Goto LN550 ElseIf (D = 4) Then Goto LN740 EndIf LN150: BArray[1]=B BArray[2]=7 BArray[3]=7 For K=1 To N If M>1 Then Goto LN240 EndIf If BArray[K]=6 Then Goto LN230 EndIf If FArray[A][BArray[K]+1]>0 Then Goto LN230 EndIf BArray[K+1]=BArray[K]+1 Goto LN280 LN230: M=2 LN240: If BArray[1]<BArray[2] AND BArray[1]<BArray[3] Then Z=BArray[1] EndIf If BArray[2]<BArray[1] AND BArray[2]<BArray[3] Then Z=BArray[2] EndIf If BArray[3]<BArray[1] AND BArray[3]<BArray[2] Then Z=BArray[3] EndIf If Z=1 Then Goto LN90 EndIf If FArray[A][Z-1]>0 Then Goto LN90 EndIf BArray[K+1]=Z-1 LN280: EndFor FArray[A][B]=9-2*I-J For K=1 To N FArray[A][BArray[K+1]]=FArray[A][B] EndFor Goto LN990 LN340: AArray[1]=A BArray[1]=B AArray[2]=0 AArray[3]=0 BArray[2]=0 BArray[3]=0 For K=1 To N If M>1 Then Goto LN460 EndIf If AArray[K]=1 OR BArray[K]=1 Then Goto LN450 EndIf If FArray[AArray[K]-1][BArray[K]-1]>0 Then Goto LN450 EndIf If FArray[AArray[K]-1][BArray[K]] > 0 AND FArray[AArray[K]-1][BArray[K]]=FArray[AArray[K]][BArray[K]-1] Then Goto LN450 EndIf AArray[K+1]=AArray[K]-1 BArray[K+1]=BArray[K]-1 Goto LN530 LN450: M=2 LN460: If AArray[1]>AArray[2] AND AArray[1]>AArray[3] Then Z1=AArray[1] EndIf If AArray[2]>AArray[1] AND AArray[2]>AArray[3] Then Z1=AArray[2] EndIf If AArray[3]>AArray[1] AND AArray[3]>AArray[2] Then Z1=AArray[3] EndIf If BArray[1]>BArray[2] AND BArray[1]>BArray[3] Then Z2=BArray[1] EndIf If BArray[2]>BArray[1] AND BArray[2]>BArray[3] Then Z2=BArray[2] EndIf If BArray[3]>BArray[1] AND BArray[3]>BArray[2] Then Z2=BArray[3] EndIf If Z1=6 OR Z2=6 Then Goto LN90 EndIf If FArray[Z1+1][Z2+1]>0 Then Goto LN90 EndIf If FArray[Z1][Z2+1]>0 AND FArray[Z1][Z2+1]=FArray[Z1+1][Z2] Then Goto LN90 EndIf AArray[K+1]=Z1+1 BArray[K+1]=Z2+1 LN530: EndFor Goto LN950 LN550: AArray[1]=A AArray[2]=7 AArray[3]=7 For K=1 To N If M>1 Then Goto LN640 EndIf If AArray[K]=6 Then Goto LN630 EndIf If FArray[AArray[K]+1][B]>0 Then Goto LN630 EndIf AArray[K+1]=AArray[K]+1 Goto LN680 LN630: M=2 LN640: If AArray[1]<AArray[2] AND AArray[1]<AArray[3] Then Z=AArray[1] EndIf If AArray[2]<AArray[1] AND AArray[2]<AArray[3] Then Z=AArray[2] EndIf If AArray[3]<AArray[1] AND AArray[3]<AArray[2] Then Z=AArray[3] EndIf If Z=1 Then Goto LN90 EndIf If FArray[Z-1][B]>0 Then Goto LN90 EndIf AArray[K+1]=Z-1 LN680: EndFor FArray[A][B]=9-2*I-J For K=1 To N FArray[AArray[K+1]][B]=FArray[A][B] EndFor Goto LN990 LN740: AArray[1]=A BArray[1]=B AArray[2]=7 AArray[3]=7 BArray[2]=0 BArray[3]=0 For K=1 To N If M>1 Then Goto LN870 EndIf If AArray[K]=6 OR BArray[K]=1 Then Goto LN860 EndIf If FArray[AArray[K]+1][BArray[K]-1]>0 Then Goto LN860 EndIf If FArray[AArray[K]+1][BArray[K]] > 0 AND FArray[AArray[K]+1][BArray[K]]=FArray[AArray[K]][BArray[K]-1] Then Goto LN860 EndIf AArray[K+1]=AArray[K]+1 BArray[K+1]=BArray[K]-1 Goto LN940 LN860: M=2 LN870: If AArray[1]<AArray[2] AND AArray[1]<AArray[3] Then Z1=AArray[1] EndIf If AArray[2]<AArray[1] AND AArray[2]<AArray[3] Then Z1=AArray[2] EndIf If AArray[3]<AArray[1] AND AArray[3]<AArray[2] Then Z1=AArray[3] EndIf If BArray[1]>BArray[2] AND BArray[1]>BArray[3] Then Z2=BArray[1] EndIf If BArray[2]>BArray[1] AND BArray[2]>BArray[3] Then Z2=BArray[2] EndIf If BArray[3]>BArray[1] AND BArray[3]>BArray[2] Then Z2=BArray[3] EndIf If Z1=1 OR Z2=6 Then Goto LN90 EndIf If FArray[Z1-1][Z2+1]>0 Then Goto LN90 EndIf If FArray[Z1][Z2+1]>0 AND FArray[Z1][Z2+1]=FArray[Z1-1][Z2] Then Goto LN90 EndIf AArray[K+1]=Z1-1 BArray[K+1]=Z2+1 LN940: EndFor LN950: FArray[A][B]=9-2*I-J For K=1 To N FArray[AArray[K+1]][BArray[K+1]]=FArray[A][B] EndFor LN990: EndFor EndFor TextWindow.WriteLine("") TextWindow.WriteLine("THE FOLLOWING CODE OF THE BAD GUYS' FLEET DISPOSITION") TextWindow.WriteLine("HAS BEEN CAPTURED BUT NOT DECODED: ") TextWindow.WriteLine("") For I=1 To 6 For J=1 To 6 HArray[I][J]=FArray[J][I] EndFor EndFor For I=1 To 6 For J=1 To 6 TextWindow.Write(HArray[I][J]) EndFor TextWindow.WriteLine("") EndFor TextWindow.WriteLine("") TextWindow.WriteLine("DE-CODE IT AND USE IT IF YOU CAN") TextWindow.WriteLine("BUT KEEP THE DE-CODING METHOD A SECRET.") TextWindow.WriteLine("") For I=1 To 6 For J=1 To 6 HArray[I][J]=0 EndFor EndFor For I=1 To 3 LArray[I]=0 EndFor CArray[1]=2 CArray[2]=2 CArray[3]=1 CArray[4]=1 CArray[5]=0 CArray[6]=0 S=0 H=0 TextWindow.WriteLine("START GAME") LN1180: Textwindow.write("Input X ") X = TextWindow.ReadNumber() Textwindow.write("Input Y ") Y = TextWindow.ReadNumber() If X<1 OR X>6 OR Math.Floor(X)<>Math.Abs(X) Then Goto LN1210 EndIf If Y>0 AND Y<7 AND Math.Floor(Y)=Math.abs(Y) Then Goto LN1230 EndIf LN1210: TextWindow.WriteLine("INVALID INPUT. TRY AGAIN.") Goto LN1180 LN1230: R=7-Y C=X If FArray[R][C]>0 Then Goto LN1290 EndIf S=S+1 TextWindow.WriteLine("SPLASH! TRY AGAIN.") Goto LN1180 LN1290: If CArray[FArray[R][C]]<4 Then Goto LN1340 EndIf TextWindow.WriteLine("THERE USED TO BE A SHIP AT THAT POINT, BUT YOU SUNK IT.") TextWindow.WriteLine("SPLASH! TRY AGAIN.") S=S+1 Goto LN1180 LN1340: If HArray[R][C]>0 Then Goto LN1420 EndIf H=H+1 HArray[R][C]=FArray[R][C] TextWindow.WriteLine("A DIRECT HIT ON SHIP NUMBER "+FArray[R][C]) CArray[FArray[R][C]]=CArray[FArray[R][C]]+1 If CArray[FArray[R][C]] >=4 Then Goto LN1470 EndIf TextWindow.WriteLine("TRY AGAIN.") Goto LN1180 LN1420: TextWindow.Write("YOU ALREADY PUT A HOLE IN SHIP NUMBER "+FArray[R][C]) TextWindow.WriteLine("AT THAT POINT.") TextWindow.WriteLine("SPLASH! TRY AGAIN.") S=S+1 Goto LN1180 LN1470: If (FArray[R][C] = 1 Or FArray[R][C] = 2) Then LArray[1] = LArray[1] + 1 ElseIf (FArray[R][C] = 3 Or FArray[R][C] = 4) Then LArray[2] = LArray[2] + 1 else LArray[3] = LArray[3] + 1 EndIf TextWindow.WriteLine("AND YOU SUNK IT. HURRAH FOR THE GOOD GUYS.") TextWindow.WriteLine("SO FAR, THE BAD GUYS HAVE LOST") TextWindow.Write(LArray[1]+" DESTROYER(S), "+LArray[2]+" CRUISER(S), AND ") TextWindow.WriteLine(LArray[3]+" AIRCRAFT CARRIER(S).") TextWindow.WriteLine("YOUR CURRENT SPLASH/HIT RATIO IS "+S/H) If (LArray[1]+LArray[2]+LArray[3])<6 Then Goto LN1180 EndIf TextWindow.WriteLine("") TextWindow.WriteLine("YOU HAVE TOTALLY WIPED OUT THE BAD GUYS' FLEET") TextWindow.WriteLine("WITH A FINAL SPLASH/HIT RATIO OF "+S/H) If S/H>0 Then Goto LN1590 EndIf TextWindow.WriteLine("CONGRATULATIONS -- A DIRECT HIT EVERY TIME.") LN1590: TextWindow.WriteLine("") TextWindow.WriteLine("****************************") TextWindow.WriteLine("") Goto LN50

 

Sample Run:

eBatil

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.