      $SET ANS85
       IDENTIFICATION DIVISION.
           PROGRAMID. TICTAC.
       ENVIRONMENT DIVISION.
           CONFIGURATION SECTION.
           SOURCECOMPUTER. IBMPC.
           OBJECTCOMPUTER. IBMPC.
       SPECIALNAMES.
           CONSOLE IS CRT.
       DATA DIVISION.
       WORKINGSTORAGE SECTION.
       01 TICTAC00.
        02 TICTACQ.
           03 GAME             PIC X(10) VALUE SPACES.
           03 FILLER0         PIC X(70) VALUE SPACES.
           03 QUESTION         PIC X(20) VALUE SPACES.
        02 FILLER.
           03 FILLER1         PIC X(414) VALUE ALL SPACES.
           03 TICTAC000735   PIC X(17) VALUE 7|      8|      9.
           03 FILLER2         PIC X(64) VALUE ALL SPACES.
           03 TICTAC000836   PIC X(09) VALUE |       |.
           03 FILLER3         PIC X(71) VALUE ALL SPACES.
           03 TICTAC000936   PIC X(09) VALUE |       |.
           03 FILLER4         PIC X(64) VALUE ALL SPACES.
           03 TICTAC001029 PIC X(23) VALUE ++.
           03 FILLER5         PIC X(63) VALUE ALL SPACES.
           03 TICTAC001135   PIC X(17) VALUE 4|      5|      6.
           03 FILLER6         PIC X(64) VALUE ALL SPACES.
           03 TICTAC001236   PIC X(09) VALUE |       |.
           03 FILLER7         PIC X(71) VALUE ALL SPACES.
           03 TICTAC001336   PIC X(09) VALUE |       |.
           03 FILLER8         PIC X(64) VALUE ALL SPACES.
           03 TICTAC001429 PIC X(23) VALUE ++.
           03 FILLER9         PIC X(63) VALUE ALL SPACES.
           03 TICTAC001535   PIC X(17) VALUE 1|      2|      3.
           03 FILLER10        PIC X(64) VALUE ALL SPACES.
           03 TICTAC001636   PIC X(09) VALUE |       |.
           03 FILLER11        PIC X(71) VALUE ALL SPACES.
           03 TICTAC001736   PIC X(09) VALUE |       |.
           03 FILLER12        PIC X(595) VALUE ALL SPACES.
       01 ENTRYARRAY.
           03 ENTRYCHAR       PIC X               OCCURS 9 TIMES.
       01 CHECKARRAY.
           03 CHECK            PIC S99     COMP  OCCURS 9 TIMES.
       01 XCOUNT               PIC 9(2)    COMP.
       01 OCOUNT               PIC 9(2)    COMP.
       01 FACTOR               PIC S9(2)   COMP.
       01 CHAR                 PIC X.
       01 CHAR9 REDEFINES CHAR PIC 9.
       01 IDX                  PIC 9(2)    COMP.
       01 RESULT               PIC 9(2)    COMP.
       01 CURSORPOS.
           03 ROW              PIC 9(2)    COMP  VALUE 99.
           03 LIN              PIC 9(2)    COMP  VALUE 99.
       01 ADDRESSINIT.
           03 FILLER           PIC 9(4)    VALUE   1732.
           03 FILLER           PIC 9(4)    VALUE   1740.
           03 FILLER           PIC 9(4)    VALUE   1748.
           03 FILLER           PIC 9(4)    VALUE   1332.
           03 FILLER           PIC 9(4)    VALUE   1340.
           03 FILLER           PIC 9(4)    VALUE   1348.
           03 FILLER           PIC 9(4)    VALUE   0932.
           03 FILLER           PIC 9(4)    VALUE   0940.
           03 FILLER           PIC 9(4)    VALUE   0948.
       01 ADDRESSARRAY        REDEFINES   ADDRESSINIT.
           03 ADDR             PIC 9(4)    OCCURS 9 TIMES.
       01 LOCATION             PIC 9(4).
       01 GAMELINES VALUE     147123311113332436978979.
           03 A                PIC 9       OCCURS 8 TIMES.
           03 B                PIC 9       OCCURS 8 TIMES.
           03 C                PIC 9       OCCURS 8 TIMES.
       01 I                    PIC 9(2)    COMP.
       01 J                    PIC 9(2)    COMP.
       01 MOVES                PIC 9(2)    COMP.

       78 CLEARSCREEN        VALUE XE4.
       78 SOUNDBELL          VALUE XE5.

       PROCEDURE DIVISION.
       PLAYGAME SECTION.
       PLAY1.
           CALL CLEARSCREEN.
           DISPLAY To select a square type a number between 1 and 9
                      upon crt.
           PERFORM WITH TEST AFTER
               UNTIL CHAR NOT = 'y' AND CHAR NOT = 'Y'
               PERFORM INIT
               MOVE 'Shall I start ? ' TO QUESTION
               PERFORM GETREPLY
               IF CHAR = 'y' OR CHAR = 'Y'
                   MOVE 10 TO CHECK(5)
                   PERFORM PUTMOVE
               ENDIF
               PERFORM NEWMOVE UNTIL GAME NOT = SPACES
               MOVE 'Play again ?    ' TO QUESTION
               PERFORM GETREPLY
           ENDPERFORM.

       PLAYSTOP.
           STOP RUN.

       GETREPLY SECTION.
           DISPLAY TICTACQ AT 0201.
           ACCEPT CHAR AT 0317 WITH NOECHO AUTOSKIP.
           MOVE SPACES TO QUESTION.
           DISPLAY TICTAC00 AT 0201.

       INIT SECTION.
           MOVE 'Y' TO CHAR.
           MOVE SPACES TO ENTRYARRAY
           MOVE LOWVALUES TO CHECKARRAY
           MOVE SPACES TO GAME
           MOVE ZERO TO MOVES.

       NEWMOVE SECTION.
           PERFORM GETMOVE WITH TEST AFTER UNTIL CHAR9 NOT = 0.
           PERFORM MOVECHECK.
           IF GAME NOT = 'Stalemate'
               MOVE LOWVALUES TO CHECKARRAY
               PERFORM CHECKLINE VARYING I FROM 1 BY 1
                               UNTIL I > 8 OR GAME NOT = SPACES
               IF GAME NOT = 'You win'
                   PERFORM PUTMOVE
               ENDIF
               IF GAME = 'I win' OR GAME = 'You win'
                     PERFORM VARYING IDX FROM A(J) BY B(J)
                                                UNTIL IDX > C(J)
                         MOVE ADDR(IDX) TO LOCATION
                         MOVE ENTRYCHAR(IDX) TO CHAR
                         DISPLAY CHAR AT LOCATION WITH BLINK HIGHLIGHT
                     ENDPERFORM
               ENDIF
           ENDIF.

       CHECKLINE SECTION.
           MOVE ZERO TO XCOUNT,OCOUNT,FACTOR
           PERFORM COUNTUP VARYING IDX FROM A(I) BY B(I)
                                            UNTIL IDX > C(I)
           IF OCOUNT = 0 OR XCOUNT = 0
               EVALUATE TRUE
                   WHEN OCOUNT = 2
                         IF I = 4
                             MOVE 6 TO J
                             MOVE ZERO TO XCOUNT,OCOUNT
                             PERFORM COUNTUP VARYING IDX
                                                FROM A(J) BY B(J)
                                                UNTIL IDX > C(J)
                             IF XCOUNT = 3 MOVE 6 TO I ENDIF
                         ENDIF
                         IF XCOUNT NOT = 3
                             MOVE 50 TO FACTOR
                             MOVE 'I win' TO GAME
                             MOVE I TO J
                         ENDIF
                   WHEN XCOUNT = 2
                         MOVE 20 TO FACTOR
                   WHEN OCOUNT = 1
                         MOVE  4 TO FACTOR
                   WHEN XCOUNT = 1
                         IF ENTRYCHAR(5) = 'X'
                             MOVE  1 TO FACTOR
                         ELSE
                             MOVE 1 TO FACTOR
                         ENDIF
                   WHEN OCOUNT = 0
                         IF XCOUNT = 0
                             MOVE  2 TO FACTOR
                         ENDIF
               ENDEVALUATE.
           IF XCOUNT = 3
               MOVE 'You win' TO GAME
               MOVE I TO J
           ELSE
               PERFORM VARYING IDX FROM A(I) BY B(I) UNTIL IDX > C(I)
                   IF ENTRYCHAR(IDX) = SPACE
                       ADD FACTOR TO CHECK(IDX)
                   ENDIF
               ENDPERFORM
           ENDIF.

       COUNTUP SECTION.
           IF ENTRYCHAR(IDX) = 'X'        ADD 1 TO XCOUNT
           ELSE IF ENTRYCHAR(IDX) = 'O'   ADD 1 TO OCOUNT.

       PUTMOVE SECTION.
           MOVE ZERO TO IDX.
           MOVE 99 TO FACTOR.
           PERFORM FINDPOS VARYING I FROM 1 BY 1 UNTIL I > 9.
           MOVE 'O' TO ENTRYCHAR(IDX).
           PERFORM MOVECHECK.

       MOVECHECK SECTION.
           MOVE ADDR(IDX) TO LOCATION.
           MOVE ENTRYCHAR(IDX) TO CHAR.
           DISPLAY CHAR AT LOCATION.
           ADD 1 TO MOVES.
           IF MOVES > 8 AND GAME = SPACES
               MOVE 'Stalemate' TO GAME.

       FINDPOS SECTION.
           IF ENTRYCHAR(5) = SPACE
               MOVE CHECK(5) TO FACTOR
               MOVE 5 TO IDX
           ELSE IF CHECK(I) NOT < FACTOR AND ENTRYCHAR(I) = SPACE
                   MOVE CHECK(I) TO FACTOR
                   MOVE I TO IDX.

       GETMOVE SECTION.
           DISPLAY Please select an empty square at 0201.
           ACCEPT CHAR9 AT 0231 WITH AUTOSKIP.
           IF CHAR9 < 1 OR CHAR9  > 9
               CALL SOUNDBELL
           ELSE
               MOVE CHAR TO IDX
               IF ENTRYCHAR(IDX) = SPACE
                   MOVE 'X' TO ENTRYCHAR(IDX)
               ELSE
                   MOVE 0 TO CHAR
                   CALL SOUNDBELL.

u	X9+>X9
tX35g>X9
uX9+>X0t>t>X0thdH