# HP Forums

Full Version: Moon Landing Simulator for HP-12c again
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

On 24 September Tony (nz) offered a lunar lander program for the HP-12c which follows HP-29c rocket lander program.

I watched Apollo 13 movie yesterday and after that I started to think about this old program again :-)

Here is another moon landing simulator program for the HP-12c. The calculation of crash and landing velocity of this program meets the laws of Newton's Physics better than the original games (HP-67 moon rocket lander, HP-29c moon rocket lander etc.) do. Besides that this program is some shorter and faster.

Rem: Only integer values for fuel burn are allowed.

```01  5
02  0
03  0
04  STO 1
05  5
06  0
07  CHS
08  STO 2
09  6
10  0
11  STO 3
12  1
13  CHS
14  RCL 2
15  RCL 1
16  EEX
17  4
18  /
19  -
20  X<=Y?
21  GTO 25
22  RCL 2
23  LST X
24  +
25  [f] 4
26  PSE
27  PSE
28  RCL 3
29  [f] 0
30  PSE
31  3
32  PSE
33  2
34  PSE
35  1
36  PSE
37  0
38  PSE
39  RCL 3
40  X<=Y?        burn>fuel?
41  X<>Y         yes, burn all fuel
42  RDN
43  STO-3
44  2            x=x0+v0+0.5*a=x0+v0+burn-2.5
45  .
46  5
47  -            0.5*a=burn-2.5
48  STO+1
49  ENTER
50  +
51  STO 4        a=2*burn-5 (gravity=-5)
52  RCL 2
53  STO+1
54  RDN
55  STO+2        v=v0+a
56  0
57  RCL 1
58  X<=Y?        new x<=0?
59  GTO 67       yes, calculate crash or landing velocity
60  RCL 3
61  X=0?         fuel=0?
62  GTO 64       yes, go to free fall
63  GTO 12
64  5            free fall a=-5
65  CHS
66  STO 4
67  RCL 2        calculate crash or landing velocity
68  ENTER
69  *
70  RCL 4
71  ENTER
72  +
73  RCL 1
74  *
75  -
76  SQRT
77  CHS
78  PSE
79  GTO 78
```

Edited: 5 Oct 2005, 12:14 a.m.

Kalevipoeg , thank you! This is brilliant. Lines 12-24 create the VV.0hhh very elegantly. Also I believe you are the first to calculate the correct crash velocity when the first iteration gives a new height as negative - you reverse time and bring us back to the surface with the correct speed!! Please move onto Tic-Tac-toe now:-)
Gene's page has an ingenious program that uses 3 by 3 magic square space to facilitate win/block calculations.

Cheers,
Tony

Slightly less intense digit,.,EEX, alternatives might be:

5 lines 14-18: RCL1 % % RCL2 X<>Y

12 lines 44-55: ENTER + 5 - STO4 RCL2 X<>Y STO+2 2 / + STO+1

One could even shave 1 line off this, if I count correctly<G>:

STO4 STO+4 5 STO-4 RCL2 RCL4 STO+2 2 / + STO+1

Down to 78 lines and falling<G>. This is fun. From the quality of your code and clarity of exposition I feel I know you from somewhere ;-)

Now I'm onto my third edit. It seems possible to remove more lines by allowing an empty craft to recycle once more. One could have 5 chs sto4 up at line 12. Then we can remove the 5 sto-4, and change the start to STO+4 STO+4 above in my last feeble attempt to shave lines - but that part is now down to 9 where it was 12.
Instead we can do the rcl3 x=0? test by "inserting" x=0 gto "67" after the rcl 3 at line 39.
Overall this may save us another 3 lines, but I admit the operation will be slightly different - we get to "see" an empty craft just one more time before the free-fall with R4=-5. Of course we could save another 2 by not testing for R3=0 at all - just keep seeing the free-fall second by second until we go underground at the end :-)
But that is too slow ... although one can burn the 60 and see oneself travel out into space before returning. But, too slow to watch. The best place for the R3=0 test is probably after it is displayed - before the burn count-down - then one at least gets to see the final VV.0hhh begore the free-fall. Yup, it seems to work fine here at 75 lines. Good night all. Thanks for the fun :-)

Here it is, in case anyone wants to try/tweak/untweak it.
Thanks again to Kalevipoeg - whoever he/she is - maybe an ex-astronaut :-) And also thanks to Gene for these game challenges.

