unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;
  const n=12;
type
  TKletka = record Zan:integer;// кем занята клетка 0 1 2
                  x,y,r:integer;

  end;
  type
 TForm1=class(TForm)
    Button1: TButton;
    Image1: TImage;
    Label1: TLabel;
    Image2: TImage;
    Image3: TImage;
    Button2: TButton;
    procedure ShowMass;
    procedure Button1Click(Sender: TObject);
    procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  CanGo :array [1..12,1..12] of boolean=
  ((false,false,false,false,false,true,false,true,false,false,false,false),
  ( false,false,false,false,false,false,true,false,true,false,false,false),
  ( false,false,false,true,false,false,false,true,false,false,false,false),
  ( false,false,true,false,false,false,false,false,true,false,true,false),
  ( false,false,false,false,false,false,false,false,false,true,false,true),
  ( true,false,false,false,false,false,true,false,false,false,true,false),
  ( false,true,false,false,false,true,false,false,false,false,false,true),
  ( true,false,true,false,false,false,false,false,false,false,false,false),
  ( false,true,false,true,false,false,false,false,false,true,false,false),
  ( false,false,false,false,true,false,false,false,true,false,false,false),
  ( false,false,false,true,false,true,false,false,false,false,false,false),
  ( false,false,false,false,true,false,true,false,false,false,false,false));

     Kletka:array[1..12] of TKletka;
    r:integer=40;
    mouse1:boolean=false;
    start:integer=0;
    finish:integer;
    wolf,rabbit:TBitMap;
    hod:integer=0;
    RStart:integer;

implementation

{$R *.dfm}
procedure InitMass;
begin
Kletka[1].x:=162 ;
Kletka[1].y:=64  ;
Kletka[1].zan:=1;
Kletka[2].x:=422  ;
Kletka[2].y:=64    ;
Kletka[2].zan:=1;
Kletka[3].x:= 670 ;
Kletka[3].y:=64     ;
Kletka[3].zan:=1;
Kletka[4].x:=162      ;
Kletka[4].y:=264    ;
Kletka[4].zan:=0 ;
Kletka[5].x:=422   ;
Kletka[5].y:=264    ;
Kletka[5].zan:=0 ;
Kletka[6].x:=670     ;
Kletka[6].y:=264      ;
Kletka[6].zan:=0 ;
Kletka[7].x:=162       ;
Kletka[7].y:=480       ;
Kletka[7].zan:=0;
Kletka[8].x:=422    ;
Kletka[8].y:=480     ;
Kletka[8].zan:=0 ;
Kletka[9].x:=670      ;
Kletka[9].y:=480       ;
Kletka[9].zan:=0  ;
Kletka[10].x:=162        ;
Kletka[10].y:=649     ;
Kletka[10].zan:=2 ;
Kletka[11].x:=422     ;
Kletka[11].y:=649      ;
Kletka[11].zan:=2 ;
Kletka[12].x:=670     ;
Kletka[12].y:=649     ;
Kletka[12].zan:=2 ;
end;

procedure TForm1.ShowMass;
var i ,j  : integer ;
begin


Image1.Canvas.Pen.Color:=clred;
//линии соединения
for i:=1 to 12 do
for j:=1 to 12 do
 begin
   if CanGo[i,j] then
   begin
   Image1.Canvas.MoveTo(kletka[i].x,kletka[i].y);
  Image1.Canvas.LineTo(kletka[j].x,kletka[j].y);
   end;
 end;
//рисовать все кружки
image1.Canvas.Brush.Color:=clblack;
 for i:=1 to 12 do
 Image1.Canvas.Ellipse(kletka [i].x-r,kletka[i].y-r,kletka[i].x+r, kletka[i].y+r);


image1.Canvas.Brush.Color:=clblack;
for i:=1 to 3 do
Image1.Canvas.Draw(kletka [i].x-r,kletka[i].y-r,wolf);

 image1.Canvas.Brush.Color:=clblue;
 for i:=10 to 12 do
 Image1.Canvas.Draw(kletka [i].x-r,kletka[i].y-r,rabbit);
end;


procedure TForm1.Button1Click(Sender: TObject);
//var wolf:TBitMap;
begin
InitMass;
image1.Picture.LoadFromFile('осень3.bmp');
Wolf:=TBitMap.Create;
wolf.LoadFromFile('волчара.bmp');
wolf.Transparent:=true;

rabbit:=TBitMap.Create;
rabbit.LoadFromFile('овца.bmp');
rabbit.Transparent:=true;
ShowMass;
hod:=0;
end;


procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
     var i,q:integer;   a,b:real;
begin

finish:=0;
for   i:=1 to 12 do
   begin
     a:=kletka[i].x-x ;
     b:=kletka[i].y-y ;

    if sqrt(sqr(a)+sqr(b))< r   then

               if kletka[i].zan >0 then
               begin

               start:=i;
               end
                else finish:=i;
    end;
if (start>0) and (finish>0) and CanGo[start,finish] then

    begin
    // ShowMessage(inttostr(kletka[start].Zan)+' '+inttostr(kletka[finish].Zan));
      Button2.Enabled:=true ;
      q:=kletka[finish].Zan;
      kletka[finish].Zan:=kletka[start].Zan;
      kletka[Start].Zan:=q ;
      Image1.canvas.Brush.Color:=clblack;
      Image1.Canvas.Ellipse(kletka [start].x-r,kletka[start].y-r,kletka [start].x+r,kletka[start].y+r) ;
      if kletka[finish].Zan=1 then
            Image1.Canvas.Draw(kletka [finish].x-r,kletka[finish].y-r,wolf)
            else  image1.canvas.Draw(kletka [finish].x-r,kletka[finish].y-r,rabbit) ;

         RStart:=start;
      start:=0;
      hod:=hod+1;
     label1.Caption:=intToStr(hod);
    end;
end;



procedure TForm1.Button2Click(Sender: TObject);
var q: integer;
begin

      q:=kletka[Rstart].Zan;
      kletka[Rstart].Zan:=kletka[finish].Zan;
      kletka[finish].Zan:=q ;
      Image1.canvas.Brush.Color:=clblack;
      Image1.Canvas.Ellipse(kletka [finish].x-r,kletka[Finish].y-r,kletka [finish].x+r,kletka[finish].y+r) ;
      if kletka[Rstart].Zan=1 then
            Image1.Canvas.Draw(kletka [Rstart].x-r,kletka[Rstart].y-r,wolf)
            else  image1.canvas.Draw(kletka [Rstart].x-r,kletka[Rstart].y-r,rabbit) ;

       Button2.Enabled:=false ;
  hod:=hod-1;
     label1.Caption:=intToStr(hod);
end;

end.
