180 lines
3.6 KiB
Plaintext
180 lines
3.6 KiB
Plaintext
operators$ = "*+-/"
|
|
space$ = " "
|
|
|
|
sub present()
|
|
clear screen
|
|
print "24 Game"
|
|
print "============\n"
|
|
print "Computer provide 4 numbers (1 to 9). With operators +, -, * and / you try to\nobtain 24."
|
|
print "Use Reverse Polish Notation (first operand and then the operators)"
|
|
print "For example: instead of 2 + 4, type 2 4 +\n\n"
|
|
end sub
|
|
|
|
repeat
|
|
present()
|
|
serie$ = sortString$(genSerie$())
|
|
valid$ = serie$+operators$
|
|
print "If you give up, press ENTER and the program attempts to find a solution."
|
|
line input "Write your solution: " input$
|
|
if input$ = "" then
|
|
print "Thinking ... "
|
|
res$ = explorer$()
|
|
if res$ = "" print "Can not get 24 with these numbers.."
|
|
else
|
|
input$ = delSpace$(input$)
|
|
inputSort$ = sortString$(input$)
|
|
if (right$(inputSort$,4) <> serie$) or (len(inputSort$)<>7) then
|
|
print "Syntax error"
|
|
else
|
|
result = evalInput(input$)
|
|
print "Your solution = ",result," is ";
|
|
if result = 24 then
|
|
print "Correct!"
|
|
else
|
|
print "Wrong!"
|
|
end if
|
|
end if
|
|
end if
|
|
print "\nDo you want to try again? (press N for exit, other key to continue)"
|
|
until(upper$(left$(inkey$(),1)) = "N")
|
|
|
|
exit
|
|
|
|
sub genSerie$()
|
|
local i, c$, s$
|
|
|
|
print "The numbers you should use are: ";
|
|
i = ran()
|
|
for i = 1 to 4
|
|
c$ = str$(int(ran(9))+1)
|
|
print c$," ";
|
|
s$ = s$ + c$
|
|
next i
|
|
print
|
|
return s$
|
|
end sub
|
|
|
|
|
|
sub evalInput(entr$)
|
|
local d1, d2, c$, n(4), i
|
|
|
|
while(entr$<>"")
|
|
c$ = left$(entr$,1)
|
|
entr$ = mid$(entr$,2)
|
|
if instr(serie$,c$) then
|
|
i = i + 1
|
|
n(i) = val(c$)
|
|
elseif instr(operators$,c$) then
|
|
d2 = n(i)
|
|
n(i) = 0
|
|
i = i - 1
|
|
if i = 0 return
|
|
d1 = n(i)
|
|
n(i) = evaluator(d1, d2, c$)
|
|
else
|
|
print "Invalid symbol"
|
|
return
|
|
end if
|
|
wend
|
|
|
|
return n(i)
|
|
|
|
end sub
|
|
|
|
|
|
sub evaluator(d1, d2, op$)
|
|
local t
|
|
|
|
switch op$
|
|
case "+": t = d1 + d2 : break
|
|
case "-": t = d1 - d2 : break
|
|
case "*": t = d1 * d2 : break
|
|
case "/": t = d1 / d2 : break
|
|
end switch
|
|
|
|
return t
|
|
end sub
|
|
|
|
|
|
sub delSpace$(entr$)
|
|
local n, i, s$, t$(1)
|
|
|
|
n = token(entr$,t$()," ")
|
|
|
|
for i=1 to n
|
|
s$ = s$ + t$(i)
|
|
next i
|
|
return s$
|
|
end sub
|
|
|
|
|
|
sub sortString$(string$)
|
|
local signal, n, fin, c$
|
|
|
|
fin = len(string$)-1
|
|
repeat
|
|
signal = false
|
|
for n = 1 to fin
|
|
if mid$(string$,n,1) > mid$(string$,n+1,1) then
|
|
signal = true
|
|
c$ = mid$(string$,n,1)
|
|
mid$(string$,n,1) = mid$(string$,n+1,1)
|
|
mid$(string$,n+1,1) = c$
|
|
end if
|
|
next n
|
|
until(signal = false)
|
|
return string$
|
|
end sub
|
|
|
|
|
|
sub explorer$()
|
|
local d1,d2,o3,x4,x5,x6,o7,p$,result,solution,solutions$,n
|
|
|
|
for d1 = 1 to 4
|
|
for d2 = 1 to 4
|
|
for o3 = 1 to 4
|
|
for x4 = 1 to 8
|
|
for x5 = 1 to 8
|
|
for x6 = 1 to 8
|
|
for o7 = 1 to 4
|
|
p$ = mid$(serie$,d1,1)+mid$(serie$,d2,1)+mid$(operators$,o3,1)
|
|
p$ = p$+mid$(valid$,x4,1)+mid$(valid$,x5,1)+mid$(valid$,x6,1)
|
|
p$ = p$+mid$(operators$,o7,1)
|
|
if not instr(solutions$,p$) then
|
|
if validateInput(p$) then
|
|
result = evalInput(p$)
|
|
if result = 24 then
|
|
solution = solution + 1
|
|
print "Solution: ",solution," = ";
|
|
solutions$ = solutions$ + p$
|
|
for n = 1 to 7
|
|
print mid$(p$,n,1)," ";
|
|
next n
|
|
print
|
|
end if
|
|
end if
|
|
end if
|
|
next o7
|
|
next x6
|
|
next x5
|
|
next x4
|
|
next o3
|
|
next d2
|
|
next d1
|
|
return p$
|
|
end sub
|
|
|
|
|
|
sub validateInput(e$)
|
|
local n, inputSort$
|
|
|
|
inputSort$ = sortString$(e$)
|
|
if serie$ <> right$(inputSort$,4) return false
|
|
for n=1 to 3
|
|
if not instr(operators$,mid$(inputSort$,n,1)) then
|
|
return false
|
|
end if
|
|
next n
|
|
return true
|
|
end sub
|