|
| 1 | +degToRad = function(n) |
| 2 | + return n * pi / 180 |
| 3 | +end function |
| 4 | + |
| 5 | +radToDeg = function(n) |
| 6 | + return n * 180 / pi |
| 7 | +end function |
| 8 | + |
| 9 | +roundDown = function(n, r) |
| 10 | + return floor(n / r) * r |
| 11 | +end function |
| 12 | + |
| 13 | +getCoord = function(distance, radX, radZ) |
| 14 | + xc = sin(radZ)*cos(radX)*distance |
| 15 | + yc = sin(radZ)*sin(radX)*distance |
| 16 | + zc = cos(radZ)*distance |
| 17 | + return [xc,yc,zc] |
| 18 | +end function |
| 19 | + |
| 20 | +distanceBetween = function (d1,d2) |
| 21 | + return ((d1[0]-d2[0])^2 + (d1[1]-d2[1])^2 + (d1[2]-d2[2])^2)^.5 |
| 22 | +end function |
| 23 | + |
| 24 | +print " " * 33 + "TARGET" |
| 25 | +print " " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY" |
| 26 | +print; print; print |
| 27 | + |
| 28 | +print "You are the weapons officer on the Starship Enterprise" |
| 29 | +print "and this is a test to see how accurae a shot you" |
| 30 | +print "are in a 3-dimensional range. You will be told" |
| 31 | +print "the radian offset for the X and Z axes, the location" |
| 32 | +print "of the target in 3-dimensional rectangular coordinates," |
| 33 | +print "the approximate number of degrees from the X and Z" |
| 34 | +print "axes, and the approximate distance to the target." |
| 35 | +print "You will then proceed to shoot at the target until it is" |
| 36 | +print "destroyed!" |
| 37 | +print; print |
| 38 | +print "Good luck!" |
| 39 | +roundToList = [20,10,2,1] |
| 40 | +ready = true |
| 41 | +while ready |
| 42 | + turns = -1 |
| 43 | + radX = rnd * 2 * pi |
| 44 | + radZ = rnd * 2 * pi |
| 45 | + print "Radians from X axis = " + radX + " from Z axis = " + radZ |
| 46 | + |
| 47 | + distance = 100000 * rnd * rnd |
| 48 | + coords = getCoord(distance, radX, radZ) |
| 49 | + |
| 50 | + print "Target sighted: Approx Coordinates (X,Y,Z) = ("+coords.join(",")+")" |
| 51 | + |
| 52 | + gameRunning = true |
| 53 | + while gameRunning |
| 54 | + turns += 1 |
| 55 | + if turns >=4 then |
| 56 | + estDistance = distance |
| 57 | + else |
| 58 | + estDistance = roundDown(distance, roundToList[turns]) |
| 59 | + end if |
| 60 | + |
| 61 | + print " Estimated Distance: " + estDistance |
| 62 | + print |
| 63 | + tx = input ("Input angle deviation from X in degrees: ").val |
| 64 | + tz = input ("Input angle deviation from Z in degrees: ").val |
| 65 | + tdist = input ("Input distance: ").val |
| 66 | + print |
| 67 | + if tdist < 20 then |
| 68 | + print "You blew yourself up!!" |
| 69 | + gameRunning = false |
| 70 | + else |
| 71 | + tx = degToRad(tx) |
| 72 | + tz = degToRad(tz) |
| 73 | + |
| 74 | + print "Radians from X-axis = " + tx + " from Z-axis = " + tz |
| 75 | + targeted = getCoord(tdist, tx,tz) |
| 76 | + distBet = distanceBetween(coords, targeted) |
| 77 | + if distBet > 20 then |
| 78 | + dx = targeted[0] - coords[0] |
| 79 | + dy = targeted[1] - coords[1] |
| 80 | + dz = targeted[2] - coords[2] |
| 81 | + xMsg = {false: "Shot in front of target ", true: "Shot behind target "} |
| 82 | + print xMsg[dx<0] + dx + " kilometers." |
| 83 | + yMsg = {false: "Shot to left of target ", true: "Shot to right of target "} |
| 84 | + print yMsg[dy<0] + dy + " kilometers." |
| 85 | + zMsg = {false: "Shot above target ", true: "Shot below target "} |
| 86 | + print zMsg[dz<0] + dz + " kilometers." |
| 87 | + |
| 88 | + print "Approx position of explosion + (" + targeted.join(",") + ")" |
| 89 | + print " Distance from target = " + distBet |
| 90 | + print |
| 91 | + print |
| 92 | + |
| 93 | + else |
| 94 | + print |
| 95 | + print " * * * HIT * * * Target is non-functional" |
| 96 | + print |
| 97 | + print "Distance of explosion from target was " + distBet + "kilometers." |
| 98 | + print |
| 99 | + print "Mission accomplished in " + (turns+1) + " shots." |
| 100 | + print |
| 101 | + gameRunning = false |
| 102 | + end if |
| 103 | + end if |
| 104 | + end while |
| 105 | + print |
| 106 | + ans = input("Ready for next target? ").lower |
| 107 | + if ans == "" then |
| 108 | + ready == false |
| 109 | + else |
| 110 | + ready = ans[0].lower == "y" |
| 111 | + end if |
| 112 | + print |
| 113 | +end while |
0 commit comments