45 lines
834 B
Text
45 lines
834 B
Text
game()={
|
|
my(v=vecsort(vector(4,i,random(8)+1)));
|
|
print("Form 24 using */+-() and: "v);
|
|
while(1,
|
|
my(ans=input);
|
|
if (!valid(s,v), next);
|
|
trap(,
|
|
print("Arithmetic error");
|
|
next
|
|
,
|
|
if(eval(s)==24, break, print("Bad sum"))
|
|
)
|
|
);
|
|
print("You win!")
|
|
};
|
|
valid(s,v)={
|
|
my(op=vecsort(Vec("+-*/()")),u=[]);
|
|
s=Vec(s);
|
|
for(i=1,#s,
|
|
if(setsearch(op,s[i]),next);
|
|
trap(,
|
|
print("Invalid character "s[i]);
|
|
return(0)
|
|
,
|
|
if(setsearch(v,eval(s[i])),
|
|
u=concat(u,eval(s[i]))
|
|
,
|
|
print(s[i]" not allowed");
|
|
return(0)
|
|
)
|
|
)
|
|
);
|
|
for(i=2,#s,
|
|
if(!setsearch(op,s[i])&!setsearch(op,s[i-1]),
|
|
print("Concatenating digits is not allowed!");
|
|
return(0)
|
|
)
|
|
);
|
|
if(vecsort(u)!=v,
|
|
print("Invalid digits");
|
|
0
|
|
,
|
|
1
|
|
)
|
|
};
|