Added new port TinyBasic

(An improved version in conjunction with ktcc can generate executable files.)

git-svn-id: svn://kolibrios.org@8733 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
turbocat
2021-05-23 15:55:49 +00:00
parent 4a09257a8f
commit 43795ab11a
34 changed files with 7387 additions and 0 deletions

View File

@@ -0,0 +1,160 @@
REM
REM --- Tiny BASIC Interpreter and Compiler Project
REM --- Hammurabi Demonstration Game
REM
REM --- Released as Public Domain by Damian Gareth Walker, 2019
REM --- Created: 25-Aug-2019
REM
REM --- Variable List
REM
REM A - A random number returned by the Random Number Generator
REM B - The amount of land bought/sold by the player
REM D - The number of people who died of starvation
REM E - Average percentage of deaths
REM F - The amount of grain fed to the people
REM G - The quantity of stored grain
REM I - The number of immigrants on a given turn
REM L - How much land the city owns
REM M - Maximum land that can be planted
REM P - The number of people in the city
REM R - The amount of grain eaten by rats
REM S - The amount of grain planted as seed
REM T - Time played
REM V - The value of land per acre
REM Y - The grain yield of each piece of land
REM Z - The random number seed
REM --- Initialise the random number seed
PRINT "Think of a number."
INPUT Z
REM --- Initialise the game
LET D=0
LET E=0
LET G=2800
LET I=5
LET L=1000
LET P=100
LET R=200
LET T=1
LET Y=3
REM --- Print the report
15 PRINT "Hammurabi, I beg to report to you,"
PRINT "In year ",T,", ",D," people starved,"
PRINT "and ",I," came to the city."
PRINT "You harvested ",Y," bushels of grain per acre."
PRINT "Rats destroyed ",R," bushels."
PRINT "Population is now ",P,","
PRINT "and the city owns ",L," acres of land."
PRINT "You have ",G," bushels of grain in store."
IF D>P*45/100 THEN GOTO 100
IF T=11 THEN GOTO 90
REM --- Buy land
GOSUB 250
LET V=17+A-A/10*10
PRINT "Land is trading at ",V," bushels per acre."
30 PRINT "How many acres do you wish to buy (0-",G/V,")?"
INPUT B
IF B>G/V THEN GOTO 30
IF B>0 THEN GOTO 40
REM --- Sell land
35 PRINT "How many acres do you wish to sell (0-",L,")?"
INPUT B
IF B>L THEN GOTO 35
LET B=-B
REM --- Feed the people
40 PRINT "How many bushels to feed the people (0-",G-B*V,")?"
INPUT F
IF F>=0 THEN IF F<=G-B*V THEN GOTO 45
GOTO 40
REM --- Plant with seed
45 LET M=2*(G-F-B*V)
IF 10*P<M THEN LET M=10*P
IF L+B<M THEN LET M=L+B
50 PRINT "How many acres do you wish to plant with seed (0-",M,")?"
INPUT S
IF S>M THEN GOTO 50
REM --- Work out the result
LET L=L+B
LET G=G-B*V-F-S/2
REM Yield
GOSUB 250
LET Y=1+A-A/5*5
REM Rats
GOSUB 250
LET A=1+A-A/5*5
LET R=0
IF A>2 THEN GOTO 70
GOSUB 250
IF G>0 THEN LET R=1+A-A/G*G
REM Recalculate grain
70 LET G=G+S*Y-R
IF G<0 THEN LET G=0
REM Immigration/Birth
GOSUB 250
LET A=1+A-A/5*5
LET I=A*(L+S/20)/P/5+1
REM Feeding the people
LET D=0
IF P<=F/20 THEN GOTO 80
LET D=P-F/20
LET E=((T-1)*E+(100*D/P))/(T+1)
80 LET P=P-D+I
IF P<=0 THEN GOTO 210
LET T=T+1
REM Back to report
PRINT ""
GOTO 15
REM --- Evaluation
90 PRINT "Your reign ends after ",T-1," years."
PRINT "You leave your city with ",P," people."
PRINT "You have ",L," acres of land to support them."
PRINT G," bushels of grain remain in store."
PRINT ""
IF E<=3 THEN IF L/P>=10 THEN GOTO 110
IF E<=10 THEN IF L/P>=9 THEN GOTO 120
IF E<=33 THEN IF L/P>=7 THEN GOTO 130
REM --- Terrible performance - including premature end
100 PRINT "Your performance has been so terrible that"
PRINT "you were driven from your throne after ",T," years!"
END
REM --- Best performance
110 PRINT "Your expert statesmanship is worthy of Hammurabi himself!"
PRINT "The city will honour your memory for all eternity."
END
REM --- Average performance
120 PRINT "Your competent rule is appreciated by your citizens."
PRINT "They will remember you fondly for some time to come."
END
REM --- Poor performance
130 PRINT "Your mismanagement left your city in a very poor state."
PRINT "Your incompetence and oppression will not be missed by your people."
END
REM --- Everybody starved
210 PRINT "You have starved your entire kingdom to death!"
END
REM --- Random Number Generator
250 LET Z=5*Z+35
LET Z=Z-Z/4096*4096
LET A=Z
RETURN

