IDENTIFICATION DIVISION.
PROGRAM-ID. SnakeGame.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-BOARD.
    05 FILLER PIC X(20) VALUE '....................'.
    05 FILLER PIC X(20) VALUE '....................'.
    05 FILLER PIC X(20) VALUE '....................'.
    05 FILLER PIC X(20) VALUE '.........O..........'.
    05 FILLER PIC X(20) VALUE '....................'.
    05 FILLER PIC X(20) VALUE '....................'.
01 WS-SNAKE-POSITION.
    05 WS-X PIC 99 VALUE 10.
    05 WS-Y PIC 99 VALUE 4.
01 WS-APPLE-POSITION.
    05 APPLE-X PIC 99 VALUE 5.
    05 APPLE-Y PIC 99 VALUE 3.
01 WS-DIRECTION PIC X VALUE 'R'.
01 WS-SCORE PIC 99 VALUE 0.

PROCEDURE DIVISION.
MAIN-PROCEDURE.
    PERFORM UNTIL WS-DIRECTION = 'Q'
        DISPLAY BOARD
        MOVE WS-DIRECTION TO WS-DIRECTION
        PERFORM MOVE-SNAKE
        PERFORM CHECK-APPLE
        DISPLAY 'SCORE: ' WS-SCORE
    END-PERFORM.
    STOP RUN.

MOVE-SNAKE.
    IF WS-DIRECTION = 'U' THEN
        SUBTRACT 1 FROM WS-Y
    ELSE IF WS-DIRECTION = 'D' THEN
        ADD 1 TO WS-Y
    ELSE IF WS-DIRECTION = 'L' THEN
        SUBTRACT 1 FROM WS-X
    ELSE IF WS-DIRECTION = 'R' THEN
        ADD 1 TO WS-X
    END-IF.

CHECK-APPLE.
    IF WS-X = APPLE-X AND WS-Y = APPLE-Y THEN
        ADD 1 TO WS-SCORE
        MOVE FUNCTION RANDOM(1 20) TO APPLE-X
        MOVE FUNCTION RANDOM(1 20) TO APPLE-Y
    END-IF.

DISPLAY BOARD.
    MOVE SPACES TO WS-BOARD(WS-Y, WS-X).
    MOVE 'O' TO WS-BOARD(WS-Y, WS-X).
    MOVE 'A' TO WS-BOARD(APPLE-Y, APPLE-X).
    DISPLAY WS-BOARD.

