dim d(4) dim chk(4) print "The 24 Game" print print "Given four digits and using just the +, -, *, and / operators; and the" print "possible use of brackets, (), enter an expression that equates to 24." do d$="" for i = 1 to 4 d(i)=int(rnd(1)*9)+1 '1..9 chk(i)=d(i) d$=d$;d(i) 'valid digits, to check with Instr next print print "These are your four digits: "; for i = 1 to 4 print d(i);left$(",",i<>4); next print Print "Enter expression:" Input "24 = ";expr$ 'check expr$ for validity 'check right digits used failed = 0 for i = 1 to len(expr$) c$=mid$(expr$,i,1) if instr("123456789", c$)<>0 then 'digit if instr(d$, c$)=0 then failed = 1: exit for if i>1 and instr("123456789", mid$(expr$,i-1,1))<>0 then failed = 2: exit for for j =1 to 4 if chk(j)=val(c$) then chk(j)=0: exit for next end if next if failed=1 then print "Wrong digit (";c$;")" goto [fail] end if if failed=2 then print "Multiple digit numbers is disallowed." goto [fail] end if 'check all digits used if chk(1)+ chk(2)+ chk(3)+ chk(4)<>0 then print "Not all digits used" goto [fail] end if 'check valid operations failed = 0 for i = 1 to len(expr$) c$=mid$(expr$,i,1) if instr("+-*/()"+d$, c$)=0 then failed = 1: exit for next if failed then print "Wrong operation (";c$;")" goto [fail] end if 'some errors (like brackets) trapped by error handler Err$="" res=evalWithErrCheck(expr$) if Err$<>"" then print "Error in expression" goto [fail] end if if res = 24 then print "Correct!" else print "Wrong! (you got ";res ;")" end if [fail] Input "Play again (y/n)? "; ans$ loop while ans$="y" end function evalWithErrCheck(expr$) on error goto [handler] evalWithErrCheck=eval(expr$) exit function [handler] end function