10 REM GOBANG
20 REM M IS ARRAY HOLDING BEST MOVE
30 REM T IS BOARD, S IS PRIORITY OF THAT POSITION
35 DEF FNC$(Y,X)=CHR$(27)+CHR$(61)+CHR$(32+Y)+CHR$(32+X)
40 PRINT CHR$(26);TAB(30);"G O B A N G";
50 PRINT FNC$(9,9);"DO YOU WANT INSTRUCTIONS? (Y/N) ";:INPUT I$
60 IF I$="Y" THEN GOSUB 1820
70 GOTO 90
80 ERASE M,T,S
90 DIM M(19,19),T(27,27),S(81)
100 REM SET UP PRIORITIES-SEE TABLE 1
110 FOR I=1 TO 81
120 S(I)=0
130 NEXT I
140 S(20)=1
150 S(10)=40
160 S(12)=30
170 S(13)=47
180 S(27)=15
190 S(28)=20
200 S(29)=10
210 S(30)=40
220 S(31)=50
230 S(32)=30
240 S(24)=1
250 S(36)=39
260 S(37)=65
270 S(38)=40
280 S(39)=70
290 S(40)=100
300 S(41)=60
310 S(42)=30
320 S(43)=30
330 S(44)=30
340 S(62)=41
350 S(72)=31
360 S(73)=11
370 S(74)=41
380 S(78)=51
390 S(80)=90
400 S(26)=21
410 S(79)=40
420 S(60)=21
430 S(61)=11
440 REM CLEAR BOARD AND BEST MOVE ARRAYS
450 FOR I=1 TO 27
460  FOR J=1 TO 27
470   IF I<19 AND J<19 THEN M(I,J)=0
480   REM MAKE FIRST MOVE
490  NEXT J
500 NEXT I
510 INPUT I$
520 C=-1
530 W=14
540 N=14
550 O=14
560 X=14
570 GOTO 920
580 GOSUB 1010
590 REM REQUEST MOVE AND CHECK FOR VALIDITY
600 INPUT Z,Y
610 Y=Y+4
620 Z=Z+4
630 IF Y>23 THEN GOTO 600
640 IF Z>23 THEN GOTO 600
650 IF Y<5 THEN GOTO 600
660 IF Z<5 THEN GOTO 600
670 IF T(Y,Z)>0 THEN GOTO 600
680 T(Y,Z)=2
690 I=Y
700 J=Z
710 REM STUDY LAST TWO MOVES
720 GOSUB 1170
730 IF C<>-1 THEN GOTO 950
740 REM IF C=0 COMPUTER HAS LOST
750 I=W
760 J=X
770 GOSUB 1170
780 REM SCAN BOARD FOR BEST MOVE
790 REM NOTE LIMITS TO SPEED UP PROGRAM
800 Q=-1
810 FOR I=N-1 TO O+1
820  FOR J=5 TO 23
830   IF T(I,J)>0 THEN GOTO 890
840   A=M(I-4,J-4)
850   IF A<Q THEN GOTO 890
860   W=I
870   X=J
880   Q=A
890  NEXT J
900 NEXT I
910 PRINT "MY MOVE";X-4;",";W-4
920 T(W,X)=1
930 IF M(W-4,X-4)<100 THEN GOTO 580
940 PRINT "I WIN!!"
950 IF C=0 THEN PRINT "YOU WIN!!"
960 GOSUB 1010
970 PRINT "DO YOU WANT TO PLAY AGAIN? (Y/N)";: INPUT A$
980 IF A$<>"N" THEN RUN
990 GOTO 80
1000 REM SUBROUTINE TO DISPLAY BOARD
1010 PRINT "      1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19"
1020 FOR I=5 TO 23
1030  IF I-4<10 THEN PRINT I-4;" ";
1040  IF I-4>9 THEN PRINT I-4;
1050  FOR J=5 TO 23
1060   IF T(I,J)=0 THEN PRINT "  :";
1070   IF T(I,J)=1 THEN PRINT "  X";
1080   IF T(I,J)=2 THEN PRINT "  O";
1090  NEXT J
1100  PRINT " "
1110 NEXT I
1120 RETURN
1130 REM SUBROUTINE TO CALCULATE BEST MOVE
1140 REM SCAN THRU MOVE AT I,J
1150 REM FOR FIVE SQUARES EITHER SIDE OF MOVE
1160 REM IN EIGHT DIRECTIONS, AND UPDATE BEST MOVE ARRAY.
1170 K=1
1180 L=-1
1190 IF I<N AND I>5 THEN N=I
1200 IF I>O AND I<23 THEN O=I
1210 REM UPDATE SCAN LIMITS
1220 U=I
1230 V=J
1240 REM I,J IS MOVE TO CHECK, D IS LOOP COUNT
1250 REM K,L ARE X AND Y DIRECTIONS THRU MOVE
1260 D=0
1270 D=D+1
1280 P=81
1290 REM CHECK STILL ON BOARD
1300 IF U>23 THEN GOTO 1730
1310 IF V>23 THEN GOTO 1730
1320 IF U<5 THEN GOTO 1730
1330 IF V<5 THEN GOTO 1730
1340 E=U-4
1350 G=V-4
1360 A=M(E,G)
1370 Q=T(U+K,V+L)
1380 REM CALCULATE PRIORITY OF POSITION
1390 R=T(U-K,V-L)*27+T(U-2*K,V-2*L)*9
1400 R=R+T(U-3*K,V-3*L)*3+T(U-4*K,V-4*L)
1410 B=Q*27+T(U+2*K,V+2*L)*9+T(U+3*K,V+3*L)*3
1420 IF R=80 AND T(U,V)=2 THEN C=0
1430 IF T(U,V)<>0 THEN GOTO 1680
1440 REM S(R) IS PRIORITY; THE FOLLOWING ARE EXCEPTIONS
1450 REM SEE TABLE 2
1460 IF R<14 AND R>11 AND Q=1 THEN P=37
1470 IF R>71 AND B>53 AND B<63 THEN P=80
1480 IF R>71 AND B>71 THEN P=80
1490 IF R>53 AND R<63 AND Q=2 THEN P=72
1500 IF P=72 AND R=60 THEN P=31
1510 IF Q<>2 THEN GOTO 1550
1520 IF R=78 THEN P=80
1530 IF R=79 THEN P=80
1540 IF R=41 THEN R=81
1550 IF R<42 AND R>35 AND Q=1 THEN P=41
1560 IF R<33 AND R>29 AND Q=1 THEN P=41
1570 IF R>53 AND R<62 AND B>71 THEN P=80
1580 IF R>38 AND R<42 AND Q=1 THEN R=40
1590 IF R>35 AND R<45 AND B>35 AND B<45 THEN R=40
1600 IF R>27 AND R<54 AND B>38 AND B<42 THEN R=40
1610 IF R=79 AND A=51 THEN M(E,G)=41
1620 IF R=0 THEN R=81
1630 IF S(P)>S(R) THEN R=P
1640 IF S(R)-INT(S(R)/10)*10=1 AND A-INT(A/10)*10=1 AND S(R)<41 THEN R=74
1650 IF S(R)-INT(S(R)/10)*10=9 AND A-INT(A/10)*10=9 AND S(R)<65 THEN R=37
1660 REM UPDATE BEST MOVE ARRAY
1670 IF S(R)>M(E,G) THEN M(E,G)=S(R)
1680 IF D>4 THEN GOTO 1730
1690 U=U+K
1700 V=V+L
1710 GOTO 1270
1720 REM CHANGE DIRECTION
1730 IF K=0 AND L=-1 THEN RETURN
1740 IF K=-1 AND L=-1 THEN K=0
1750 IF K=-1 AND L=0 THEN L=-1
1760 IF K=-1 AND L=1 THEN L=0
1770 IF K=0 AND L=1 THEN K=-1
1780 IF K=1 AND L=1 THEN K=0
1790 IF K=1 AND L=0 THEN L=1
1800 IF K=1 AND L=-1 THEN L=0
1810 GOTO 1220
1820 PRINT CHR$(26):PRINT "GOBANG is, as far as I can tell, a traditional game"
1830 PRINT "of the Orient.  It is a large game of tic-tac-toe (noughts and"
1840 PRINT "crosses), played on a 19 by 19 inch board.  The object of the"
1850 PRINT "game is to get 5 adjacent markers in a row horizontally,"
1860 PRINT "vertically, or diagonally.":PRINT
1870 PRINT "The computer always goes first, and is 'X'.  It takes about 40"
1880 PRINT "seconds for the computer to think of its best move, so do not"
1890 PRINT "worry if there is not an immediate response."
1900 PRINT:PRINT "To make a move, enter the horizontal number first, then a"
1910 PRINT "comma, and the vertical number of the location you want to fill"
1920 PRINT :PRINT "Good luck!!  Press <RETURN> to start game."
1930 RETURN
ertical number of the location you want to fill"
1920 PRINT :PRINT "Good luck!!  Pre