主题:pascal怎样做24点游戏?
大只菜鸟
[专家分:0] 发布于 2009-12-10 14:08:00
扑克牌随机抽出4张牌,然后玩家自己输入表达式算24点,由程序判断正确与否
板凳
璇木新兰 [专家分:0] 发布于 2010-01-03 00:02:00
我的是你打进四个数,他帮你算:
{$N+}
const
s : array[1..4] of char = ('+','-','*','/');
var
a : array[1..4] of integer;
i,j,k,l,x,y,z : integer;
function cal (m : extended; o : integer; n : extended) : extended;
begin
case o of
1 : cal := m+n;
2 : cal := m-n;
3 : cal := m*n;
4 : if n = 0 then cal := 1e+38 else cal := m/n;
end;
end;
procedure solve;
begin
if abs((cal(cal(cal(a[i],x,a[j]),y,a[k]),z,a[l]))-24) < 0.001 then begin
writeln('((',a[i],s[x],a[j],')',s[y],a[k],')',s[z],a[l],' = 24');
readln; halt; end;
if abs(cal(cal(a[i],x,cal(a[j],y,a[k])),z,a[l])-24) < 0.001 then begin
writeln('(',a[i],s[x],'(',a[j],s[y],a[k],'))',s[z],a[l],' = 24');
readln; halt; end;
if abs((cal(cal(a[i],x,a[j]),y,cal(a[k],z,a[l])))-24) < 0.001 then begin
writeln('(',a[i],s[x],a[j],')',s[y],'(',a[k],s[z],a[l],') = 24');
readln; halt; end;
if abs((cal(a[i],x,cal(a[j],y,cal(a[k],z,a[l]))))-24) < 0.001 then begin
writeln(a[i],s[x],'(',a[j],s[y],'(',a[k],s[z],a[l],')) = 24');
readln; halt; end;
if abs(cal(a[i],x,cal(cal(a[j],y,a[k]),z,a[l]))-24) < 0.001 then begin
writeln(a[i],s[x],'((',a[j],s[y],a[k],')',s[z],a[l],') = 24');
readln; halt; end;
end;
begin
writeln('----- 24 points -----');
repeat
write('Input : ');
readln(a[1],a[2],a[3],a[4]);
until (a[1] in [1..13]) and (a[2] in [1..13])
and (a[3] in [1..13]) and (a[4] in [1..13]);
{ randomize;
for i := 1 to 4 do begin
a[i] := random(13)+1;
write(a[i],' ');
end;
readln;}
for i := 1 to 4 do
for j := 1 to 4 do
for k := 1 to 4 do
for l := 1 to 4 do
if [i,j,k,l] = [1..4] then
for x := 1 to 4 do
for y := 1 to 4 do
for z := 1 to 4 do solve;
writeln('No solution !');
readln;
end.
{}部分是随机选数,不过每次好像都是一样的。。。。。。
~-~||||