View File

@@ -0,0 +1,67 @@
REM
REM --- Tiny BASIC Interpreter and Compiler Project
REM --- Hunt the Hurkle Demostration Game
REM
REM --- Released as Public Domain by Damian Gareth Walker 2019
REM --- Created: 11-Aug-2019
REM
REM --- Variables
REM G: hurkle column
REM H: hurkle row
REM M: moves taken
REM R: random number seed
REM X: player guess column
REM Y: player guess row
REM --- Initialise the random number generator
PRINT "Think of a number."
INPUT R
IF R<0 THEN LET R=0
IF R>4095 THEN LET R=4095
REM --- Initialise the game
GOSUB 200
LET G=R-(R/10*10)
GOSUB 200
LET H=R-(R/10*10)
LET M=0
REM --- Input player guess
30 PRINT "Where is the hurkle? Enter column then row."
INPUT X,Y
IF X>=0 THEN IF X<=9 THEN IF Y>=0 THEN IF Y<=9 THEN GOTO 40
PRINT "That location is off the grid!"
GOTO 30
REM --- Process player guess
40 LET M=M+1
PRINT "The Hurkle is..."
IF G<X THEN IF H<Y THEN PRINT "...to the northwest."
IF G=X THEN IF H<Y THEN PRINT "...to the north."
IF G>X THEN IF H<Y THEN PRINT "...to the northeast."
IF G>X THEN IF H=Y THEN PRINT "...to the east."
IF G>X THEN IF H>Y THEN PRINT "...to the southeast."
IF G=X THEN IF H>Y THEN PRINT "...to the south."
IF G<X THEN IF H>Y THEN PRINT "...to the southwest."
IF G<X THEN IF H=Y THEN PRINT "...to the west."
IF G=X THEN IF H=Y THEN GOTO 60
IF M>6 THEN GOTO 70
PRINT "You have taken ",M," turns so far."
GOTO 30
REM --- Player has won
60 PRINT "...RIGHT HERE!"
PRINT "You took ",M," turns to find it."
END
REM --- Player has lost
70 PRINT "You have taken too long over this. You lose!"
END
REM --- Random number generator
REM Input: R - current seed
REM Outputs: R - updated seed
200 LET R=5*R+35
LET R=R-R/4096*4096
RETURN

View File

@@ -0,0 +1,48 @@
REM
REM --- Tiny BASIC Interpreter and Compiler Project
REM --- Lunar Lander Demonstration Game
REM
REM --- Released as Public Domain by Damian Gareth Walker 2019
REM --- Created: 15-Aug-2019
REM
REM --- Variables:
REM A: altitude
REM B: fuel to burn this turn
REM F: fuel remaining
REM T: time elapsed
REM V: velocity this turn
REM W: velocity next turn
REM --- Initialise the Program
LET A=1000
LET B=0
LET F=150
LET V=50
LET T=0
REM --- Main Loop
100 PRINT "Time:",T," Alt:",A," Velocity:",V," Fuel:",F," Thrust:",B
111 IF F>30 THEN PRINT "Thrust (0-30)?"
IF F<31 THEN PRINT "Thrust (0-",F,")?"
INPUT B
IF B>=0 THEN IF B<=30 THEN IF B<=F THEN GOTO 120
GOTO 111
120 LET W=V-B+5
LET F=F-B
LET A=A-(V+W)/2
LET V=W
LET T=T+1
IF A>0 THEN GOTO 100
REM --- End of Game
IF V<5 THEN GOTO 140
PRINT "You crashed!"
GOTO 160
140 IF A<0 THEN GOTO 150
PRINT "Perfect landing!"
GOTO 160
150 PRINT "Touchdown."
160 IF A<0 THEN LET A=0
PRINT "Time:",T," Alt:",A," Velocity:",V," Fuel:",F
END

