Delphi幻方源代码

Form1: TForm1;
GeLieZhiHe,GeHangZhiHe:array[0…1000] of integer;
intStringGrid1:array[0…1000,0…1000] of integer;
XingLa: boolean;
MaxNum,HuanFangNum:integer;
implementation
//uses
//Unit2;
{$R *.dfm}
{ 开始计算}
procedure TForm1.KaiShiJiSuan(Sender: TObject);
var
i,j,k:integer;

begin
for i:=0 to 1000 do
begin
GeLieZhiHe [i]:=0; // 各列之和
GeHangZhiHe[i]:=0;
end;

//幻方数
HuanFangNum:=StrToInt(Edit1.text) ;
// 最大数
MaxNum:=sqr(HuanFangNum );
// StringGrid1 方格行数,列数为幻方数
StringGrid1.ColCount:=HuanFangNum ;
StringGrid1.RowCount:=HuanFangNum;

{ intStringGrid1 方格全部填写为0}
for i:=0 to HuanFangNum-1 do
for j:=0 to HuanFangNum-1 do
intStringGrid1[i,j]:=0;

TianXieBenGe(0,0);

end;

                  {填写本格}

procedure TForm1.TianXieBenGe(ACol,ARow:integer);
var
// k :integer;
// i,j,k,Int1,Int2:integer;
{ 无相同数}
//function WuXiangTongShu:boolean;
//var
i,j,k,m,ANum:integer;
SuoYouDeShu1,SuoYouDeShu2:array[0…1000] of integer;
resut:boolean;
begin
//StringGrid1.Visible:=true;

//end;

//begin
//if result=false then
if ACol<=HuanFangNum-1 then
begin

     for  m:=1  to MaxNum do
     begin

begin//-----------------------------------

for i:=0 to 1000 do
SuoYouDeShu1[i] :=0;

resut:=true;

k:=0;
// StringGrid1 中所有的数
for i:=0 to HuanFangNum-1 do
for j:=0 to HuanFangNum-1 do
begin
ANum:=intStringGrid1[i,j] ;
//for k:=0 to MaxNum do begin
SuoYouDeShu1[k] :=ANum;

             inc(k);
             if k=MaxNum  then break;
         // end;

     end;

SuoYouDeShu2:=SuoYouDeShu1;

for  i:=0  to  MaxNum-1 do begin
     for  j:=0  to  MaxNum-1 do begin
         if  i<>j then
           if  SuoYouDeShu1[i]=SuoYouDeShu2[j] then
             begin
               
               resut:=false;
               break;
               //showmessage('WuXiangTongShu=false;');
             end;
     end;
end;

end; //--------------------------------------------------------

       if   resut=true then
         begin
           winexec('C:\Program Files\SlowView\SlowView.exe C:\Program Files\acc\Wave\Ringin.wav',sw_shownormal);
           //showmessage



           break;


         end;
      if   resut=false then
       begin
           //showmessage('无相同数false');
         intStringGrid1[ACol,ARow]:=m;
       // StringGrid1.Refresh;
         //  showmessage('StringGrid1 方格填写'+inttostr(k));
       // Form2.Show;
       // Form2.Hide;
           TianXieXiaYiGe(ACol,ARow);

        end;
     end;

end;

end;
{填写下一格}
procedure TForm1.TianXieXiaYiGe(ACol,ARow:integer);
var
i,j,k,Int1,Int2:integer;

begin

 i:=ACol;
 j:=ARow+1;
 if j=HuanFangNum then
 begin
   inc(i);
   j:=0;
 end;

 int1:=i;
 int2:=j;

TianXieBenGe(Int1,Int2 );

// TianXieBenGe(int1,int2);
end;
function TForm1.FuHeTiaoJian:boolean;
begin
result:=false;
// if WuXiangTongShu=true then
//if GeHangGeLieXiangTong=true then
result:=true;
end;

          {   验证各行各列 }

function TForm1.GeHangGeLieXiangTong:boolean;
var
i,j,k:integer;
// GeLieZhiHe,GeHangZhiHe:array[0…1000] of integer;

begin
result:=false;
for i:=0 to StringGrid1.ColCount-1 do
for j:=0 to StringGrid1.RowCount-1 do
GeLieZhiHe[i]:=GeLieZhiHe[i ]+ strtoint(StringGrid1.Cells[i,j]);

for  i:=0  to StringGrid1.RowCount-1 do
  for  j:=0  to StringGrid1.ColCount-1 do
   GeHangZhiHe[i]:=GeHangZhiHe[i ]+ strtoint(StringGrid1.Cells[i,j]);

for i:=0 to StringGrid1.ColCount-2 do begin

    if GeLieZhiHe[i]=GeLieZhiHe[i+1] then begin

       for  j:=0  to StringGrid1.ColCount-1 do begin
         if GeHangZhiHe[j]=GeHangZhiHe[j+1] then  begin
           if GeHangZhiHe[0]=GeLieZhiHe[0] then begin

               result:=true;


           end;
         end;
       end;
    end;

end;

//
end;
{ 验证2} //
procedure TForm1.YanZheng2;
begin
showmessage(’ XingLa’); XingLa:=true;
// StringGrid1.Visible:=true;

end;

procedure TForm1.Button2Click(Sender: TObject);

var
i,j,k,ANum:integer;
SuoYouDeShu1,SuoYouDeShu2:array[0…1000] of integer;
begin

//幻方数
HuanFangNum:=StrToInt(Edit1.text) ;
// 最大数
MaxNum:=sqr(HuanFangNum );
// StringGrid1 方格行数,列数为幻方数
StringGrid1.ColCount:=HuanFangNum ;
StringGrid1.RowCount:=HuanFangNum;

{ StringGrid1 方格全部填写为0}
for i:=0 to HuanFangNum-1 do
for j:=0 to HuanFangNum-1 do
StringGrid1.Cells[i,j]:=inttostr(0);

k:=0;
// StringGrid1 中所有的数
for i:=0 to HuanFangNum-1 do
for j:=0 to HuanFangNum-1 do
begin
ANum:=strtoint(StringGrid2.Cells[i,j]) ;
//for k:=0 to MaxNum do begin
SuoYouDeShu1[k] :=ANum;

             inc(k);
             if k=MaxNum  then break;
         // end;

     end;

SuoYouDeShu2:=SuoYouDeShu1;

for  i:=0  to  MaxNum-1 do begin
     for  j:=0  to  MaxNum-1 do begin
           if  i<>j then
             if  SuoYouDeShu1[i]=SuoYouDeShu2[j] then
             begin


               showmessage('WuXiangTongShu=false;');
               break;
             end;
     end;
end;

//StringGrid1.Visible:=true;

end;

procedure TForm1.Button3Click(Sender: TObject);
begin
StringGrid2.Cells[0,0]:=inttostr(0);
StringGrid2.Cells[0,1]:=inttostr(1);
StringGrid2.Cells[1,0]:=inttostr(2);
StringGrid2.Cells[1,1]:=inttostr(30);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
StringGrid2.Cells[0,0]:=inttostr(0);
StringGrid2.Cells[0,1]:=inttostr(1);
StringGrid2.Cells[1,0]:=inttostr(1);
StringGrid2.Cells[1,1]:=inttostr(30);

end;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值