⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 migong.pas

📁 迷宫原代码设计
💻 PAS
字号:
unit migong;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type
  node=record
    state:0..1;
    rect:trect;
    bitmap:tbitmap;
    index:integer;
    order:0..1;
  end;
  TForm1 = class(TForm)
    Panel2: TPanel;
    Button1: TButton;
    Button2: TButton;
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    Label1: TLabel;
    Label2: TLabel;
    Button4: TButton;
    Image1: TImage;
    Panel1: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    Panel5: TPanel;
    Image2: TImage;
    Image3: TImage;
    Timer1: TTimer;
    Image4: TImage;
    Image5: TImage;
    Image6: TImage;
    Image7: TImage;
    Label3: TLabel;
    Label4: TLabel;
    ComboBox3: TComboBox;
    Label5: TLabel;
    Label6: TLabel;
    Timer2: TTimer;
    Timer3: TTimer;
    ComboBox4: TComboBox;
    Label7: TLabel;
    procedure FormPaint(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Timer3Timer(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  listnode:array[1..10,1..10] of node;
  tm,i0,j0:integer;
implementation

{$R *.dfm}

procedure TForm1.FormPaint(Sender: TObject);
var
  i,j:integer;
begin

tm:=0;
for i:=1 to 10 do
   for j:=1 to 10 do
     begin
     form1.Canvas.Pen.Color:=clred;
     form1.Canvas.Pen.Width:=4;
     form1.canvas.Rectangle(50+40*(i-1),50+40*(j-1),90+40*(i-1),90+40*(j-1));
     end;
for i:=1 to 10 do
   for j:=1 to 10 do
   begin
   listnode[i,j].rect:=rect(50+40*(j-1),50+40*(i-1),90+40*(j-1),90+40*(i-1));
   listnode[i,j].index:=10*(i-1)+j;
   listnode[i,j].order:=0;
   end;
    listnode[1,1].state:=0;
    image2.Picture.LoadFromFile('E:\My Pictures\T-MAC\278_2004103118402950986.bmp');
    image3.Picture.LoadFromFile('E:\My Pictures\T-MAC\278_2004103118402950986.bmp');
    image4.Picture.LoadFromFile('E:\My Pictures\T-MAC\278_2004103118402950986.bmp');
    image5.Picture.LoadFromFile('E:\My Pictures\T-MAC\278_2004103118402950986.bmp');
    image6.Picture.LoadFromFile('E:\My Pictures\T-MAC\278_2004103118402950986.bmp');
    image7.Picture.LoadFromFile('E:\My Pictures\T-MAC\278_2004103118402950986.bmp');
  end;

procedure TForm1.Button1Click(Sender: TObject);
var i,j,k1,k,h,n:integer;
    a:array[1..100] of integer;
begin
randomize;
form1.hide;
form1.Show;

n:=0;
for i:=1 to 100 do a[i]:=0;
k:=strtoint(combobox1.Items[combobox1.ItemIndex]);
while n<k do
begin
   for i:=1 to 10 do
   for j:=1 to 10 do
      listnode[i,j].state:=0;
  k1:=random(101);
  if a[k1]=0 then
  begin
    a[k1]:=1;
    n:=n+1;
  end;
  if a[k1]=1 then a[k1]:=1;
end;
for h:=1 to 100 do
begin
   if a[h]=1 then
   begin
     for i:=1 to 10 do
       for j:=1 to 10 do
           if listnode[i,j].index=h then listnode[i,j].state:=1;
   end;

end;

for i:=1 to 10 do
  for j:=1 to 10 do
  begin
    if listnode[i,j].state=1 then
    begin
    form1.Canvas.Brush.Style:=bscross;
    form1.Canvas.FillRect(listnode[i,j].rect);
    end;
  end;

end;

procedure TForm1.Timer1Timer(Sender: TObject);

begin
tm:=tm+1;
if odd(tm) then
label3.Visible:=false
else
label3.Visible:=true;
if tm>100000 then tm:=0;
end;


procedure TForm1.Button2Click(Sender: TObject);
var i,j,k:integer;
    s:string;
    bitmap:tbitmap;
begin
timer2.Enabled:=false;
timer3.Enabled:=false;
bitmap:=tbitmap.Create;
bitmap.LoadFromFile('E:\My Pictures\T-MAC\278_2004103118402950986.bmp');
k:=strtoint(combobox3.Items[combobox3.ItemIndex]);
for i:=1 to 10 do
  for j:=1 to 10 do
  begin
   if (listnode[i,j].index=k) and (listnode[i,j].state=1) then
   begin

   showmessage('有障碍,你不能从此开始,请选择别处开始');
   end;
   if (listnode[i,j].index=k) and (listnode[i,j].state=0) then
   begin
   form1.Canvas.Brush.Bitmap:=bitmap;
   form1.Canvas.FillRect(listnode[i,j].rect);
   end;
  end;
for i:=1 to 10 do
  for j:=1 to 10 do
  begin
   if (listnode[i,j].index=k) and (listnode[i,j].state=0) then
   begin
   i0:=i;
   j0:=j;
   end;
 end;

s:=combobox2.Items[combobox2.ItemIndex];
if s='广度优先方式' then  timer3.Enabled:=true;
if s='深度优先方式' then  timer2.Enabled:=true;
end;


procedure TForm1.Timer3Timer(Sender: TObject);
var i,j,i1,j1:integer;
    bitmap:tbitmap;
begin
i1:=i0;
j1:=j0;     //广度优先
bitmap:=tbitmap.Create;
bitmap.LoadFromFile('E:\My Pictures\T-MAC\278_2004103118402950986.bmp');


if (j1=1)and (listnode[i1,j1+1].state=1)and(listnode[i1+1,j1].state=0) then i0:=i0+1;
if (j1=1)and (listnode[i1,j1+1].order=1)and(listnode[i1+1,j1].state=0) then i0:=i0+1;
if (j1=1)and (listnode[i1,j1+1].state=0)and(listnode[i1+1,j1].state=0) then j0:=j0+1;
if (j1=1)and (listnode[i1,j1+1].state=0)and(listnode[i1+1,j1].state=1) then j0:=j0+1;
if (j1=1)and (listnode[i1,j1+1].state=1)and(listnode[i1+1,j1].state=1) then
begin
timer3.Enabled:=false;
if messagedlg('Sorry,can''t complete the game,Once more??',mtwarning,[mbyes,mbno],0)=mryes then
button1.Click
else
button4.Click;
end;
if (j1=1)and (listnode[i1,j1+1].order=1)and(listnode[i1+1,j1].state=1) then
begin
timer3.Enabled:=false;
if messagedlg('Sorry,can''t complete the game,Once more??',mtwarning,[mbyes,mbno],0)=mryes then
button1.Click
else
button4.Click;
end;


if (listnode[i1,j1+1].state=0)and (listnode[i1,j1-1].state=1) and (listnode[i1,j1+1].order=0) then j0:=j0+1;
if (listnode[i1,j1+1].state=0)and (listnode[i1,j1-1].order=1) then j0:=j0+1;
if (listnode[i1,j1+1].state=1)and (listnode[i1,j1-1].state=0) and (listnode[i1,j1-1].order=0) then j0:=j0-1;
if (listnode[i1,j1+1].order=1)and (listnode[i1,j1-1].state=0) then j0:=j0-1;

if (listnode[i1,j1+1].state=1)and (listnode[i1,j1-1].state=1)and (listnode[i1+1,j1].state=0) then i0:=i0+1;
if (listnode[i1,j1+1].state=1)and (listnode[i1,j1-1].order=1)and (listnode[i1+1,j1].state=0) then i0:=i0+1;
if (listnode[i1,j1+1].order=1)and (listnode[i1,j1-1].state=1)and (listnode[i1+1,j1].state=0) then i0:=i0+1;
if (listnode[i1,j1+1].state=0)and (listnode[i1,j1-1].state=0) and (listnode[i1,j1-1].order=0)and(listnode[i1,j1+1].order=0) then
begin
if combobox4.Items[combobox4.ItemIndex]='左' then j0:=j0-1
else j0:=j0+1;
end;
if (listnode[i1,j1+1].state=1) and (listnode[i1,j1-1].state=1) and (listnode[i1+1,j1].state=1) then
begin
timer3.Enabled:=false;
if messagedlg('Sorry,can''t complete the game,Once more??',mtwarning,[mbyes,mbno],0)=mryes then
button1.Click
else
button4.Click;
end;
if (listnode[i1,j1+1].state=1) and (listnode[i1,j1-1].order=1) and (listnode[i1+1,j1].state=1) then
begin
timer3.Enabled:=false;
if messagedlg('Sorry,can''t complete the game,Once more??',mtwarning,[mbyes,mbno],0)=mryes then
button1.Click
else
button4.Click;
end;
if (listnode[i1,j1+1].order=1) and (listnode[i1,j1-1].state=1) and (listnode[i1+1,j1].state=1) then
begin
timer3.Enabled:=false;
if messagedlg('Sorry,can''t complete the game,Once more??',mtwarning,[mbyes,mbno],0)=mryes then
button1.Click
else
button4.Click;
end;

listnode[i0,j0].order:=1;
for i:=1 to 10 do
  for j:=1 to 10 do
  begin
    if listnode[i,j].state=1 then
    begin
    form1.Canvas.Brush.style:=bscross;
    form1.Canvas.FillRect(listnode[i,j].rect);
    end;
  end;
form1.Canvas.Brush.Bitmap:=bitmap;
form1.Canvas.FillRect(listnode[i0,j0].rect);

if (i0=10)or(j0=10) then
begin
 timer3.Enabled:=false;
 if messagedlg('Congradulate!! Once More!!',mtconfirmation,[mbyes,mbno],0)=mryes then
 button1.click
 else
   if messagedlg('Do you want to exit the game?',mtconfirmation,[mbyes,mbno],0)=mryes then
   button4.click;
end; 

end;


procedure TForm1.Timer2Timer(Sender: TObject);
var i,j,i1,j1:integer;
    bitmap:tbitmap;
begin //深度优先
i1:=i0;
j1:=j0;
bitmap:=tbitmap.Create;
bitmap.LoadFromFile('E:\My Pictures\T-MAC\278_2004103118402950986.bmp');

if (j1=1)and (listnode[i1,j1+1].state=1)and(listnode[i1+1,j1].state=0) then i0:=i0+1;
if (j1=1)and (listnode[i1,j1+1].order=1)and(listnode[i1+1,j1].state=0) then i0:=i0+1;
if (j1=1)and (listnode[i1,j1+1].state=0)and(listnode[i1+1,j1].state=0) then i0:=i0+1;
if (j1=1)and (listnode[i1,j1+1].state=0)and(listnode[i1+1,j1].state=1) then j0:=j0+1;
if (j1=1)and (listnode[i1,j1+1].state=1)and(listnode[i1+1,j1].state=1) then
begin
timer2.Enabled:=false;
if messagedlg('Sorry,can''t complete the game,Once more??',mtwarning,[mbyes,mbno],0)=mryes then
button1.Click
else
button4.Click;
end;
if (j1=1)and (listnode[i1,j1+1].order=1)and(listnode[i1+1,j1].state=1) then
begin
timer2.Enabled:=false;
if messagedlg('Sorry,can''t complete the game,Once more??',mtwarning,[mbyes,mbno],0)=mryes then
button1.Click
else
button4.Click;
end;

if listnode[i1+1,j1].state=0 then i0:=i0+1;
if (listnode[i1,j1+1].state=1) and (listnode[i1,j1-1].state=1) and (listnode[i1+1,j1].state=1) then
begin
timer2.Enabled:=false;
if messagedlg('Sorry,can''t complete the game,Once more??',mtwarning,[mbyes,mbno],0)=mryes then
button1.Click
else
button4.Click;
end;
if (listnode[i1,j1+1].state=1) and (listnode[i1,j1-1].order=1) and (listnode[i1+1,j1].state=1) then
begin
timer2.Enabled:=false;
if messagedlg('Sorry,can''t complete the game,Once more??',mtwarning,[mbyes,mbno],0)=mryes then
button1.Click
else
button4.Click;
end;
if (listnode[i1,j1+1].state=0)and(listnode[i1,j1+1].order=0) and (listnode[i1,j1-1].state=0)and(listnode[i1,j1-1].order=0)and (listnode[i1+1,j1].state=1) then
begin
if combobox4.Items[combobox4.ItemIndex]='左' then j0:=j0-1
else
j0:=j0+1;
end;
if (listnode[i1,j1+1].order=1) and (listnode[i1,j1-1].state=0)and (listnode[i1+1,j1].state=1) then j0:=j0-1;
//if (listnode[i1,j1-1].state=1) and (listnode[i1,j1+1].order=1)and (listnode[i1+1,j1].state=0) then i0:=i0+1;
//if (listnode[i1,j1+1].state=1) and (listnode[i1,j1-1].order=1)and (listnode[i1+1,j1].state=0) then i0:=i0+1;
if (listnode[i1,j1+1].state=0) and (listnode[i1,j1-1].order=1) and (listnode[i1+1,j1].state=1) then j0:=j0+1;
if (listnode[i1,j1-1].state=1) and (listnode[i1+1,j1].state=1) and (listnode[i1,j1+1].state=0) then j0:=j0+1;
if (listnode[i1,j1+1].state=1) and (listnode[i1+1,j1].state=1) and (listnode[i1,j1-1].state=0) then j0:=j0-1;

listnode[i0,j0].order:=1;
for i:=1 to 10 do
  for j:=1 to 10 do
  begin
    if (listnode[i,j].state=1)and(listnode[i,j].order<>1) then
    begin
    form1.Canvas.Brush.Style:=bscross;
    form1.Canvas.FillRect(listnode[i,j].rect);
    end;
  end;
form1.Canvas.Brush.bitmap:=bitmap;
form1.Canvas.FillRect(listnode[i0,j0].rect);
if (i0=10)or(j0=10) then
begin
 timer2.Enabled:=false;
 if messagedlg('Congradulate!! Once More!!',mtconfirmation,[mbyes,mbno],0)=mryes then
 button1.click
 else
   if messagedlg('Do you want to exit the game?',mtconfirmation,[mbyes,mbno],0)=mryes then
   button4.click;
end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var k,i,j:integer;
begin
form1.Hide;
form1.Show;
k:=strtoint(combobox3.Items[combobox3.ItemIndex]);
for i:=1 to 10 do
  for j:=1 to 10 do
  begin
   if (listnode[i,j].index=k) and (listnode[i,j].state=0) then
   begin
   i0:=i;
   j0:=j;
   end;
end;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
form1.Top:=20;
form1.Left:=250;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
if messagedlg('确定要退出吗?',mtwarning,[mbyes,mbno],0)=mryes then
form1.Close;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -