life.bac

' Conway's game of LIFE on ANSI compliant ASCII screen.
'
' https://en.wikipedia.org/wiki/Game_of_Life
'
' (c) PvE, July 2015 - GPL
'
' To generate HTML page:
' enscript --color --tabsize=4 --style=emacs --language=html --highlight=bacon --title=life.bac life.bac -p life.bac.html
'------------------------------------------------------------------------------------------------------------------------

DECLARE cache, board ARRAY COLUMNS, ROWS
DECLARE status

' Initialize screen
CLEAR
GOTOXY 1, ROWS
PRINT "<arrows>: move, <space>: draw/generate, <return>: start/stop generation mode, <esc>: end";

' Start position
GOTOXY COLUMNS/2, ROWS/2

' Main game loop
WHILE TRUE
    ' Wait for a key
    key = WAIT(0, 50)

    ' Escape sequence?
    IF key = 27 THEN
        curs = WAIT(STDIN_FILENO, 10)

        ' Special key was pressed
        IF curs = 91 THEN
            curs = WAIT(STDIN_FILENO, 10)

            ' One of the cursor keys
            SELECT curs
                CASE 66
                    ' Cursor down
                    IF GETY < ROWS THEN GOTOXY GETX, GETY+1
                CASE 65
                    ' Cursor up
                    IF GETY > 1 THEN GOTOXY GETX, GETY-1
                CASE 67
                    ' Cursor right
                    IF GETX < COLUMNS THEN GOTOXY GETX+1, GETY
                CASE 68
                    ' Cursor left
                    IF GETX > 0 THEN GOTOXY GETX-1, GETY
            END SELECT
        ELSE
            ' Normal ESC was pressed
            BREAK
        END IF
    ELIF key = 10 THEN
        ' Return key changes draw mode to generate mode and vv
        status = 1 - status
        IF status THEN
            CURSOR OFF
        ELSE
            CURSOR ON
            GOTOXY COLUMNS/2, ROWS/2
        ENDIF
    ELIF key = 32 THEN
        ' <SPACE> bar draws
        IF status = 0 THEN
            IF board[GETX][GETY] = 0 THEN
                board[GETX][GETY] = 1
                PRINT "O";
            ELSE
                board[GETX][GETY] = 0
                PRINT " ";
            ENDIF
        ELSE
            ' <SPACE> bar generates
            FOR y = 1 TO ROWS-2
                FOR x = 1 TO COLUMNS-2
                    SELECT board[x-1][y-1]+board[x][y-1]+board[x+1][y-1]+board[x-1][y]+board[x+1][y]+board[x-1][y+1]+board[x][y+1]+board[x+1][y+1]
                        CASE 2
                            IF board[x][y] = 1 THEN cache[x][y] = 1
                        CASE 3
                            cache[x][y] = 1
                        DEFAULT
                            cache[x][y] = 0
                    END SELECT
                NEXT
            NEXT
            ' Print on screen
            FOR y = 0 TO ROWS-1
                FOR x = 0 TO COLUMNS-1
                    IF cache[x][y] != board[x][y] THEN
                        GOTOXY x, y
                        IF cache[x][y] = 0 THEN
                            PRINT " ";
                        ELSE
                            PRINT "O";
                        ENDIF
                        board[x][y] = cache[x][y]
                    ENDIF
                NEXT
            NEXT
        END IF
    ENDIF
WEND

' End game message
GOTOXY 1, ROWS
CURSOR ON
PRINT NL$, "End of Conway's LIFE game."

Generated by GNU Enscript 1.6.5.90.