```Keystrokes      |Display       | Comments
[f][P/R]        |              |
[f]CLEAR[PRGM]  |00-           |
5               |01-         5 |
0               |02-         0 |
0               |03-         0 |
[STO]1          |04-    44   1 | h=500ft
5               |05-         5 |
0               |06-         0 |
[CHS]           |07-        16 |
[STO]2          |08-    44   2 | V=-50ft/sec
6               |09-         6 |
0               |10-         0 |
[STO]3          |11-    44   3 | Fuel=60
5               |12-         5 |
[CHS]           |13-        16 |
[STO]4          |14-    44   4 | accel=-5ft/sec/sec
1               |15-         1 |
[CHS]           |16-        16 |
[RCL]1          |17-    45   1 |
[%]             |18-        25 |
[%]             |19-        25 | +hE-4=.0hhh
[RCL]2          |20-    45   2 |
[x<>y]          |21-        34 |
[-]             |22-        30 |
[g][x<=y]       |23-    43  34 |
[g][GTO]28      |24- 43,33  28 |
[RCL]2          |25-    45   2 |
[g][LSTx]       |26-    43  36 |
[+]             |27-        40 |
[f]4            |28-    42   4 |
[g][PSE]        |29-    43  31 | VV.0hhh
[g][PSE]        |30-    43  31 |
[RCL]3          |31-    45   3 | Fuel
[f]0            |32-    42   0 |
[g][PSE]        |33-    43  31 |
[g][x=0]        |34-    43  35 | no fuel left?
[g][GTO]63      |35- 43,33  63 | free fall
3               |36-         3 | count down
[g][PSE]        |37-    43  31 |
2               |38-         2 |
[g][PSE]        |39-    43  31 |
1               |40-         1 |
[g][PSE]        |41-    43  31 |
0               |42-         0 |
[g][PSE]        |43-    43  31 |
[RCL]3          |44-    45   3 |
[g][x<=y]       |45-    43  34 |
[x<>y]          |46-        34 |
[RDN]           |47-        33 |
[STO][-]3       |48- 44 30   3 | update Fuel
[STO][+]4       |49- 44 40   4 | update
[STO][+]4       |50- 44 40   4 | acceleration
[RCL]2          |51-    45   2 |
[RCL]4          |52-    45   4 |
[STO][+]2       |53- 44 40   2 | update V
2               |54-         2 |
[/]             |55-        10 |
[+]             |56-        40 |
[STO][+]1       |57- 44 40   1 | update h
0               |58-         0 |
[RCL]1          |59-    45   1 |
[g][x<=y]       |60-    43  34 | underground?
[g][GTO]63      |61- 43,33  63 | 're-calc' crash velocity
[g][GTO]12      |62- 43,33  12 | burn again
[RCL]2          |63-    45   2 |
[ENTER]         |64-        36 |
[x]             |65-        20 | v0^2
[RCL]4          |66-    45   4 |
[RCL]1          |67-    45   1 |
[x]             |68-        20 |
[ENTER]         |69-        36 |
[+]             |70-        40 | 2*a*h
[-]             |71-        30 |
[g][SQRT]       |72-    43  21 |
[CHS]           |73-        16 | V at h=0
[g][PSE]        |74-    43  31 |
[g][GTO]74      |75- 43,33  74 |
[f][P/R]        |              |
```
To find a final free-fall time in seconds, stop &:

[RCL]2 [-] [RCL]4 [/]

eg, if all 60 is burnt on first cycle, we obtain a final crash v of
-96ft/sec and time=32 sec. This is the longest trip and worst
crash :-)

Edited: 5 Oct 2005, 7:09 a.m.

Tony, thank you for comments and analyses!

For me the lines 39-43 were the biggest challenge of this program. Heureka! There is not always need to use GTO-command after conditional test. This is a good example of it!

Tic-tac-toe isnâ€™t my favourite but who knows...

And thanks to you for many additions to this program. Especially "1 CHS RCL 1 % % RCL 2 X<>Y -" is clever.

Edited: 5 Oct 2005, 2:02 p.m.

Hi Kalevipoeg - I learned something today after looking up your name on google. You must be from Estonia! :-)

I wanted to find a perfect landing fitting for your gem of a program and finally I found one with 14 integer burns! At the end V=h=Fuel=0.00 :-)

``` VV.0hhh  Fuel 3210  Burn
--------  ----       ----
-50.0500  60         1
-53.0449  59         1  (2)
under 400ft:
-56.0394  58         2
-57.0338  56         3  (5)
under 300ft:
-56.0281  53         3
-55.0226  50         4  (7)
under 200ft:
-52.0172  46         6
-45.0124  40         6  (12)
under 100ft:
-38.0082  34         7
-29.0049  27         8
-18.0025  19         9
- 5.0014  10         2
- 6.0008   8         3
- 5.0003   5         5  (34)
0 flashing
RCL 1,2,3 all show 0.000000000 :-)
```

I had a few pretty close landings after 10 burns and with some fuel left, but as for the rest I must say you can blame me for a few lunar craters :-)

Has anyone else had a perfect landing?

Cheers,
Tony

2, 0, 1, 0, 4, 2, 8, 7, 9, 17