unit Labirint;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, ExtCtrls, Spin, ImgList;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
GroupBox1: TGroupBox;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
RadioButton3: TRadioButton;
RadioButton4: TRadioButton;
SpinEdit1: TSpinEdit;
SpinEdit2: TSpinEdit;
StaticText1: TStaticText;
StaticText2: TStaticText;
DrawGrid1: TDrawGrid;
ImageList1: TImageList;
Timer1: TTimer;
Button4: TButton;
Button5: TButton;
StringGrid1: TStringGrid;
procedure Button3Click(Sender: TObject);
procedure SpinEdit1Change(Sender: TObject);
procedure SpinEdit2Change(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure DrawGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
procedure Timer1Timer(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
m, n, sx, sy, fx, fy: integer; //размер лабиринта, координаты в массиве лабиринта и старта и финиша
a,b: array[0..20, 0..20] of integer; //массивы лабиринта а и b
go: boolean;
done: boolean;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var c,r: integer;
begin
SpinEdit1.Text:='10'; // начальный размер
SpinEdit2.Text:='10'; // лабиринта
sx:=0;sy:=0;
fx:=strtoint(SpinEdit1.Text)-1;
fy:=strtoint(SpinEdit2.Text)-1;
RadioButton1.Checked:=true;
a[fx,fy]:=1;
a[sx,sy]:=2;
for c:=0 to m do
for r:=0 to n do b[c,r]:=0;
end;
procedure TForm1.SpinEdit1Change(Sender: TObject); //изменение ширины
var c,r: integer;
begin
DrawGrid1.ColCount:=strtoint(SpinEdit1.Text);
m:=strtoint(SpinEdit1.Text);
DrawGrid1.Canvas.Rectangle(-1,-1,DrawGrid1.Width-1,DrawGrid1.Height-1);
DrawGrid1.Repaint;
sx:=0;sy:=0;fx:=m-1;fy:=n-1;
for c:=0 to m do
for r:=0 to n do begin a[c,r]:=0; b[c,r]:=0; end;
a[sx,sy]:=2;
fx:=m-1;
a[fx,fy]:=1;
end;
procedure TForm1.SpinEdit2Change(Sender: TObject); //изменение высоты
var c,r: integer;
begin
DrawGrid1.RowCount:=strtoint(SpinEdit2.Text);
n:=strtoint(SpinEdit2.Text);
DrawGrid1.Canvas.Rectangle(-1,-1,DrawGrid1.Width-1,DrawGrid1.Height-1);
DrawGrid1.Repaint;
sx:=0;sy:=0;fx:=m-1;fy:=n-1;
for c:=0 to m+1 do
for r:=0 to n+1 do begin a[c,r]:=0; b[c,r]:=0; end;
a[fx,fy]:=1;a[sx,sy]:=2;
a[fx,fy]:=0;
fy:=n-1;
a[fx,fy]:=1;
end;
procedure TForm1.DrawGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean); //построение лабиринта
begin
if (ACol=SX) and (ARow=SY)
then exit;
if (ACol=FX) and (ARow=FY)
then exit;
if radiobutton3.Checked=true then //препятствие
begin
a[acol,arow]:=-1;
DrawGrid1.Canvas.Rectangle(acol*35-1,arow*35-1,acol*35+35,arow*35+35);
ImageList1.Draw(DrawGrid1.Canvas,ACol*35+2,ARow*35+2,0);
end;
if radiobutton1.Checked=true then //старт
begin
a[sx,sy]:=0;
a[acol,arow]:=2;
DrawGrid1.Canvas.Rectangle(sx*35-1,sy*35-1,sx*35+35,sy*35+35);
sx:=acol; sy:=arow;
DrawGrid1.Canvas.Rectangle(acol*35-1,arow*35-1,acol*35+35,arow*35+35);
ImageList1.Draw(DrawGrid1.Canvas,ACol*35+2,ARow*35+2,2);
end;
if radiobutton2.Checked=true then //финиш
begin
a[fx,fy]:=0;
a[acol,arow]:=1;
DrawGrid1.Canvas.Rectangle(fx*35-1,fy*35-1,fx*35+35,fy*35+35);
fx:=acol; fy:=arow;
DrawGrid1.Canvas.Rectangle(acol*35-1,arow*35-1,acol*35+35,arow*35+35);
ImageList1.Draw(DrawGrid1.Canvas,ACol*35+2,ARow*35+2,1);
end;
if radiobutton4.Checked=true then //удалить препятствие
begin
a[ACol, ARow]:=0;
DrawGrid1.Canvas.Rectangle(acol*35-1,arow*35-1,acol*35+35,arow*35+35);
end;
DrawGrid1.Repaint;
end;
procedure TForm1.Button2Click(Sender: TObject); //очистка лабиринта полностью
var c,r: integer;
begin
DrawGrid1.Canvas.Rectangle(-1,-1,DrawGrid1.Width-1,DrawGrid1.Height-1);
DrawGrid1.Repaint;
sx:=0;sy:=0;fx:=m-1;fy:=n-1;
for c:=0 to m do
for r:=0 to n do begin a[c,r]:=0; b[c,r]:=0; end;
a[fx,fy]:=1;a[sx,sy]:=2;
end;
procedure TForm1.Button1Click(Sender: TObject); //поиск пути
var
count, k, i, j,c,r: integer;
begin
count:=0;
i:=sx; j:=sy;
a[i,j]:=0; b[i,j]:=1;
for k:=0 to 200 do
begin
if ((a[i+1,j]=0)or(a[i+1,j]=1)) and (b[i+1,j]=0) then
begin
inc(i); if a[i,j]=1 then //идем вправо
begin
showmessage('Путь найден!');
//for c:=0 to m do
//for r:=0 to n do begin b[c,r]:=0; end;
exit;
end;
b[i,j]:=1; inc(count);
DrawGrid1.Canvas.textout(i*35+5,j*35+5,inttostr(count));
end;
if ((a[i-1,j]=0)or(a[i-1,j]=1)) and (b[i-1,j]=0) then
begin
dec(i); if a[i,j]=1 then //идем влево
begin
showmessage('Путь найден!');
//for c:=0 to m do
//for r:=0 to n do begin b[c,r]:=0; end;
exit;
end;
b[i,j]:=1;inc(count);
DrawGrid1.Canvas.textout(i*35+5,j*35+5,inttostr(count));
end;
if ((a[i,j+1]=0)or(a[i,j+1]=1)) and (b[i,j+1]=0) then
begin
inc(j); if a[i,j]=1 then //идем вверх
begin
showmessage('Путь найден!');
//for c:=0 to m do
//for r:=0 to n do begin b[c,r]:=0; end;
exit;
end;
b[i,j]:=1;inc(count);
DrawGrid1.Canvas.textout(i*35+5,j*35+5,inttostr(count));
end;
if ((a[i,j-1]=0)or(a[i,j-1]=1)) and (b[i,j-1]=0) then
begin
dec(j); if a[i,j]=1 then //идем вниз
begin
showmessage('Путь найден!');
//for c:=0 to m do
//for r:=0 to n do begin b[c,r]:=0; end;
exit;
end;
b[i,j]:=1;inc(count);
DrawGrid1.Canvas.textout(i*35+5,j*35+5,inttostr(count));
end;
if ((a[i+1,j]=-1)or(b[i+1,j]=1)) // условие невозможности поиска выхода
and((a[i-1,j]=-1)or(b[i-1,j]=1))
and((a[i,j+1]=-1)or(b[i,j+1]=1))
and((a[i,j-1]=-1)or(b[i,j-1]=1)) then
begin
showmessage('Пути не существует');
a[sx,sy]:=2;
exit;
end;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject); //прорисовка компонентов
var c,r:integer;
begin
ImageList1.Draw(DrawGrid1.Canvas,sx*35+2,sy*35+2,2);
ImageList1.Draw(DrawGrid1.Canvas,fx*35+2,fy*35+2,1);
for c:=0 to m do
for r:=0 to n do
begin
if a[c,r]=-1 then ImageList1.Draw(DrawGrid1.Canvas,c*35+2,r*35+2,0);
end;
end;
procedure TForm1.Button3Click(Sender: TObject); //выход из игры
begin
Close;
end;
procedure TForm1.Button4Click(Sender: TObject); //отображение массива а
var c,r: integer;
begin
for c:=0 to m do
for r:=0 to n do
begin
stringgrid1.Colcount:=m;
stringgrid1.rowcount:=n;
stringgrid1.Cells[c,r]:= inttostr(a[c,r]);
end;
end;
procedure TForm1.Button5Click(Sender: TObject); //отображение массива b
var c,r: integer;
begin
for c:=0 to m do
for r:=0 to n do
begin
stringgrid1.Colcount:=m;
stringgrid1.rowcount:=n;
stringgrid1.Cells[c,r]:= inttostr(b[c,r]);
end;
end;
end.