View File

@@ -0,0 +1,75 @@
REM
REM --- Tiny BASIC Interpreter and Compiler Project
REM --- Mugwump Demonstration Game
REM
REM --- Released as Public Domain by Damian Gareth Walker 2019
REM --- Created: 13-Aug-2019
REM
REM --- Variables
REM C: axis diff between player guess and mugwump position
REM D: distance between player guess and mugwump position
REM G: mugwump column
REM H: mugwump row
REM M: moves taken
REM R: random number generator seed
REM X: player guess column
REM Y: player guess row
REM --- Initialise the random number generator
PRINT "Think of a number."
INPUT R
IF R<0 THEN LET R=0
IF R>4095 THEN LET R=4095
REM --- Initialise the game
GOSUB 200
LET G=R-(R/10*10)
GOSUB 200
LET H=R-(R/10*10)
LET M=0
REM --- Input player guess
10 PRINT "Where is the mugwump? Enter column then row."
INPUT X,Y
IF X>=0 THEN IF X<=9 THEN IF Y>=0 THEN IF Y<=9 THEN GOTO 20
PRINT "That location is off the grid!"
GOTO 10
REM --- Process player guess
20 LET M=M+1
PRINT "The mugwump is..."
LET D=0
LET C=G-X
GOSUB 60
LET C=H-Y
GOSUB 60
IF D=0 THEN GOTO 40
PRINT "...",D," cells away."
IF M>10 THEN GOTO 50
PRINT "You have taken ",M," turns so far."
GOTO 10
REM --- Player has won
40 PRINT "...RIGHT HERE!"
PRINT "You took ",M," turns to find it."
END
REM --- Player has lost
50 PRINT "You have taken too long over this. You lose!"
END
REM --- Helper subroutine to calculate distance from player to mugwump
REM Inputs: C - difference in rows or columns
REM D - running total distance
REM Output: D - running total distance, updated
60 IF C<0 THEN LET C=-C
LET D=D+C
RETURN
REM --- Random number generator
REM Input: R - current seed
REM Outputs: R - updated seed
200 LET R=5*R+35
LET R=R-R/4096*4096
RETURN

View File

