Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
FOCAL (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Example code== The original Lunar Lander makes an excellent example for examining FOCAL code, as it uses most of the features of the language. This code is from the original, found on [https://www.cs.brandeis.edu/~storer/LunarLander/LunarLander.html Jim Storer's Lunar Lander page].<ref>{{cite web |title=Lunar Lander |url=https://www.cs.brandeis.edu/~storer/LunarLander/LunarLander/LunarLanderListing.jpg |website= Lunar Landing Game Related Documents}}</ref> {{vanchor|01.04}} T "CONTROL CALLING LUNAR MODULE. MANUAL CONTROL IS NECESSARY"! {{vanchor|01.06}} T "YOU MAY RESET FUEL RATE K EACH 10 SECS TO 0 OR ANY VALUE"! {{vanchor|01.08}} T "BETWEEN 8 & 200 LBS/SEC. YOU'VE 16000 LBS FUEL. ESTIMATED"! {{vanchor|01.11}} T "FREE FALL IMPACT TIME-120 SECS. CAPSULE WEIGHT-32500 LBS"! {{vanchor|01.20}} T "FIRST RADAR CHECK COMING UP"!!!;E {{vanchor|01.30}} T "COMMENCE LANDING PROCEDURE"!"TIME,SECS ALTITUDE," {{vanchor|01.40}} T "MILES+FEET VELOCITY,MPH FUEL,LBS FUEL RATE"! {{vanchor|01.50}} S A=120;S V=1;S M=32500;S N=16500;S G=.001;S Z=1.8 {{vanchor|02.10}} T " ",%3,L," ",FITR(A)," ",%4,5280*(A-FITR(A)) {{vanchor|02.20}} T %6.02," ",3600*V," ",%6.01,M-N," K=";A K;S T=10 {{vanchor|02.70}} T %7.02;I (200-K)2.72;I (8-K)3.1,3.1;I (K)2.72,3.1 {{vanchor|02.72}} T "NOT POSSIBLE";F X=1,51;T "." {{vanchor|02.73}} T "K=";A K;G 2.7 {{vanchor|03.10}} I (M-N-.001)4.1;I (T-.001)2.1;S S=T {{vanchor|03.40}} I ((N+S*K)-M)3.5,3.5;S S=(M-N)/K {{vanchor|03.50}} D 9;I (I)7.1,7.1;I (V)3.8,3.8;I (J)8.1 {{vanchor|03.80}} D 6;G 3.1 {{vanchor|04.10}} T "FUEL OUT AT",L," SECS"! {{vanchor|04.40}} S S=(FSQT(V*V+2*A*G)-V)/G;S V=V+G*S;S L=L+S {{vanchor|05.10}} T "ON THE MOON AT",L," SECS"!;S W=3600*V {{vanchor|05.20}} T "IMPACT VELOCITY OF",W,"M.P.H."!,"FUEL LEFT:"M-N," LBS"! {{vanchor|05.40}} I (1-W)5.5,5.5;T "PERFECT LANDING !-(LUCKY)"!;G 5.9 {{vanchor|05.50}} I (10-W)5.6,5.6;T "GOOD LANDING-(COULD BE BETTER)"!;G 5.9 {{vanchor|05.60}} I (22-W)5.7,5.7;T "CONGRATULATIONS ON A POOR LANDING"!;G 5.9 {{vanchor|05.70}} I (40-W)5.81,5.81;T "CRAFT DAMAGE. GOOD LUCK"!;G 5.9 {{vanchor|05.81}} I (60-W)5.82,5.82;T "CRASH LANDING-YOU'VE 5 HRS OXYGEN"!;G 5.9 {{vanchor|05.82}} T "SORRY,BUT THERE WERE NO SURVIVORS-YOU BLEW IT!"!"IN " {{vanchor|05.83}} T "FACT YOU BLASTED A NEW LUNAR CRATER",W*.277777," FT.DEEP." {{vanchor|05.90}} T !!!!"TRY AGAIN?"! {{vanchor|05.92}} A "(ANS. YES OR NO)"P;I (P-0NO)5.94,5.98 {{vanchor|05.94}} I (P-0YES)5.92,1.2,5.92 {{vanchor|05.98}} T "CONTROL OUT"!!!;Q {{vanchor|06.10}} S L=L+S;S T=T-S;S M=M-S*K;S A=I;S V=J {{vanchor|07.10}} I (S-.005)5.1;S S=2*A/(V+FSQT(V*V+2*A*(G-Z*K/M))) {{vanchor|07.30}} D 9;D 6;G 7.1 {{vanchor|08.10}} S W=(1-M*G/(Z*K))/2;S S=M*V/(Z*K*(W+FSQT(W*W+V/Z)))+.05;D 9 {{vanchor|08.30}} I (I)7.1,7.1;D 6;I (-J)3.1,3.1;I (V)3.1,3.1,8.1 {{vanchor|09.10}} S Q=S*K/M;S J=V+G*S+Z*(-Q-Q^2/2-Q^3/3-Q^4/4-Q^5/5) {{vanchor|09.40}} S I=A-G*S*S/2-V*S+Z*S*(Q/2+Q^2/6+Q^3/12+Q^4/20+Q^5/30) The program is cleanly separated into many subroutines. This was almost universal in FOCAL programs (and JOSS), as the line number scheme made such constructs easy to use. This program uses nine routines. The first, group 1, simply prints out the instructions using the {{code|T}}ype statement and sets the initial values for the run. The mass of fuel is not recorded directly, instead, it uses the current {{code|M}}ass and empty mass, {{code|N}}, so the remaining fuel is {{code|M-N}} and the lander runs out of fuel when {{code|M-N}} is 0. Also note the {{code|E}}rase at the end of line {{pslink|01.20}}, which resets all the variable values. The main game loop is driven by group 2. As the code "falls" through group 1 into group 2 during the first run, the initial values are printed out in the first two lines. Near the end of line {{pslink|02.20}}, the user is {{code|A}}sked to input the burn rate as {{code|K}}, and then the loop timer is reset using {{code|S T{{=}}10}}. Line {{pslink|02.70}} tests the user's input against several possibilities, if it is over 200 or below 8 it types "<samp>NOT POSSIBLE</samp>" and a row of periods, and then loops back to ask the user to try again. If the value is between these values, it jumps forward to group 3. Note that the limited capabilities of FOCAL's {{code|IF}} command are evident here, in BASIC this could be reduced to a single {{code|IF K>200 OR K<8 THEN...|basic}} Group 3 first tests to see if the fuel has run out, and jumps to group 4 if it has. Then it tests if the 10-second period in {{code|T}} has expired and, if so, loops back to {{pslink|02.10}} print everything out again, which has the side-effect of resetting {{mono|T}} and {{mono|S}} to 10. Line {{pslink|03.40}} tests to see if the amount of fuel burned this period, {{code|S*K}}, will reduce the mass of the vehicle as a whole, {{code|S*K-M}}, beyond the empty weight, N. If not, it moves on, if it will, it instead sets the loop timer to the amount of time the remaining fuel will burn, thus ending the loop early. In either case, it calls group 9 to update the velocity and position. It then loops over groups 7, 8 and 9 until the value of I converges. When the 10-second timer runs out, or it reaches the end due to the fuel test in line {{pslink|03.10}} or the altitude test in {{pslink|07.10}}. In the latter cases, it will jump to group 4 and fall through to group 5, or jump to group 5 directly. Group 5 types the end-of-game results and then asks the user if they'd like to try again. If so, it jumps to {{pslink|01.20}} to clear out all values and print the headers again, if not if falls through to {{pslink|05.98}} and {{code|Q}}uits.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)