improve readability

This commit is contained in:
Jack Boyce
2021-03-01 22:26:09 -08:00
parent 52c0db4cf2
commit 2e2c322baa

View File

@@ -23,7 +23,7 @@ from math import sqrt
def fnr(): def fnr():
# Generate a random number from 0 to 7 inclusive. # Generate a random integer from 0 to 7 inclusive.
return random.randint(0, 7) return random.randint(0, 7)
@@ -80,7 +80,7 @@ def find_empty_place():
def navigation(): def navigation():
# Take navigation input and move the Enterprise. # Take navigation input and move the Enterprise.
global d, s, e, k, s1, s2, qs, t8, t9, t, w1, c, q1, q2 global d, s, e, k, qs, t, q1, q2, s1, s2
while True: while True:
c1s = input("COURSE (1-9)? ") c1s = input("COURSE (1-9)? ")
@@ -93,26 +93,25 @@ def navigation():
print(" LT. SULU REPORTS, 'INCORRECT COURSE DATA, SIR!'") print(" LT. SULU REPORTS, 'INCORRECT COURSE DATA, SIR!'")
return return
xs = '0.2' if d[0] < 0 else '8'
while True: while True:
w1s = input(f"WARP FACTOR (0-{xs})? ") warps = input(f"WARP FACTOR (0-{'0.2' if d[0] < 0 else '8'})? ")
if len(w1s) > 0: if len(warps) > 0:
w1 = float(w1s) warp = float(warps)
break break
if d[0] < 0 and w1 > 0.2: if d[0] < 0 and warp > 0.2:
print("WARP ENGINES ARE DAMAGED. MAXIMUM SPEED = WARP 0.2") print("WARP ENGINES ARE DAMAGED. MAXIMUM SPEED = WARP 0.2")
return return
if w1 == 0: if warp == 0:
return return
if w1 < 0 or w1 > 8: if warp < 0 or warp > 8:
print(" CHIEF ENGINEER SCOTT REPORTS 'THE ENGINES WON'T TAKE " print(" CHIEF ENGINEER SCOTT REPORTS 'THE ENGINES WON'T TAKE "
f"WARP {w1}!'") f"WARP {warp}!'")
return return
n = int(w1 * 8 + 0.5) n = round(warp * 8)
if e < n: if e < n:
print("ENGINEERING REPORTS 'INSUFFICIENT ENERGY AVAILABLE") print("ENGINEERING REPORTS 'INSUFFICIENT ENERGY AVAILABLE")
print(f" FOR MANEUVERING AT WARP {w1}!'") print(f" FOR MANEUVERING AT WARP {warp}!'")
if s >= n - e and d[6] >= 0: if s >= n - e and d[6] >= 0:
print(f"DEFLECTOR CONTROL ROOM ACKNOWLEDGES {s} UNITS OF ENERGY") print(f"DEFLECTOR CONTROL ROOM ACKNOWLEDGES {s} UNITS OF ENERGY")
print(" PRESENTLY DEPLOYED TO SHIELDS.") print(" PRESENTLY DEPLOYED TO SHIELDS.")
@@ -127,18 +126,15 @@ def navigation():
klingons_fire() klingons_fire()
# print damage control report # repair damaged devices and print damage report
d1 = 0
line = '' line = ''
d6 = 1 if w1 >= 1 else w1
for i in range(8): for i in range(8):
if d[i] < 0: if d[i] < 0:
d[i] += d6 d[i] += min(warp, 1)
if d[i] > -0.1 and d[i] < 0: if -0.1 < d[i] < 0:
d[i] = -0.1 d[i] = -0.1
elif d[i] >= 0: elif d[i] >= 0:
if d1 != 1: if len(line) == 0:
d1 = 1
line = "DAMAGE CONTROL REPORT:" line = "DAMAGE CONTROL REPORT:"
line += ' ' + devices[i] + ' REPAIR COMPLETED\n' line += ' ' + devices[i] + ' REPAIR COMPLETED\n'
if len(line) > 0: if len(line) > 0:
@@ -157,41 +153,41 @@ def navigation():
insert_marker(int(s1), int(s2), ' ') insert_marker(int(s1), int(s2), ' ')
ic1 = int(c1) ic1 = int(c1)
x1 = c[ic1 - 1][0] + (c[ic1][0] - c[ic1 - 1][0]) * (c1 - ic1) x1 = c[ic1 - 1][0] + (c[ic1][0] - c[ic1 - 1][0]) * (c1 - ic1)
x, y = s1, s2
x2 = c[ic1 - 1][1] + (c[ic1][1] - c[ic1 - 1][1]) * (c1 - ic1) x2 = c[ic1 - 1][1] + (c[ic1][1] - c[ic1 - 1][1]) * (c1 - ic1)
q4, q5 = q1, q2 q1_start, q2_start = q1, q2
for i in range(1, n + 1): x, y = s1, s2
for _ in range(n):
s1 += x1 s1 += x1
s2 += x2 s2 += x2
if s1 < 0 or s1 > 7 or s2 < 0 or s2 > 7: if s1 < 0 or s1 > 7 or s2 < 0 or s2 > 7:
# exceeded quadrant limits # exceeded quadrant limits; calculate final position
x += 8 * q1 + n * x1 x += 8 * q1 + n * x1
y += 8 * q2 + n * x2 y += 8 * q2 + n * x2
q1 = int(x / 8) q1, q2 = int(x / 8), int(y / 8)
q2 = int(y / 8) s1, s2 = int(x - q1 * 8), int(y - q2 * 8)
s1 = int(x - q1 * 8)
s2 = int(y - q2 * 8)
if s1 < 0: if s1 < 0:
q1 -= 1 q1 -= 1
s1 = 7 s1 = 7
if s2 < 0: if s2 < 0:
q2 -= 1 q2 -= 1
s2 = 7 s2 = 7
x5 = 0
hit_edge = False
if q1 < 0: if q1 < 0:
x5 = 1 hit_edge = True
q1 = s1 = 0 q1 = s1 = 0
if q1 > 7: if q1 > 7:
x5 = 1 hit_edge = True
q1 = s1 = 7 q1 = s1 = 7
if q2 < 0: if q2 < 0:
x5 = 1 hit_edge = True
q2 = s2 = 0 q2 = s2 = 0
if q2 > 7: if q2 > 7:
x5 = 1 hit_edge = True
q2 = s2 = 7 q2 = s2 = 7
if x5 == 1: if hit_edge:
print("LT. UHURA REPORTS MESSAGE FROM STARFLEET COMMAND:") print("LT. UHURA REPORTS MESSAGE FROM STARFLEET COMMAND:")
print(" 'PERMISSION TO ATTEMPT CROSSING OF GALACTIC " print(" 'PERMISSION TO ATTEMPT CROSSING OF GALACTIC "
"PERIMETER") "PERIMETER")
@@ -202,17 +198,17 @@ def navigation():
if t > t0 + t9: if t > t0 + t9:
end_game(won=False, quit=False) end_game(won=False, quit=False)
return return
if 8 * q1 + q2 == 8 * q4 + q5:
if q1 == q1_start and q2 == q2_start:
break break
t += 1 t += 1
maneuver_energy(n) maneuver_energy(n)
new_quadrant() new_quadrant()
return return
else: else:
s8 = int(s1) * 24 + int(s2) * 3 pos = int(s1) * 24 + int(s2) * 3
if qs[s8:(s8 + 2)] != ' ': if qs[pos:(pos + 2)] != ' ':
s1 = int(s1 - x1) s1, s2 = int(s1 - x1), int(s2 - x2)
s2 = int(s2 - x2)
print("WARP ENGINES SHUT DOWN AT SECTOR " print("WARP ENGINES SHUT DOWN AT SECTOR "
f"{s1 + 1} , {s2 + 1} DUE TO BAD NAVAGATION") f"{s1 + 1} , {s2 + 1} DUE TO BAD NAVAGATION")
break break
@@ -222,10 +218,7 @@ def navigation():
insert_marker(int(s1), int(s2), '<*>') insert_marker(int(s1), int(s2), '<*>')
maneuver_energy(n) maneuver_energy(n)
t8 = 1 t += 0.1 * int(10 * warp) if warp < 1 else 1
if w1 < 1:
t8 = 0.1 * int(10 * w1)
t += t8
if t > t0 + t9: if t > t0 + t9:
end_game(won=False, quit=False) end_game(won=False, quit=False)
return return
@@ -742,8 +735,8 @@ def print_direction(from1, from2, to1, to2):
def startup(): def startup():
# Initialize the game variables and map, and print startup messages. # Initialize the game variables and map, and print startup messages.
global g, z, k, d, t, t0, t9, docked, e, e0, p, p0, s, k9, b9, s9, c global g, z, d, t, t0, t9, docked, e, e0, p, p0, s, k9, b9, s9, c
global devices, q1, q2, s1, s2, k7, restart global devices, q1, q2, s1, s2, k7
print("\n\n\n\n\n\n\n\n\n\n\n" print("\n\n\n\n\n\n\n\n\n\n\n"
" ,------*------,\n" " ,------*------,\n"
@@ -757,11 +750,10 @@ def startup():
# set up global game variables # set up global game variables
g = [[0] * 8 for _ in range(8)] # galaxy map g = [[0] * 8 for _ in range(8)] # galaxy map
z = [[0] * 8 for _ in range(8)] # charted galaxy map z = [[0] * 8 for _ in range(8)] # charted galaxy map
k = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] # Klingons in current quadrant
d = [0] * 8 # damage stats for devices d = [0] * 8 # damage stats for devices
t = t0 = 100 * random.randint(20, 39) # stardate (current, initial) t = t0 = 100 * random.randint(20, 39) # stardate (current, initial)
t9 = random.randint(25, 34) # mission duration (stardates) t9 = random.randint(25, 34) # mission duration (stardates)
docked = False # docking flag docked = False # true when docked at starbase
e = e0 = 3000 # energy (current, initial) e = e0 = 3000 # energy (current, initial)
p = p0 = 10 # torpedoes (current, initial) p = p0 = 10 # torpedoes (current, initial)
s = 0 # shields s = 0 # shields
@@ -821,20 +813,20 @@ def startup():
def new_quadrant(): def new_quadrant():
# Enter a new quadrant: populate map and print a short range scan. # Enter a new quadrant: populate map and print a short range scan.
global g, z, k, t, t0, s9, q1, q2, s1, s2 global g, z, t, t0, s9, q1, q2, s1, s2
global k3, b3, s3, d4, qs, b4, b5 global k3, b3, s3, d4, k, qs, b4, b5
k3 = b3 = s3 = 0 k3 = b3 = s3 = 0
d4 = 0.5 * random.random() d4 = 0.5 * random.random()
z[q1][q2] = g[q1][q2] z[q1][q2] = g[q1][q2]
if 0 <= q1 < 8 and 0 <= q2 < 8: if 0 <= q1 <= 7 and 0 <= q2 <= 7:
g2s = quadrant_name(q1, q2, False) quad = quadrant_name(q1, q2, False)
if t == t0: if t == t0:
print("\nYOUR MISSION BEGINS WITH YOUR STARSHIP LOCATED") print("\nYOUR MISSION BEGINS WITH YOUR STARSHIP LOCATED")
print(f"IN THE GALACTIC QUADRANT, '{g2s}'.\n") print(f"IN THE GALACTIC QUADRANT, '{quad}'.\n")
else: else:
print(f"\nNOW ENTERING {g2s} QUADRANT . . .\n") print(f"\nNOW ENTERING {quad} QUADRANT . . .\n")
k3 = g[q1][q2] // 100 k3 = g[q1][q2] // 100
b3 = g[q1][q2] // 10 - 10 * k3 b3 = g[q1][q2] // 10 - 10 * k3
@@ -845,18 +837,15 @@ def new_quadrant():
if s <= 200: if s <= 200:
print(" SHIELDS DANGEROUSLY LOW") print(" SHIELDS DANGEROUSLY LOW")
for i in range(3): k = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] # Klingons in current quadrant
k[i] = [0, 0, 0] qs = ' ' * 192 # quadrant string
qs = ' ' * 192
# build quadrant string # build quadrant string
insert_marker(s1, s2, '<*>') insert_marker(s1, s2, '<*>')
if k3 > 0: for i in range(k3):
for i in range(k3): r1, r2 = find_empty_place()
r1, r2 = find_empty_place() insert_marker(r1, r2, '+K+')
insert_marker(r1, r2, '+K+') k[i] = [r1, r2, s9 * (0.5 + random.random())]
k[i] = [r1, r2, s9 * (0.5 + random.random())]
if b3 > 0: if b3 > 0:
b4, b5 = find_empty_place() b4, b5 = find_empty_place()
insert_marker(b4, b5, '>!<') insert_marker(b4, b5, '>!<')
@@ -913,7 +902,7 @@ while True:
while not restart: while not restart:
if s + e <= 10 or (e <= 10 and d[6] != 0): if s + e <= 10 or (e <= 10 and d[6] != 0):
print("\n** FATAL ERROR ** YOU'VE JUST STRANDED YOUR SHIP IN " print("\n** FATAL ERROR ** YOU'VE JUST STRANDED YOUR SHIP IN "
"SPACE\nYOU HAVE INSUFFICIENT MANEUVERING ENERGY, AND " "SPACE.\nYOU HAVE INSUFFICIENT MANEUVERING ENERGY, AND "
"SHIELD CONTROL\nIS PRESENTLY INCAPABLE OF CROSS-CIRCUITING " "SHIELD CONTROL\nIS PRESENTLY INCAPABLE OF CROSS-CIRCUITING "
"TO ENGINE ROOM!!") "TO ENGINE ROOM!!")