@@ -0,0 +1,204 @@
REM
REM Tiny BASIC Interpreter and Compiler Project
REM Tic-tac-toe Sample Game
REM
REM Released as public domain by Damian Gareth Walker, 2019
REM Created: 21-Sep-2019
REM
REM --- Variables
REM A - first square in line examined
REM B - second square in line examined
REM C - third square in line examined
REM D - player whose pieces to count
REM E - number of D's pieces on a line
REM F - first square of line to examine
REM G - game winner
REM H - which side the human takes
REM I - increment for line to examine
REM L - line to examine
REM M - where to move (various uses)
REM N - piece found in a square
REM P - player currently playing
REM Q - square to examine
REM R-Z - contents of the board
REM --- Main Program
GOSUB 40
GOSUB 60
GOSUB 80
END
REM --- Subroutine to initialise the game
REM Outputs: H - Human play order
REM P - Whose turn it is
40 PRINT "Tic tac toe. Board positions are:"
PRINT " 1 2 3"
PRINT " 4 5 6"
PRINT " 7 8 9"
PRINT "Play first or second (1/2)?"
INPUT H
IF H<1 THEN GOTO 40
IF H>2 THEN GOTO 40
LET P=1
RETURN
REM --- Subroutine to take turns
REM Inputs: H - who is the human
REM P - whose turn it is
REM Outputs: G - who won the game
60 IF P=H THEN GOSUB 100
IF P<>H THEN GOSUB 120
GOSUB 200
IF G>0 THEN RETURN
LET P=3-P
IF R=0 THEN GOTO 60
IF S=0 THEN GOTO 60
IF T=0 THEN GOTO 60
IF U=0 THEN GOTO 60
IF V=0 THEN GOTO 60
IF W=0 THEN GOTO 60
IF X=0 THEN GOTO 60
IF Y=0 THEN GOTO 60
IF Z=0 THEN GOTO 60
RETURN
REM --- Victory
REM Inputs: H - which side was the human
REM P - player who won
80 IF G=H THEN PRINT "You win!"
IF G<>0 THEN IF G<>H THEN PRINT "Computer wins"
IF G=0 THEN PRINT "A draw"
RETURN
REM --- Subroutine to allow the player to move
REM Inputs: P - player number
REM Outputs: M - where the player wishes to move
100 PRINT "Move? "
INPUT Q
IF Q<1 THEN GOTO 100
IF Q>9 THEN GOTO 100
GOSUB 220
IF N<>0 THEN GOTO 100
LET M=Q
GOSUB 240
RETURN
REM --- Subroutine to make the computer's move
REM Inputs: P - player number
REM Outputs: M - the move chosen
120 LET M=0
LET D=3-H
GOSUB 145
IF M>0 THEN GOTO 135
LET D=H
GOSUB 145
IF M=0 THEN IF V=0 THEN LET M=5
IF M=0 THEN IF R=0 THEN LET M=1
IF M=0 THEN IF T=0 THEN LET M=3
IF M=0 THEN IF X=0 THEN LET M=7
IF M=0 THEN IF Z=0 THEN LET M=9
IF M=0 THEN IF S=0 THEN LET M=2
IF M=0 THEN IF U=0 THEN LET M=4
IF M=0 THEN IF Y=0 THEN LET M=8
IF M=0 THEN IF W=0 THEN LET M=6
135 GOSUB 240
PRINT "Computer move ",M
RETURN
REM --- Identify moves to win or avoid a loss
REM Inputs: D - player whose pieces we're counting
REM Changes: E - number of pieces on line being scanned
REM F - first square in winning line
REM I - increment of winning line
REM L - line being scanned (counter)
145 LET L=1
146 GOSUB 170
IF E<2 THEN GOTO 152
IF A=0 THEN LET M=F
IF B=0 THEN LET M=F+I
IF C=0 THEN LET M=F+I+I
IF M>0 THEN RETURN
152 LET L=L+1
IF L<9 THEN GOTO 146
RETURN
REM --- Count a player's pieces on a line
REM Inputs: D - player whose pieces we're counting
REM L - line number
REM Changes: F - first square on the line
REM I - increment of the line
REM Q - individual squares to examine
REM Outputs: A - contents of first square
REM B - contents of second square
REM C - contents of third square
REM E - number of the player's pieces
170 IF L>3 THEN GOTO 174
LET F=3*L-2
LET I=1
GOTO 180
174 IF L>6 THEN GOTO 178
LET F=L-3
LET I=3
GOTO 180
178 LET F=1+2*(L-7)
LET I=4-2*(L-7)
180 LET E=0
LET Q=F
GOSUB 220
LET A=N
IF N=D THEN LET E=E+1
LET Q=Q+I
GOSUB 220
LET B=N
IF N=D THEN LET E=E+1
LET Q=Q+I
GOSUB 220
LET C=N
IF N=D THEN LET E=E+1
RETURN
REM --- Subroutine to check for a win
REM Inputs: R-Z - board squares
REM Outputs: G - the winning player (0 for neither)
200 LET G=0
IF R>0 THEN IF R=S THEN IF S=T THEN LET G=R
IF U>0 THEN IF U=V THEN IF V=W THEN LET G=U
IF X>0 THEN IF X=Y THEN IF Y=Z THEN LET G=X
IF R>0 THEN IF R=U THEN IF U=X THEN LET G=R
IF S>0 THEN IF S=V THEN IF V=Y THEN LET G=S
IF T>0 THEN IF T=W THEN IF W=Z THEN LET G=T
IF R>0 THEN IF R=V THEN IF V=Z THEN LET G=R
IF T>0 THEN IF T=V THEN IF V=X THEN LET G=T
RETURN
REM --- Subroutine to see what piece is in a square
REM Inputs: Q - the square to check
REM R-Z - the contents of the squares
REM Outputs: N - the piece in that square
220 LET N=0
IF Q=1 THEN LET N=R
IF Q=2 THEN LET N=S
IF Q=3 THEN LET N=T
IF Q=4 THEN LET N=U
IF Q=5 THEN LET N=V
IF Q=6 THEN LET N=W
IF Q=7 THEN LET N=X
IF Q=8 THEN LET N=Y
IF Q=9 THEN LET N=Z
RETURN
REM --- Subroutine to put a piece in a square
REM Inputs: P - the player whose piece should be placed
REM M - the square to put the piece in
REM Changes: R-Z - the contents of the squares
240 IF M=1 THEN LET R=P
IF M=2 THEN LET S=P
IF M=3 THEN LET T=P
IF M=4 THEN LET U=P
IF M=5 THEN LET V=P
IF M=6 THEN LET W=P
IF M=7 THEN LET X=P
IF M=8 THEN LET Y=P
IF M=9 THEN LET Z=P
RETURN

