From 010fdc9c05d4e3fe4339cd2d128b8e25b6e000d2 Mon Sep 17 00:00:00 2001 From: Dave LeCompte Date: Sun, 7 Mar 2021 07:54:31 -0800 Subject: [PATCH] port TARGET to python Pretty straightforward port, a loop in a loop. This game is basically an exercise in converting radians to degrees. Possibly in the player's head. --- 86 Target/python/target.py | 172 +++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 86 Target/python/target.py diff --git a/86 Target/python/target.py b/86 Target/python/target.py new file mode 100644 index 00000000..68fd9521 --- /dev/null +++ b/86 Target/python/target.py @@ -0,0 +1,172 @@ +""" +TARGET + +Weapon targeting simulation / 3d trigonometry practice + +Ported by Dave LeCompte +""" + +import collections +import math +import random + +PAGE_WIDTH = 64 + + +def print_centered(msg): + spaces = " " * ((PAGE_WIDTH - len(msg)) // 2) + print(spaces + msg) + + +def print_header(title): + print_centered(title) + print_centered("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") + print() + print() + print() + + +def print_instructions(): + print("YOU ARE THE WEAPONS OFFICER ON THE STARSHIP ENTERPRISE") + print("AND THIS IS A TEST TO SEE HOW ACCURATE A SHOT YOU") + print("ARE IN A THREE-DIMENSIONAL RANGE. YOU WILL BE TOLD") + print("THE RADIAN OFFSET FOR THE X AND Z AXES, THE LOCATION") + print("OF THE TARGET IN THREE DIMENSIONAL RECTANGULAR COORDINATES,") + print("THE APPROXIMATE NUMBER OF DEGREES FROM THE X AND Z") + print("AXES, AND THE APPROXIMATE DISTANCE TO THE TARGET.") + print("YOU WILL THEN PROCEEED TO SHOOT AT THE TARGET UNTIL IT IS") + print("DESTROYED!") + print() + print("GOOD LUCK!!") + print() + print() + + +def prompt(): + while True: + response = input("INPUT ANGLE DEVIATION FROM X, DEVIATION FROM Z, DISTANCE? ") + if not ("," in response): + continue + + terms = response.split(",") + if len(terms) != 3: + continue + + return [float(t) for t in terms] + + +def next_target(): + for i in range(5): + print() + print("NEXT TARGET...") + print() + + +def describe_miss(x, y, z, x1, y1, z1, d): + x2 = x1 - x + y2 = y1 - y + z2 = z1 - z + + if x2 < 0: + print(f"SHOT BEHIND TARGET {-x2:.2f} KILOMETERS.") + else: + print(f"SHOT IN FRONT OF TARGET {x2:.2f} KILOMETERS.") + + if y2 < 0: + print(f"SHOT TO RIGHT OF TARGET {-y2:.2f} KILOMETERS.") + else: + print(f"SHOT TO LEFT OF TARGET {y2:.2f} KILOMETERS.") + + if z2 < 0: + print(f"SHOT BELOW TARGET {-z2:.2f} KILOMETERS.") + else: + print(f"SHOT ABOVE TARGET {z2:.2f} KILOMETERS.") + + print(f"APPROX POSITION OF EXPLOSION: X={x1:.4f} Y={y1:.4f} Z={z1:.4f}") + print(f" DISTANCE FROM TARGET = {d:.2f}") + print() + print() + print() + + +def do_shot_loop(p1, x, y, z): + shot_count = 0 + while True: + shot_count += 1 + if shot_count == 1: + p3 = int(p1 * 0.05) * 20 + elif shot_count == 2: + p3 = int(p1 * 0.1) * 10 + elif shot_count == 3: + p3 = int(p1 * 0.5) * 2 + elif shot_count == 4: + p3 = int(p1) + else: + p3 = p1 + + if p3 == int(p3): + print(f" ESTIMATED DISTANCE: {p3}") + else: + print(f" ESTIMATED DISTANCE: {p3:.2f}") + print() + a1, b1, p2 = prompt() + + if p2 < 20: + print("YOU BLEW YOURSELF UP!!") + return + + a1 = math.radians(a1) + b1 = math.radians(b1) + show_radians(a1, b1) + + x1 = p2 * math.sin(b1) * math.cos(a1) + y1 = p2 * math.sin(b1) * math.sin(a1) + z1 = p2 * math.cos(b1) + + distance = math.sqrt((x1 - x) ** 2 + (y1 - y) ** 2 + (z1 - z) ** 2) + + if distance <= 20: + print() + print(" * * * HIT * * * TARGET IS NON FUNCTIONAL") + print() + print(f"DISTANCE OF EXPLOSION FROM TARGET WAS {d:.4f} KILOMETERS") + print() + print(f"MISSION ACCOMPLISHED IN {r} SHOTS.") + + return + else: + describe_miss(x, y, z, x1, y1, z1, distance) + + +def show_radians(a, b): + print(f"RADIANS FROM X AXIS = {a:.4f} FROM Z AXIS = {b:.4f}") + + +def play_game(): + while True: + a = random.uniform(0, 2 * math.pi) # random angle + b = random.uniform(0, 2 * math.pi) # random angle + + show_radians(a, b) + + p1 = random.uniform(0, 100000) + random.uniform(0, 1) + x = math.sin(b) * math.cos(a) * p1 + y = math.sin(b) * math.sin(a) * p1 + z = math.cos(b) * p1 + print( + f"TARGET SIGHTED: APPROXIMATE COORDINATES: X={x:.1f} Y={y:.1f} Z={z:.1f}" + ) + + do_shot_loop(p1, x, y, z) + next_target() + + +def main(): + print_header("TARGET") + print_instructions() + + play_game() + + +if __name__ == "__main__": + main()