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.


 0 BH =  PEEK (104):BL =  PEEK (103)
 1  GOSUB 1200: CALL  - 868
 10  LET N$ = ""
 20 R =  RND ( - ( PEEK (78) +  PEEK (79) * 256)): REM RANDOMIZE 
 30  FOR I = 1 TO 4
 40      LET N$ = N$ +  STR$ ( INT ( RND (1) * 9) + 1)
 50  NEXT I
 60  PRINT " PRESS A KEY TO CONTINUE. ";: GET A$
 65  LET I$ = "": LET F$ = "": LET P$ = ""
 70  HOME 
 80  PRINT M$M$ SPC( 16)"24 GAME"
 90  PRINT M$"ALLOWED CHARACTERS:"M$
 100  LET I$ = N$ + "+-*/()"
 110  HTAB 20
 120  FOR I = 1 TO 10
 130      PRINT  MID$ (I$,I,1)" ";
 140  NEXT I
 150  PRINT M$ TAB( 7)"0 TO END"M$
 160  INPUT "ENTER THE FORMULA: ";F$
 170  IF F$ = "0" THEN  END : GOTO 65
 180  PRINT M$ TAB( 7)F$" = ";
 190  FOR I = 1 TO  LEN (F$)
 200      LET C$ =  MID$ (F$,I,1)
 210      IF C$ = " " THEN  LET F$ =  MID$ (F$,1,I - 1) +  MID$ (F$,I + 1): GOTO 250
 220      IF C$ = "+" OR C$ = "-" OR C$ = "*" OR C$ = "/" THEN  LET P$ = P$ + "O": GOTO 250
 230      IF C$ = "(" OR C$ = ")" THEN  LET P$ = P$ + C$: GOTO 250
 240      LET P$ = P$ + "N"
 250  NEXT I
 260  RESTORE 
 270  FOR I = 1 TO 11
 280      READ T$
 290      IF T$ = P$ THEN  LET I = 11
 300  NEXT I
 310  IF T$ <  > P$ THEN  INVERSE : PRINT "BAD CONSTRUCTION!"G$M$: NORMAL : GOTO 60
 320  FOR I = 1 TO  LEN (F$)
 330      FOR J = 1 TO 10
 340      IF ( MID$ (F$,I,1) =  MID$ (I$,J,1)) AND  MID$ (F$,I,1) > "0" AND  MID$ (F$,I,1) <  = "9" THEN  LET I$ =  MID$ (I$,1,J - 1) + " " +  MID$ (I$,J + 1, LEN (I$))
 350  NEXT J,I
 370  IF  MID$ (I$,1,4) <  > "    " THEN  INVERSE : PRINT "INVALID ARGUMENTS!"G$M$: NORMAL : GOTO 60
 380  GOSUB 600: REM LET R = VAL(F$)
 390  PRINT R" ";
 400  IF R <  > 24 THEN  INVERSE : PRINT "WRONG!"G$M$: NORMAL : GOTO 60
 410  INVERSE : PRINT "CORRECT!"M$: NORMAL : GOTO 10
 420  DATA"NONONON"
 430  DATA"(NON)ONON"
 440  DATA"NONO(NON)"
 450  DATA"NO(NO(NON))"
 460  DATA"((NON)ON)ON"
 470  DATA"NO(NON)ON"
 480  DATA"(NON)O(NON)"
 485  DATA"NO((NON)ON)"
 490  DATA"(NONON)ON"
 495  DATA"(NO(NON))ON"
 500  DATA"NO(NONON)"
 600  REMGET BASIC TO EVALUATE OUR EXPRESSION
 605 A$ = "R=" + F$: GOSUB 1440
 610  FOR I = 1 TO  LEN (A$)
 615      REMSIMPLE TOKEN TRANSLATION
 620     B =  ASC ( MID$ (A$,I,1))
 625      IF (B > 41 AND B < 48) OR B = 61 OR B = 94 THEN B = T(B)
 630      POKE (AD + I - 1),B
 635  NEXT 
 640  GOSUB 2000
 645  REM  GOSUB 1440:REM UNCOMMENT TO CLEAR EVALUATION LINE AFTER USE
 650  RETURN 
 1200 M$ =  CHR$ (13)
 1210 G$ =  CHR$ (7)
 1220  HOME 
 1230  PRINT  SPC( 16)"24 GAME"
 1240  PRINT M$" THE GOAL OF THIS GAME IS TO FORMULATE"
 1250  PRINT M$" AN ARITHMETIC EXPRESSION THAT"
 1260  PRINT M$" EVALUATES TO A VALUE OF 24, HOWEVER"
 1270  PRINT M$" YOU MAY USE ONLY THE FOUR NUMBERS"
 1280  PRINT M$" GIVEN AT RANDOM BY THE COMPUTER AND"
 1290  PRINT M$" THE STANDARD ARITHMETIC OPERATIONS OF"
 1300  PRINT M$" ADD, SUBTRACT, MULTIPLY, AND DIVIDE."
 1310  PRINT M$" EACH DIGIT MUST BE USED BY ITSELF. "
 1320  PRINT M$" (E.G. IF GIVEN 1, 2, 3, 4, YOU CANNOT"
 1330  PRINT M$" COMBINE 1 AND 2 TO MAKE 12.)"
 1340  PRINT M$
 1350  PRINT "INITIALIZING...";
 1360  HTAB 1
 1400  DIM T(94)
 1401 T( ASC ("+")) = 200: REM $C8
 1402 T( ASC ("-")) = 201: REM $C9
 1403 T( ASC ("*")) = 202: REM $CA
 1404 T( ASC ("/")) = 203: REM $CB
 1405 T( ASC ("=")) = 208: REM $D0
 1406 T( ASC ("^")) = 204: REM $CC
 1409  REMLOCATE LINE 2005 IN RAM
 1410 LH = BH:LL = BL:NH = 0:NL = 0
 1415 AD = LH * 256 + LL
 1420 LH =  PEEK (AD + 1):LL =  PEEK (AD)
 1425 NL =  PEEK (AD + 2):NH =  PEEK (AD + 3):N = NH * 256 + NL
 1430  IF N <  > 2005 THEN  GOTO 1415
 1435 AD = AD + 4: RETURN 
 1440  FOR J = AD TO AD + 12: POKE J, ASC (":"): NEXT 
 1445  RETURN 
 2000  REMPUT 13 COLONS ON THE NEXT LINE
 2005 :::::::::::::
 2010  RETURN 
 