Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1.     REM
  2.     REM Tiny BASIC Interpreter and Compiler Project
  3.     REM Tic-tac-toe Sample Game
  4.     REM
  5.     REM Released as public domain by Damian Gareth Walker, 2019
  6.     REM Created: 21-Sep-2019
  7.     REM
  8.  
  9.     REM --- Variables
  10.     REM     A   - first square in line examined
  11.     REM     B   - second square in line examined
  12.     REM     C   - third square in line examined
  13.     REM     D   - player whose pieces to count
  14.     REM     E   - number of D's pieces on a line
  15.     REM     F   - first square of line to examine
  16.     REM     G   - game winner
  17.     REM     H   - which side the human takes
  18.     REM     I   - increment for line to examine
  19.     REM     L   - line to examine
  20.     REM     M   - where to move (various uses)
  21.     REM     N   - piece found in a square
  22.     REM     P   - player currently playing
  23.     REM     Q   - square to examine
  24.     REM     R-Z - contents of the board
  25.  
  26.     REM --- Main Program
  27.     GOSUB 40
  28.     GOSUB 60
  29.     GOSUB 80
  30.     END
  31.  
  32.     REM --- Subroutine to initialise the game
  33.     REM     Outputs: H - Human play order
  34.     REM              P - Whose turn it is
  35.  40 PRINT "Tic tac toe. Board positions are:"
  36.     PRINT " 1  2  3"
  37.     PRINT " 4  5  6"
  38.     PRINT " 7  8  9"
  39.     PRINT "Play first or second (1/2)?"
  40.     INPUT H
  41.     IF H<1 THEN GOTO 40
  42.     IF H>2 THEN GOTO 40
  43.     LET P=1
  44.     RETURN
  45.  
  46.     REM --- Subroutine to take turns
  47.     REM     Inputs:  H - who is the human
  48.     REM              P - whose turn it is
  49.     REM     Outputs: G - who won the game
  50.  60 IF P=H THEN GOSUB 100
  51.     IF P<>H THEN GOSUB 120
  52.     GOSUB 200
  53.     IF G>0 THEN RETURN
  54.     LET P=3-P
  55.     IF R=0 THEN GOTO 60
  56.     IF S=0 THEN GOTO 60
  57.     IF T=0 THEN GOTO 60
  58.     IF U=0 THEN GOTO 60
  59.     IF V=0 THEN GOTO 60
  60.     IF W=0 THEN GOTO 60
  61.     IF X=0 THEN GOTO 60
  62.     IF Y=0 THEN GOTO 60
  63.     IF Z=0 THEN GOTO 60
  64.     RETURN
  65.  
  66.     REM --- Victory
  67.     REM Inputs: H   - which side was the human
  68.     REM         P   - player who won
  69.  80 IF G=H THEN PRINT "You win!"
  70.     IF G<>0 THEN IF G<>H THEN PRINT "Computer wins"
  71.     IF G=0 THEN PRINT "A draw"
  72.     RETURN
  73.  
  74.     REM --- Subroutine to allow the player to move
  75.     REM     Inputs:  P   - player number
  76.     REM     Outputs: M   - where the player wishes to move
  77. 100 PRINT "Move? "
  78.     INPUT Q
  79.     IF Q<1 THEN GOTO 100
  80.     IF Q>9 THEN GOTO 100
  81.     GOSUB 220
  82.     IF N<>0 THEN GOTO 100
  83.     LET M=Q
  84.     GOSUB 240
  85.     RETURN
  86.  
  87.     REM --- Subroutine to make the computer's move
  88.     REM     Inputs:  P   - player number
  89.     REM     Outputs: M   - the move chosen
  90. 120 LET M=0
  91.     LET D=3-H
  92.     GOSUB 145
  93.     IF M>0 THEN GOTO 135
  94.     LET D=H
  95.     GOSUB 145
  96.     IF M=0 THEN IF V=0 THEN LET M=5
  97.     IF M=0 THEN IF R=0 THEN LET M=1    
  98.     IF M=0 THEN IF T=0 THEN LET M=3    
  99.     IF M=0 THEN IF X=0 THEN LET M=7    
  100.     IF M=0 THEN IF Z=0 THEN LET M=9
  101.     IF M=0 THEN IF S=0 THEN LET M=2    
  102.     IF M=0 THEN IF U=0 THEN LET M=4    
  103.     IF M=0 THEN IF Y=0 THEN LET M=8    
  104.     IF M=0 THEN IF W=0 THEN LET M=6    
  105. 135 GOSUB 240
  106.     PRINT "Computer move ",M
  107.     RETURN
  108.  
  109.     REM --- Identify moves to win or avoid a loss
  110.     REM     Inputs:  D   - player whose pieces we're counting
  111.     REM     Changes: E   - number of pieces on line being scanned
  112.     REM              F   - first square in winning line
  113.     REM              I   - increment of winning line
  114.     REM              L   - line being scanned (counter)
  115. 145 LET L=1
  116. 146 GOSUB 170
  117.     IF E<2 THEN GOTO 152
  118.     IF A=0 THEN LET M=F
  119.     IF B=0 THEN LET M=F+I
  120.     IF C=0 THEN LET M=F+I+I
  121.     IF M>0 THEN RETURN
  122. 152 LET L=L+1
  123.     IF L<9 THEN GOTO 146
  124.     RETURN
  125.  
  126.     REM --- Count a player's pieces on a line
  127.     REM     Inputs:  D   - player whose pieces we're counting
  128.     REM              L   - line number
  129.     REM     Changes: F   - first square on the line
  130.     REM              I   - increment of the line
  131.     REM              Q   - individual squares to examine
  132.     REM     Outputs: A   - contents of first square
  133.     REM              B   - contents of second square
  134.     REM              C   - contents of third square
  135.     REM              E   - number of the player's pieces
  136. 170 IF L>3 THEN GOTO 174
  137.     LET F=3*L-2
  138.     LET I=1
  139.     GOTO 180
  140. 174 IF L>6 THEN GOTO 178
  141.     LET F=L-3
  142.     LET I=3
  143.     GOTO 180
  144. 178 LET F=1+2*(L-7)
  145.     LET I=4-2*(L-7)
  146. 180 LET E=0
  147.     LET Q=F
  148.     GOSUB 220
  149.     LET A=N
  150.     IF N=D THEN LET E=E+1
  151.     LET Q=Q+I
  152.     GOSUB 220
  153.     LET B=N
  154.     IF N=D THEN LET E=E+1
  155.     LET Q=Q+I
  156.     GOSUB 220
  157.     LET C=N
  158.     IF N=D THEN LET E=E+1
  159.     RETURN
  160.  
  161.     REM --- Subroutine to check for a win
  162.     REM     Inputs:  R-Z - board squares
  163.     REM     Outputs: G   - the winning player (0 for neither)
  164. 200 LET G=0
  165.     IF R>0 THEN IF R=S THEN IF S=T THEN LET G=R
  166.     IF U>0 THEN IF U=V THEN IF V=W THEN LET G=U
  167.     IF X>0 THEN IF X=Y THEN IF Y=Z THEN LET G=X
  168.     IF R>0 THEN IF R=U THEN IF U=X THEN LET G=R
  169.     IF S>0 THEN IF S=V THEN IF V=Y THEN LET G=S
  170.     IF T>0 THEN IF T=W THEN IF W=Z THEN LET G=T
  171.     IF R>0 THEN IF R=V THEN IF V=Z THEN LET G=R
  172.     IF T>0 THEN IF T=V THEN IF V=X THEN LET G=T
  173.     RETURN
  174.  
  175.     REM --- Subroutine to see what piece is in a square
  176.     REM     Inputs:  Q   - the square to check
  177.     REM              R-Z - the contents of the squares
  178.     REM     Outputs: N   - the piece in that square
  179. 220 LET N=0
  180.     IF Q=1 THEN LET N=R
  181.     IF Q=2 THEN LET N=S
  182.     IF Q=3 THEN LET N=T
  183.     IF Q=4 THEN LET N=U
  184.     IF Q=5 THEN LET N=V
  185.     IF Q=6 THEN LET N=W
  186.     IF Q=7 THEN LET N=X
  187.     IF Q=8 THEN LET N=Y
  188.     IF Q=9 THEN LET N=Z
  189.     RETURN
  190.  
  191.     REM --- Subroutine to put a piece in a square
  192.     REM     Inputs:  P   - the player whose piece should be placed
  193.     REM              M   - the square to put the piece in
  194.     REM     Changes: R-Z - the contents of the squares
  195. 240 IF M=1 THEN LET R=P
  196.     IF M=2 THEN LET S=P
  197.     IF M=3 THEN LET T=P
  198.     IF M=4 THEN LET U=P
  199.     IF M=5 THEN LET V=P
  200.     IF M=6 THEN LET W=P
  201.     IF M=7 THEN LET X=P
  202.     IF M=8 THEN LET Y=P
  203.     IF M=9 THEN LET Z=P
  204.     RETURN
  205.