# convert procedural to object

I “ported” this basic program from the book “Basic Computer Games - Microcomputer Edition” The origional basic program is credited to Dana Noftle. I did this mostly as a tutorial for learning python. It might not even work very well.

``````# Depth Charge
import math
import random

print "DEPTH CHARGE"
print "Depth Charge Game
You are the captain of the destroyer USS Blender.  An enemy sub has been causing you trouble.  Your mission is to destroy it.  Specify depth charge explosion point with a trio of numbers -- the first two are the surface coordinates; the third is the depth - Good Luck!"
p = 'y'
while p == 'y':
g = input("Dimension of search area: ")
n = int(math.log(g)/math.log(2))+1
a = int(g*random.random())
b = int(g*random.random())
c = int(g*random.random())
print "Enemy Degug: ", a, b, c
for d in range(1, (n+1)):
print "Charges: ", n+1-d
x = input("X: ")
y = input("Y: ")
z = input("Depth: ")
if math.fabs(x-a)+math.fabs(y-b)+math.fabs(z-c)==0:
print "B O O M ! ! You destroyed the enemy in", d, "tries."
break
elif d == n:
print; print "You have been torpedoed! ABANDON SHIP!!"
print "The submarine was at ", a, b, c
else:
print "Sonar reports that shot was "
if y&gt;b:
print "North"
if y&lt;b:
print "South"
if x&gt;a:
print "East"
if x&lt;a:
print "West"
if y!=b or x!=a:
print "and"
if z&gt;c:
print "Low"
if z&lt;c:
print "High"
if z==c:
print "Depth OK"
p = raw_input("Play game: ")
``````

My first attempt at converting from procedural to object orientated was 85% sucessfull but failed. Once I learn that I would like to make it 3D and a tutorial for using python in the game engine.

It seems like fun to me. I’ll post my hack job of OO when it works.

Btw, this old book has other great snippets like Super Star Trek, Lunar LEM Rocket and Orbit. These tasty little gems would be great for learning.