谁有二十四点游戏的代码

zhu_2022 2002-04-01 09:10:55
我想做个二十四点游戏,希望能找个算法
email:freecafe@etang.com
...全文
87 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
maxsuy 2002-04-02
  • 打赏
  • 举报
回复
dsgd
zhu_2022 2002-04-01
  • 打赏
  • 举报
回复
程序调通了 ,我正在啃代码吗,看看能不能优化,就这个问题而言,效率不是主要问题,但对算法设计者来说,提高一点都是孜孜以求的。

谢谢,给分!
chinakiss8848 2002-04-01
  • 打赏
  • 举报
回复
这个简单程序没用考虑括号和运算符号的优先级,所以结果都是以:
(((a j1 b) j2 c) j3 d)

其中a、b、c、d是整数j1、j2、j3为运算符号!

你只要在窗体上加4个EDIT控件、1个ListBox控件、1个Label、1个Button。

要考虑括号和运算符号的优先级用二叉树就可以了,自己试吧。
chinakiss8848 2002-04-01
  • 打赏
  • 举报
回复
我前天做了个简单的24点的程序(绝对不是最优解,不过可以给你参考一下),见笑:
procedure TForm1.Button1Click(Sender: TObject);
var
sum : array[1..4] of integer;
fuhao :array[1..4] of byte; // '+','-','*','/' => 0,1,2,3
fh :array[0..3] of char;
i,j,k,l :integer; //sum
m,n,o :integer; //fuhao
jsjieguo :integer;
//'+' '-' '*' '/' -> 0 1 2 3
function JS(const a,b,c,d:Integer; const j1,j2,j3:byte):integer;

function sf(const x,y:Integer; const jj:byte):integer;
begin
case jj of
0: sf:=x+y;
1: sf:=x-y;
2: sf:=x*y;
3: if y<>0 then
if x = ((x div y)*y) then sf := (x div y) else sf := 9991
else sf := 9991 ;
end;
end;

begin
JS := sf( sf( sf(a,b,j1),c,j2 ), d,j3);// (a j1 b j2 c j3 d)
end;
begin
sum[1]:=strToint(Edit1.Text);
sum[2]:=strToint(Edit2.Text);
sum[3]:=strToint(Edit3.Text);
sum[4]:=strToint(Edit4.Text);
ListBox1.Items.Add('====='+Edit1.Text + ' ' + Edit2.Text + ' ' +
Edit3.Text + ' ' + Edit4.Text+'=====');
ListBox1.TopIndex :=ListBox1.Items.count-1;
fuhao[1]:=0; fuhao[2]:=1; fuhao[3]:=2; fuhao[4]:=3;

fh[0]:='+'; fh[1]:='-'; fh[2]:='*'; fh[3]:='/';

for i:=1 to 4 do
for j:=1 to 4 do
if (i<>j) then
for k:=1 to 4 do
if(i<>k) and (j<>k) then
for l:=1 to 4 do
if(i<>l) and (j<>l) and (k<>l) then
begin
for m:=1 to 4 do
for n:=1 to 4 do
// if(m<>n) then
for o:=1 to 4 do
// if (m<>o) and (n<>o) then
begin
jsjieguo := JS(sum[i],sum[j],sum[k],sum[l],fuhao[m],fuhao[n], fuhao[o]);
if (jsjieguo<1000) and (Jsjieguo=24) then
begin
Label1.Caption:=intTostr(jsjieguo);
ListBox1.Items.Add('((('+
intTostr(sum[i])+fh[fuhao[m]]+intTostr(sum[j])+')'+
fh[fuhao[n]]+intTostr(sum[k])+')'+
fh[fuhao[o]]+intTostr(sum[l])+')'+
'='+intTostr(jsjieguo));
ListBox1.TopIndex :=ListBox1.Items.count-1;
end;
end;
end;
Label2.Caption := 'End';
end;

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi GAME,图形处理/多媒体
社区管理员
  • GAME,图形处理/多媒体社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