View File

@@ -0,0 +1,230 @@
REM
REM --- Tiny BASIC Interpreter and Compiler Project
REM --- Hunt the Wumpus Demonstration Game
REM
REM --- Released as Public Domain by Damian Gareth Walker 2019
REM --- Created: 08-Aug-2019
REM
REM --- Variable List
REM
REM A - Bat 1 position
REM B - Bat 2 position
REM C - Player position
REM D - Destination to move or shoot
REM E - exit 1 from the current cave
REM F - exit 2 from the current cave
REM G - exit 3 from the current cave
REM H - Hole 1 (bottomless pit) position
REM I - Hole 2 (bottomless pit) position
REM J - Randomised position for player or hazard
REM K - origin location of arrow in motion (before L)
REM L - previous location of arrow in motion
REM M - menu option for move or shoot
REM N - range for arrow shot
REM P - parameter to the 'exits' routine
REM Q - number of arrows in quiver
REM R - random number
REM S - random number generator seed
REM W - Wumpus position
REM --- Intialise the random number generator
PRINT "Think of a number"
INPUT S
REM --- Initialise the player and hazard positions
LET A=0
LET B=0
LET C=0
LET H=0
LET I=0
LET W=0
REM --- Fill the player's quiver
LET Q=5
REM --- Distribute the player and hazards across the map
GOSUB 130
LET A=J
GOSUB 130
LET B=J
GOSUB 130
LET C=J
GOSUB 130
LET H=J
GOSUB 130
LET I=J
GOSUB 130
LET W=J
REM --- Introductory text
PRINT "You enter the caves to Hunt the Wumpus!"
REM --- Main Game Loop
30 PRINT "You are in room ",C
LET P=C
GOSUB 200
GOSUB 50
PRINT "Exits are ",E,",",F,",",G
35 PRINT "1:Move or 2:Shoot?"
INPUT M
IF M<1 THEN GOTO 35
IF M>2 THEN GOTO 35
IF M=1 THEN GOSUB 120
IF M=2 THEN GOSUB 150
GOTO 30
REM --- Subroutine to check for hazards
REM Has the player encountered a bat?
50 IF C<>A THEN IF C<>B THEN GOTO 60
PRINT "A bat swoops down and picks you up..."
GOSUB 100
PRINT "...and drops you down"
LET P=C
GOSUB 200
REM Has the player fallen in a pit?
60 IF C<>H THEN IF C<>I THEN GOTO 65
PRINT "You fall down a bottomless hole into the abyss!"
END
REM Has the player startled the wumpus?
65 IF C<>W THEN GOTO 70
PRINT "You stumble upon the wumpus!"
GOSUB 90
PRINT "The wumpus runs away!"
REM Is there a pit nearby?
70 IF E<>H THEN IF F<>H THEN IF G<>H THEN GOTO 72
GOTO 73
72 IF E<>I THEN IF F<>I THEN IF G<>I THEN GOTO 75
73 PRINT "You feel a cold wind blowing from a nearby cavern."
REM Is the wumpus nearby?
75 IF E<>W THEN IF F<>W THEN IF G<>W THEN GOTO 80
PRINT "You smell something terrible nearby."
REM Is there a bat nearby?
80 IF E<>A THEN IF F<>A THEN IF G<>A THEN GOTO 82
GOTO 83
82 IF E<>B THEN IF F<>B THEN IF G<>B THEN GOTO 84
83 PRINT "You hear a loud squeaking and a flapping of wings."
84 RETURN
REM --- Relocate the Wumpus
90 GOSUB 140
LET R=R-(R/4*4)
IF R=1 THEN LET W=E
IF R=2 THEN LET W=F
IF R=3 THEN LET W=G
IF W<>C THEN RETURN
PRINT "The wumpus eats you!"
END
REM --- Relocate bat and player
100 GOSUB 140
LET R=R-(R/4*4)
IF R=0 THEN RETURN
LET P=C
GOSUB 200
IF R=1 THEN LET D=E
IF R=2 THEN LET D=F
IF R=3 THEN LET D=G
IF D<>A THEN IF D<>B THEN GOTO 110
GOTO 100
110 PRINT "...moves you to room ",D,"..."
IF A=C THEN LET A=D
IF B=C THEN LET B=D
LET C=D
GOTO 100
REM --- Subroutine to move
120 PRINT "Where?"
INPUT D
IF D<>E THEN IF D<>F THEN IF D<>G THEN GOTO 120
LET C=D
RETURN
REM -- Find a random unoccupied position
130 GOSUB 140
LET J=1+R-R/20*20
IF J<>A THEN IF J<>B THEN IF J<>C THEN GOTO 134
GOTO 130
134 IF J<>H THEN IF J<>I THEN IF J<>W THEN RETURN
GOTO 130
REM --- Random number generator
140 LET S=5*S+35
LET S=S-S/4096*4096
LET R=S
RETURN
REM --- Subroutine to shoot
150 PRINT "Shoot how far (1-5)?"
INPUT N
IF N<1 THEN GOTO 150
IF N>5 THEN GOTO 150
LET P=C
LET L=0
160 GOSUB 200
LET K=L
LET L=P
PRINT "Arrow is next to rooms ",E,",",F,",",G
164 PRINT "Shoot where?"
INPUT P
IF P<>E THEN IF P<>F THEN IF P<>G THEN GOTO 180
IF P=K THEN GOTO 185
IF P=W THEN GOTO 195
LET N=N-1
IF N>0 THEN GOTO 160
LET Q=Q-1
PRINT "The arrow startles the wumpus."
LET P=W
GOSUB 200
GOSUB 90
IF Q=0 THEN GOTO 190
PRINT "You have ",Q," arrows left."
RETURN
180 PRINT "The arrow can't reach there."
GOTO 164
185 PRINT "The arrow can't double back on itself."
GOTO 164
190 PRINT "You used your last arrow!"
PRINT "Your demise is now inevitable."
END
195 PRINT "You hit the wumpus!"
END
REM --- Subroutine to set the exits
REM Input: P - current position
REM Outputs: E, F, G (exits)
200 IF P>=1 THEN IF P<=20 THEN GOTO 205
PRINT "Illegal position ",P
END
205 GOTO 200+10*((14+P)/10)
REM --- Outer caves
210 LET E=P-1
IF E=0 THEN LET E=5
LET F=P+1
IF F=6 THEN LET F=1
LET G=4+2*P
RETURN
REM --- Middle caves
220 LET E=P-1
IF E=5 THEN LET E=15
LET F=P+1
IF F=16 THEN LET F=6
IF P/2*2<>P THEN LET G=13+P/2
IF P/2*2=P THEN LET G=P/2-2
RETURN
REM --- Inner caves
230 LET E=P-1
IF E=15 THEN LET E=20
LET F=P+1
IF F=21 THEN LET F=16
LET G=2*P-25
RETURN