' Conway's Game of Life — 1D array implementation
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL grid(99) AS LONG, ng(99) AS LONG
    LOCAL x AS LONG, y AS LONG, gen AS LONG
    LOCAL n AS LONG

    y = 0
    WHILE y <= 9
        x = 0
        WHILE x <= 9
            grid(y * 10 + x) = 0
            x = x + 1
        WEND
        y = y + 1
    WEND

    grid(12) = 1: grid(23) = 1
    grid(31) = 1: grid(32) = 1: grid(33) = 1
    grid(55) = 1: grid(56) = 1: grid(57) = 1
    grid(77) = 1: grid(78) = 1: grid(87) = 1: grid(88) = 1

    PRINT "Generation 0:"
    y = 0
    WHILE y <= 9
        x = 0
        WHILE x <= 9
            IF grid(y * 10 + x) = 1 THEN PRINT "#"; ELSE PRINT ".";
            x = x + 1
        WEND
        PRINT ""
        y = y + 1
    WEND

    gen = 1
    WHILE gen <= 3
        y = 0
        WHILE y <= 9
            x = 0
            WHILE x <= 9
                n = 0
                IF x > 0 AND y > 0 THEN IF grid((y-1)*10+(x-1)) = 1 THEN n = n + 1
                IF y > 0 THEN IF grid((y-1)*10+x) = 1 THEN n = n + 1
                IF x < 9 AND y > 0 THEN IF grid((y-1)*10+(x+1)) = 1 THEN n = n + 1
                IF x > 0 THEN IF grid(y*10+(x-1)) = 1 THEN n = n + 1
                IF x < 9 THEN IF grid(y*10+(x+1)) = 1 THEN n = n + 1
                IF x > 0 AND y < 9 THEN IF grid((y+1)*10+(x-1)) = 1 THEN n = n + 1
                IF y < 9 THEN IF grid((y+1)*10+x) = 1 THEN n = n + 1
                IF x < 9 AND y < 9 THEN IF grid((y+1)*10+(x+1)) = 1 THEN n = n + 1
                IF grid(y*10+x) = 1 THEN
                    IF n = 2 THEN ng(y*10+x) = 1
                    IF n = 3 THEN ng(y*10+x) = 1
                    IF n < 2 THEN ng(y*10+x) = 0
                    IF n > 3 THEN ng(y*10+x) = 0
                END IF
                IF grid(y*10+x) = 0 THEN
                    IF n = 3 THEN ng(y*10+x) = 1 ELSE ng(y*10+x) = 0
                END IF
                x = x + 1
            WEND
            y = y + 1
        WEND
        y = 0
        WHILE y <= 9
            x = 0
            WHILE x <= 9
                grid(y*10+x) = ng(y*10+x)
                x = x + 1
            WEND
            y = y + 1
        WEND
        PRINT "Generation "; gen; ":"
        y = 0
        WHILE y <= 9
            x = 0
            WHILE x <= 9
                IF grid(y*10+x) = 1 THEN PRINT "#"; ELSE PRINT ".";
                x = x + 1
            WEND
            PRINT ""
            y = y + 1
        WEND
        gen = gen + 1
    WEND

    PRINT "Game of Life simulation complete."

    FUNCTION = 0
END FUNCTION
