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): if not found_it: 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." if not found_it: 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.
F# version similar to original: