The 24 Game tests one's mental arithmetic.



Task
Write a program that randomly chooses and displays four digits, each from 1 ──► 9 (inclusive) with repetitions allowed.

The program should prompt for the player to enter an arithmetic expression using just those, and all of those four digits, used exactly once each. The program should check then evaluate the expression.

The goal is for the player to enter an expression that (numerically) evaluates to 24.

 Only the following operators/functions are allowed: multiplication, division, addition, subtraction
 Division should use floating point or rational arithmetic, etc, to preserve remainders.
 Brackets are allowed, if using an infix expression evaluator.
 Forming multiple digit numbers from the supplied digits is disallowed. (So an answer of 12+12 when given 1, 2, 2, and 1 is wrong).
 The order of the digits when given does not have to be preserved.


Notes
 The type of expression evaluator used is not mandated. An RPN evaluator is equally acceptable for example.
 The task is not for the program to generate the expression, or test whether an expression is even possible.


Related tasks
 24 game/Solve


Reference
 The 24 Game on h2g2.


      REM Choose four random digits (1-9) with repetitions allowed:
      DIM digits%(4), check%(4)
      FOR choice% = 1 TO 4
        digits%(choice%) = RND(9)
      NEXT choice%
 
      REM Prompt the player:
      PRINT "Enter an equation (using all of, and only, the single digits ";
      FOR index% = 1 TO 4
        PRINT ; digits%(index%) ;
        IF index%<>4 PRINT " " ;
      NEXT
      PRINT ")"
      PRINT "which evaluates to exactly 24.  Only multiplication (*), division (/),"
      PRINT "addition (+) & subtraction (-) operations and parentheses are allowed:"
      INPUT "24 = " equation$
 
      REPEAT
 
        REM Check that the correct digits are used:
        check%() = 0
        FOR char% = 1 TO LEN(equation$)
          digit% = INSTR("0123456789", MID$(equation$, char%, 1)) - 1
          IF digit% >= 0 THEN
            FOR index% = 1 TO 4
              IF digit% = digits%(index%) THEN
                IF NOT check%(index%) check%(index%) = TRUE : EXIT FOR
              ENDIF
            NEXT index%
            IF index% > 4 THEN
              PRINT "Sorry, you used the illegal digit "; digit%
              EXIT REPEAT
            ENDIF
          ENDIF
        NEXT char%
 
        FOR index% = 1 TO 4
          IF NOT check%(index%) THEN
            PRINT "Sorry, you failed to use the digit " ; digits%(index%)
            EXIT REPEAT
          ENDIF
        NEXT index%
 
        REM Check that no pairs of digits are used:
        FOR pair% = 11 TO 99
          IF INSTR(equation$, STR$(pair%)) THEN
            PRINT "Sorry, you may not use a pair of digits "; pair%
            EXIT REPEAT
          ENDIF
        NEXT pair%
 
        REM Check whether the equation evaluates to 24:
        ON ERROR LOCAL PRINT "Sorry, there was an error in the equation" : EXIT REPEAT
        result = EVAL(equation$)
        RESTORE ERROR
        IF result = 24 THEN
          PRINT "Congratulations, you succeeded in the task!"
        ELSE
          PRINT "Sorry, your equation evaluated to " ; result " rather than 24!"
        ENDIF
 
      UNTIL TRUE
 
      INPUT '"Play again", answer$
      IF LEFT$(answer$,1) = "y" OR LEFT$(answer$,1) = "Y" THEN CLS : RUN
      QUIT