' Tetris — falling blocks, line clearing, scoring simulation
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL board(199) AS LONG
    LOCAL x AS LONG, y AS LONG
    LOCAL full AS LONG, lines AS LONG, score AS LONG

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

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

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

    board(170) = 1: board(171) = 1: board(172) = 1
    board(165) = 1: board(155) = 1
    board(164) = 1: board(154) = 1
    board(163) = 1

    PRINT "INITIAL BOARD:"
    PRINT "+----------+"
    y = 0
    WHILE y <= 19
        PRINT "|"
        x = 0
        WHILE x <= 9
            IF board(y * 10 + x) = 1 THEN PRINT "#"; ELSE PRINT ".";
            x = x + 1
        WEND
        PRINT "|"
        y = y + 1
    WEND
    PRINT "+----------+"
    PRINT ""

    lines = 0
    y = 0
    WHILE y <= 19
        full = 1
        x = 0
        WHILE x <= 9
            IF board(y * 10 + x) = 0 THEN full = 0
            x = x + 1
        WEND
        IF full = 1 THEN
            lines = lines + 1
            x = 0
            WHILE x <= 9
                board(y * 10 + x) = 0
                x = x + 1
            WEND
        END IF
        y = y + 1
    WEND

    score = lines * 100
    PRINT "Lines cleared: "; lines
    PRINT "Score: "; score
    PRINT ""

    PRINT "FINAL BOARD:"
    PRINT "+----------+"
    y = 0
    WHILE y <= 19
        PRINT "|"
        x = 0
        WHILE x <= 9
            IF board(y * 10 + x) = 1 THEN PRINT "#"; ELSE PRINT ".";
            x = x + 1
        WEND
        PRINT "|"
        y = y + 1
    WEND
    PRINT "+----------+"

    FUNCTION = 0
END FUNCTION
