## Depth Charge

### July 5, 2016

That was considered good programming style at the time. My version is in Python, due to its similarity to the original BASIC program:

```def depth_charge(g):
from random import randint, seed
from math import log
print "DEPTH CHARGE GAME"; print
n = int(log(g)/log(2))+1; seed()
play_again = "Y"
print "YOU ARE CAPTAIN OF THE DESTROYER USS DIGITAL."
print "AN ENEMY SUB HAS BEEN CAUSING YOU TROUBLE. YOUR"
print "MISSION IS TO DESTROY IT.  YOU HAVE", n, "SHOTS."
print "SPECIFY DEPTH CHARGE EXPLOSION POINT WITH A"
print "TRIO OF NUMBERS -- THE FIRST TWO ARE THE"
print "SURFACE COORDINATES; THE THIRD IS THE DEPTH."
while play_again == "Y":
print; print "GOOD LUCK !"; print
a, b, c = randint(1,g), randint(1,g), randint(1,g)
found_it = False
for d in range(1,n+1):
x, y, z = input("TRIAL # " + str(d) + " ? ")
if a == int(x) and b == int(y) and c == int(z):
print "B O O M ! !  YOU FOUND IT IN", d, "TRIES!"
found_it = True
else:
print "SONAR REPORTS SHOT WAS",
if b < y: print "NORTH",
if y < b: print "SOUTH",
if a < x: print "EAST",
if x < a: print "WEST",
if y <> b or x <> a: print "AND",
if c < z: print "TOO LOW."
if z < c: print "TOO HIGH."
if c == z: print "DEPTH OK."
print "YOU HAVE BEEN TORPEDOED! ABANDON SHIP!"
print "THE SUBMARINE WAS AT", a, b, c
print; print; play_again = raw_input("ANOTHER GAME (Y OR N) ? ")
print "OK.  HOPE YOU ENJOYED YOURSELF."```

There are a few differences. The size of the playing cube is given as a parameter when the games is started, rather than prompting the user. And a new variable `found_it` has been introduced to replace some `goto`s in the original. Here’s a sample run:

```>>> depth_charge(10)
DEPTH CHARGE GAME

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 SOUTH EAST AND TOO LOW.
TRIAL # 2 ? 3,8,3
SONAR REPORTS SHOT WAS SOUTH WEST AND DEPTH OK.
TRIAL # 3 ? 4,9,3
B O O M ! !  YOU FOUND IT IN 3 TRIES!

ANOTHER GAME (Y OR N) ? N
OK.  HOPE YOU ENJOYED YOURSELF.```

The player solves the game by performing binary search on all three axes. You can run the program at http://ideone.com/GXxTlK.

Pages: 1 2

### One Response to “Depth Charge”

1. catalinc said

F# version similar to original:

```
open System

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)?"
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()

```