' Typing Speed Test — measures words-per-minute typing accuracy
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL target AS STRING
    LOCAL prompt AS STRING
    LOCAL inputText AS STRING
    LOCAL i AS LONG
    LOCAL correct AS LONG
    LOCAL totalChars AS LONG
    LOCAL elapsed AS DOUBLE
    LOCAL wpm AS LONG
    LOCAL accuracy AS LONG
    LOCAL ch AS STRING
    LOCAL t1 AS DOUBLE
    LOCAL t2 AS DOUBLE
    LOCAL p AS LONG
    LOCAL c AS LONG
    LOCAL row AS LONG
    LOCAL line AS STRING
    LOCAL spaces AS LONG
    LOCAL pad AS STRING
    LOCAL score AS STRING
    target = "The quick brown fox jumps over the lazy dog"
    target = target + "Pack my box with five dozen liquor jugs"
    target = target + "How vexingly quick daft zebras jump"
    target = target + "The five boxing wizards jump quickly"
    target = target + "Sphinx of black quartz judge my vow"
    prompt = "Type the following text as fast and accurately as you can"
    prompt = prompt + "Press ENTER when done"
    PRINT "============================================"
    PRINT "           TYPING SPEED TEST"
    PRINT "============================================"
    PRINT prompt
    PRINT ""
    PRINT target
    PRINT ""
    PRINT "Press ENTER to start"
    LINE INPUT inputText
    t1 = TIMER
    LINE INPUT inputText
    t2 = TIMER
    totalChars = LEN(target)
    correct = 0
    IF LEN(inputText) < totalChars THEN
        totalChars = LEN(inputText)
    END IF
    FOR i = 1 TO totalChars
        c = ASC(MID$(target, i, 1))
        p = ASC(MID$(inputText, i, 1))
        IF c = p THEN
            correct = correct + 1
        END IF
    NEXT i
    elapsed = t2 - t1
    IF elapsed < 0.001 THEN
        elapsed = 0.001
    END IF
    wpm = (correct / 5) / (elapsed / 60)
    accuracy = (correct * 100) / LEN(target)
    PRINT ""
    PRINT "============================================"
    PRINT "              RESULTS"
    PRINT "============================================"
    PRINT "Time: " + STR$(elapsed) + " seconds"
    PRINT "Correct chars: " + STR$(correct) + " of " + STR$(LEN(target))
    PRINT "Speed: " + STR$(wpm) + " WPM"
    PRINT "Accuracy: " + STR$(accuracy) + "%"
    PRINT ""
    IF accuracy > 95 THEN
        PRINT "Excellent typing skills"
    ELSE
        IF accuracy > 85 THEN
            PRINT "Good typing skills"
        ELSE
            IF accuracy > 75 THEN
                PRINT "Average typing skills"
            ELSE
                PRINT "Keep practicing"
            END IF
        END IF
    END IF
    PRINT ""
    row = 0
    FOR i = 1 TO LEN(target)
        c = ASC(MID$(target, i, 1))
        p = 0
        IF i <= LEN(inputText) THEN
            p = ASC(MID$(inputText, i, 1))
        END IF
        IF row = 0 THEN
            line = ""
        END IF
        IF c = p THEN
            IF c = 32 THEN
                ch = " "
            ELSE
                ch = CHR$(c)
            END IF
        ELSE
            IF p = 0 THEN
                ch = "_"
            ELSE
                ch = "X"
            END IF
        END IF
        line = line + ch
        IF LEN(line) >= 60 THEN
            PRINT line
            row = 0
        ELSE
            row = 1
        END IF
    NEXT i
    IF LEN(line) > 0 THEN
        spaces = 60 - LEN(line)
        pad = ""
        FOR i = 1 TO spaces
            pad = pad + " "
        NEXT i
        PRINT line + pad
    END IF
    PRINT ""
    score = "WPM: " + STR$(wpm) + "  Accuracy: " + STR$(accuracy) + "%"
    PRINT score
    FUNCTION = 0
END FUNCTION
