unit Unit1;

interface

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

  type Tkletka=record col:integer;
            count: integer;
            end;
  TPole= array [1..100, 1..100] of TKletka;
  var Pole: TPole; var n: integer;
     i1,j1,i2,j2,k: integer;
     w,r,t,e,y,u,l,m,v,o: integer;   chet: integer=0;
        MapChange,hod: boolean;
        Pc, Player: TBitMap;
type
  TForm1 = class(TForm)
    Image1: TImage;
    Label1: TLabel;
    Image2: TImage;
    ProgressBar1: TProgressBar;
    Label2: TLabel;
    RadioGroup1: TRadioGroup;
    Label3: TLabel;
    RadioGroup2: TRadioGroup;
    Label4: TLabel;
    procedure ShowMass;
    procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    function HowMuch(color: integer;i1,j1,i2,j2: integer):integer;
    procedure FormCreate(Sender: TObject);
    procedure Intelegence;
    procedure RadioGroup1Click(Sender: TObject);
       Procedure changePole( a,b,c,d: integer);
    procedure RadioGroup2Click(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;


var
  Form1: TForm1;

implementation

function TForm1.HowMuch(color: integer;i1,j1,i2,j2: integer):integer;
var i,j,c :integer;
begin
result:=0;
 if i1>i2 then
begin
c:=i2;
i2:=i1;
i1:=c;
end;

if j1>j2 then
begin
c:=j2;
j2:=j1;
j1:=c;
end;
 for i:=i1 to i2 do
   for j:=j1 to j2 do
   begin
   if pole[i,j].col= color then result:= result+1;


end;

end;
{$R *.dfm}

procedure InitMass;
var i,j: integer;
 x1,y1,x2,y2:integer; igra: textfile; q: char; e: string;

begin
 e:='1.txt';
 AssignFile(igra, e);
 reset (igra);

 readln(igra, n);

 for i:= 1 to n do
 begin
  for j:= 1 to n do
  begin
     read (igra,q);
     pole[i,j].col:=strtoint(q);
    end;
     readln(igra);
    end;
    closefile(igra);
     end;


Procedure TForm1.ShowMass;
var d,i,j: integer;
    x1,y1,x2,y2:integer;
begin
d:=image1.width div n;
 for i:=1 to n do
  for j:=1 to n do
   begin
   //ShowMessage(' ');
   Form1.Caption:= ('Ñàøêà');
   x1:=d*(j-1);
   x2:=x1+d;
   y1:=d*(i-1);
   y2:=y1+d;
   if pole [i,j].col=1  then Image1.Canvas.Draw(x1,y1,Player);
   if pole[i,j].col=0 then  Image1.Canvas.Draw(x1,y1,PC);
   Image2.Visible:=true;
   // Image1.Canvas.Rectangle (x1,y1,x2,y2);
    end;
    end;



procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
  var d: integer;
begin
 d:= image1.width div n;
 i1:= y div d+1;
 j1:= x div d+1;
ShowMass;
k:=HowMuch (1,1,1,n,n);
 if (k=0) then Label2.Visible:= true;
 If Image2.Visible=false then IMage2.Visible:=true;
 If Label3.Visible=false then Label3.Visible:=true;
end;


procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
   var c,d,i,j: integer;
begin
 d:= image1.width div n;
 i2:= y div d+1;
 j2:= x div d+1;

if i1>i2 then
 begin
c:=i2;
i2:=i1;
i1:=c;
end;

if j1>j2 then
begin
c:=j2;
j2:=j1;
j1:=c;
end;
If MapChange then
begin
   If hod=False then
   begin
    Image2.Picture.LoadFromFile('Nuka-Cola_display.bmp');
     end;
 if hod=true
  then
  begin
  Image2.Picture.LoadFromFile ('sunset-sarsaparilla.bmp');
  end;
 end;
 //if MapChange=false then Image2.Canvas.Rectangle (0,0,81,81);

 for  i:=i1 to i2 do
 for j:=j1 to j2 do
 if pole[i,j].col=1 then pole[i,j].col:=0 else pole[i,j].col:=1;
 ShowMass;
m:=i1;
u:=i2;
l:=j1;
o:=j2;

k:=HowMuch (1,1,1,n,n);
//showmessage (IntToStr (k));
if (k=25)   then
Label1.Visible:=True;
if MApChange=false then
begin
  Chet:=chet+1;
  Label3.Caption :='Record '+IntToStr (chet);
  Label3.Visible:=TRue;
 end;
Sleep (100);
if ((K<>0) and (k<>25)) and (MapChange=false) then Intelegence;
if (k=0) and (MapChange=false) then Label2.Visible:= true;
If (k=0) and (MapChange=True)  then Label1.Visible:= true;
if (hod and MapChange)
then hod:=False
 else hod:=TRue;

end;


procedure TForm1.FormCreate(Sender: TObject);
var FON:TBitMap;
begin
w:=0;
e:=0;
r:=0;
t:=0;
FON:=TBitMap.Create;
 FON.LoadFromFile('Desert.bmp');
 Form1.Canvas.Draw(0,0,Fon);
 Pc:=TBitMap.Create;
 Player:=TBitMap.Create;
 Pc.LoadFromFile('PC.bmp');
 Image1.Picture.LoadFromFile('Fon1.bmp');
 Player.LoadFromFile('player.bmp');
 Image2.Picture.LoadFromFile('Nuka-Cola_display.bmp');
  hod:=true;
 //Image2.Canvas.Draw('Nuka-Cola_display.bmp');
end;

   Procedure TForm1.changePole( a,b,c,d: integer);
var i,j: integer;
begin
    for  i:=a to c do
 for j:=b to d do

 begin

 if pole[i,j].col=1 then pole[i,j].col:=0 else pole[i,j].col:=1 ;
 end;
 end;



 Procedure TForm1.Intelegence;
var max,min1,i,j, max1,max2,z,a,b,c,d,e,f,t,r,p,q,i1i,i2i,j1i,j2i,h,com: integer;
begin
  max:=-25;
  z:=-25;
  If (Label1.Visible=false) and (label2.Visible=false) and MapChange=false then
  begin
  for i1i:=1 to n do
   for j1i:= 1 to n do
    for i2i:=i1i to n do
     for j2i:=j1i to n do
     begin
      max2:=HowMuch (0,i1i,j1i,i2i,j2i)-HowMuch (1,i1i,j1i,i2i,j2i);
      max1:=HowMuch (1,i1i,j1i,i2i,j2i)-HowMuch(0,i1i,j1i,i2i,j2i);
       if  max1>max then
         begin
         max:=max1;
         a:=i1i;
         b:=j1i;
         c:=i2i;
         d:=j2i;
         end;
     if max2>z then
     begin
        z:=max2;
        e:=i1i;
        f:=j1i;
        t:=i2i;
        r:=j2i;
        p:=HowMuch (1,i1i,j1i,i2i,j2i);
        h:=HowMuch (0,i1i,j1i,i2i,j2i)
      end;
      end;
     k:=Howmuch(1,1,1,n,n);
 Image1.Refresh;
//showmessage('Õîä êîìïüþòåðà');
   for com:=ProgressBar1.Min to ProgressBar1.Max do
          begin
          ProgressBar1.Position:=com;
          sleep(6);

          end;
          ProgressBar1.Position:= ProgressBar1.Min;


     v:= random (Radiogroup2.itemindex+2);

     if (P=0) and (v=1)then   //   ×ÒÎ ÝÒÎ?    !!!
     begin

      if (p=0)and ((q mod 5)=0) and (m<>5) then
        e:=t
        else
           begin
           if (p=0) and (f>1) then
           begin
           f:=f-1;
           r:=f;
           end;
           if (p=0) and (f=1) and (e=1) then
            begin
            e:=t+1;
            t:=e;
            end;

            end;

            changePole( e,f,t,r) ;

       end
       else
       begin

 if ((a=m) and (c=u) and (b=l) and (d=o)) then
     begin
        a:=random(2)+1;
        b:=random (2)+1 ;
        c:=random(3)+2;
        d:=random(3)+2;
     end;

 changePole( a,b,c,d) ;
end;
 k:=HowMuch (1,1,1,n,n);
 if (k=0) then Label2.Visible:= true;
  ShowMass;
end;
end;
procedure TForm1.RadioGroup1Click(Sender: TObject);
 var FON:TBitMap;
begin
FON:=TBitMap.Create;
 FON.LoadFromFile('Desert.bmp');
 Form1.Canvas.Draw(0,0,Fon);
 Image2.Visible:=true;
 If Label1.Visible or Label2.Visible then
   begin
     Label1.Visible:=false;
     Label2.Visible:=true;
     end;
 Image2.Visible:=true;
 InitMass;
 ShowMass;
Image2.Visible:=true;
//Image2.Canvas.clTransparent:=ClWhite;
Label2.Visible:=False;

 if radiogroup1.ItemIndex=1 then
 begin
 Image2.Visible:=true;
 Label3.Visible:=true;
 chet:=0;

//Image2.Canvas.Brush.Color:=clRed;
//Image2.Canvas.Rectangle (0,0,81,81);
MapChange:=true;
end
else
begin
chet:=0;
MapChange:=false;
 //ProgressBar1.Visible:=false;
k:=HowMuch (1,1,1,n,n);
IF (k=0) or (k=25) then
begin
RadioGroup2.Visible:= true;
Label1.Visible:=false;
Label2.Visible:=true;
end;
end;

end;
procedure TForm1.RadioGroup2Click(Sender: TObject);
begin
   if radiogroup2.ItemIndex=0 then
   Image2.Visible:=true;
   InitMass;
   ShowMass;
   chet:=0;
   Label1.Visible:=false;
   Label2.Visible:=false;

   Label3.Caption :='Record '+IntToStr (chet);
  Label3.Visible:=TRue;
   if radiogroup2.ItemIndex=1 then
   Image2.Visible:=true;
   InitMass;
   ShowMass;
   chet:=0;
   Label3.Caption :='Record '+IntToStr (chet);
  Label3.Visible:=TRue;
   Label1.Visible:=false;
   Label2.Visible:=false;
   if radiogroup2.ItemIndex=2 then
   Image2.Visible:=true;
   InitMass;
   ShowMass;
   chet:=0;
   image2.Visible:=true;
   Label3.Caption :='Record '+IntToStr (chet);
  Label3.Visible:=TRue;
Label1.Visible:=false;
   Label2.Visible:=false;
   end;

end.


