Depth Charge

July 5, 2016

Creative Computing was a staple of my programming education; I remember typing in the games to run on a friend’s computer. Here’s a game from the very first issue of the magazine, written by 18-year old Dana Noftle:

In this program, you are the captain of the destroyer, USS Digital. An enemy submarine has been causing trouble and your mission is to destroy it. You may select the size of the “cube” of water you wish to search in. The computer then determines how many depth charges you get to destroy the submarine.

Each depth charge is exploded by you specifying a trio of numbers; the first two are the surface coordinates, the third is the depth. After each depth charge, your sonar observer will tell you where the explosion was relative to the submarine.

Here’s a sample game:

DEPTH CHARGE GAME

DIMENSION OF SEARCH AREA? 10

YOU ARE CAPTAIN OF THE DESTROYER USS DIGITAL.
AN ENEMY SUB HAS BEEN CAUSING YOU TROUBLE. YOUR
MISSION IS TO DESTROY IT. YOU HAVE 4 SHOTS.
SPECIFY DEPTH CHARGE EXPLOSION POINT WITH A
TRIO OF NUMBERS -- THE FIRST TWO ARE THE
SURFACE COORDINATES; THE THIRD IS THE DEPTH.

GOOD LUCK !

TRIAL # 1 ? 5,5,5
SONAR REPORTS SHOT WAS SOUTHEAST AND TOO HIGH.

TRIAL # 2 ? 3,7,7
SONAR REPORTS SHOT WAS SOUTHEAST AND DEPTH OK.

TRIAL # 3 ? 1,9,7
SONAR REPORTS SHOT WAS NORTHEAST AND DEPTH OK.

TRIAL # 4 ? 0,8,7

B O O M ! !  YOU FOUND IT IN FOUR TRIES!

The first coordinate is east/west, the second coordinate is north/south. Here is the program that appeared in Creative Computing (pages 18-19):

10 PRINT "DEPTH CHARGE GAME" \ PRINT
20 INPUT "DIMENSION OF SEARCH AREA"; G \ PRINT
30 N = INT(LOG(G)/LOG(2))+1 \ RANDOMIZE
40 PRINT "YOU ARE CAPTAIN OF THE DESTROYER USS DIGITAL."
50 PRINT "AN ENEMY SUB HAS BEEN CAUSING YOU TROUBLE. YOUR"
60 PRINT "MISSION IS TO DESTROY IT.  YOU HAVE"N"SHOTS."
70 PRINT "SPECIFY DEPTH CHARGE EXPLOSION POINT WITH A"
80 PRINT "TRIO OF NUMBERS -- THE FIRST TWO ARE THE"
90 PRINT "SURFACE COORDINATES; THE THIRD IS THE DEPTH."
100 PRINT \ PRINT "GOOD LUCK !" \ PRINT
110 A=INT(G*RND) \ B=INT(G*RND) \ C=INT(G*RND)
120 FOR D=1 TO N \ PRINT \ PRINT "TRIAL #"D, \ INPUT X,Y,Z
130 IF ABS(X-A)+ABS(Y-B)+ABS(Z-C)=0 THEN 300
140 GOSUB 500 \ PRINT \ NEXT D
200 PRINT \ PRINT "YOU HAVE BEEN TORPEDOED! ABANDON SHIP!"
210 PRINT "THE SUBMARINE WAS AT"A","B","C" \ GOTO 400
300 PRINT \ PRINT "B O O M ! !  YOU FOUND IT IN "D"TRIES!"
400 PRINT \ PRINT \ INPUT "ANOTHER GAME (Y OR N)", A$
410 IF A$="Y" THEN 100
42O PRINT "OK.  HOPE YOU ENJOYED YOURSELF." \ GOTO 600
500 PRINT "SONAR REPORTS SHOT WAS ",
510 IF Y>B THEN PRINT "NORTH",
520 IF Y<B THEN PRINT "SOUTH",
530 IF X>A THEN PRINT "EAST",
540 IF X<A THEN PRINT "WEST",
550 IF Y<>B OR X<>A THEN PRINT " AND",
560 IF Z>C THEN PRINT " TOO LOW."
570 IF Z<C THEN PRINT " TOO HIGH."
580 IF Z=C THEN PRINT " DEPTH OK."
590 RETURN
600 END

Your task is to rewrite the program in your favored programming language. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Advertisement

Pages: 1 2

One Response to “Depth Charge”

  1. catalinc said

    F# version similar to original:

    
    open System
    
    let input() = Int32.Parse(Console.ReadLine())
    
    let rec game() =
        printfn "DEPTH CHARGE GAME"
        printfn "DIMENSION OF SEARCH AREA?"
        let g = input()
        let n = int ((log (float g)) / (log (float 2))) + 1
        printfn "YOU ARE CAPTAIN OF THE DESTROYER USS DIGITAL."
        printfn "AN ENEMY SUB HAS BEEN CAUSING YOU TROUBLE. YOUR"
        printfn "MISSION IS TO DESTROY IT. YOU HAVE %d SHOTS." n
        printfn "SPECIFY DEPTH CHARGE EXPLOSION POINT WITH A"
        printfn "TRIO OF NUMBERS -- THE FIRST TWO ARE THE"
        printfn "SURFACE COORDINATES; THE THIRD IS THE DEPTH."   
        printfn "GOOD LUCK !"
        let rnd = Random()
        let a, b, c = rnd.Next(1, g + 1), rnd.Next(1, g + 1), rnd.Next(1, g + 1)
        let report x y z =
            printf "SONAR REPORTS SHOT WAS "
            if y > b then printf "NORTH"
            if y < b then printf "SOUTH"
            if x > a then printf "EAST"
            if x < a then printf "WEST"
            if y <> b || x <> a then printf " AND "
            if z > c then printfn "TOO LOW."
            if z < c then printfn "TOO HIGH."
            if z = c then printfn "DEPTH OK." 
        let another() = 
            printfn "ANOTHER GAME (Y OR N)?"
            let o = Console.ReadLine().ToUpper()
            match o with 
            | "Y" -> game()
            | _   -> printfn "OK.  HOPE YOU ENJOYED YOURSELF."
                     exit 0
        for i = 1 to n do
            printfn "TRIAL #%d" i
            let x, y, z = input(), input(), input()
            if abs(a - x) = 0 && abs(b - y) = 0 && abs(z - c) = 0 then
                printfn "B O O M ! !  YOU FOUND IT IN %d TRIES!" i
                another()
            else report x y z
        printfn "YOU HAVE BEEN TORPEDOED! ABANDON SHIP!"
        printfn "THE SUBMARINE WAS AT %d %d %d" a b c
        another()
    
    game()
    
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: