unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;
  const n=8;
  const m=8;
  const l=3;
 type Tstopka=record
     KolCif: integer; //кол-во бусинок в стопке
     Col: array [1..n] of integer;  //цвет каждой бусинки в стопке
     end;
  type Tkletka=record col:boolean; //цвет
                count:integer; //количество инверсий
                end;
var
       Stopka: array [1..m] of Tstopka  ;  //кол-во стопок
         mouse: integer=0;   //сточка для поочередности ходов
        GameOver: boolean=false;
        d: integer;
        Stakan:TBitMap;
        Ball_blue:TBitMap;
        Ball_pink:TBitMap;
        win:integer=0 ;
            win2:integer=0 ;
type
  TForm1 = class(TForm)
    Image1: TImage;
    Button1: TButton;
    Image2: TImage;
    Image3: TImage;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    procedure moveBall(ist, ifin: integer);
    procedure Button1Click(Sender: TObject);
    procedure ShowMass;
    procedure RisKletka(i,j:integer);
     procedure RisKletka2(i,j:integer);
    procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormCreate(Sender: TObject);
    Function HowMuchMove(Color,s,f:integer): integer;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
procedure TForm1.RisKletka(i,j:integer);
var x1, x2, y1, y2,x,y,x3:integer;
begin

      x1:=d*(j-1);
      x2:=x1+d;
      y1:=d*(i-1);
      y2:=y1+d;



end;
  procedure TForm1.RisKletka2(i,j:integer);
var x1, x2, y1, y2,x,y,x3:integer;
begin
      x1:=d*(j-1);
      x2:=x1+d;
      y1:=d*(i-1);
      y2:=y1+d;
end;
    procedure TForm1.moveBall(ist, ifin:integer);
       var x1 ,y1, x2, y2, x, y,z:integer;
       begin
          Image1.Canvas.Pen.Style:=psClear;
          Stopka[ifin].KolCif:=Stopka[ifin].KolCif+1 ;
          Stopka[ifin].Col[Stopka[ifin].KolCif]:=Stopka[ist].Col[ Stopka[ist].KolCif] ;
          Stopka[ist].Col[Stopka[ist].KolCif]:=0;
          Stopka[ist].KolCif:=Stopka[ist].KolCif-1;
          z:=d*(n-Stopka[ifin].KolCif);
          x1:= d*(ifin-1) ;
          y1:= z ;
          x2:= x1+d ;
          y2:= y1+d;
          If Stopka[ifin].Col[Stopka[ifin].KolCif]=1
          then
           Image1.Canvas.Draw(x1+ d div 2-Ball_pink.Width div 2,y1+ d div 2-Ball_pink.Height div 2,Ball_pink)
           else
            Image1.Canvas.Draw(x2- d div 2-Ball_pink.Width div 2,y2- d div 2-Ball_pink.Height div 2,Ball_blue);
          z:=d*(n-Stopka[ist].KolCif-1);
          x1:= d*(ist-1) ;
          y1:= z ;
          x2:= x1+d ;
          y2:= y1+d;
           Image1.Canvas.Brush.Color:=clwhite ;
           image1.Canvas.Rectangle(x1,y1,x2,y2);



        end;

procedure InitMass;
var i, j: integer;
begin

     for i:=1 to m do
      begin
      Stopka[i].KolCif:=0;      //сделать поле пустым
      for j:=1 to n do
      Stopka[i].Col[j]:=0;
      end;
      Stopka[1].KolCif:=l;    //задать кол-во бусинок в начале
      Stopka[m].KolCif:=l;
      for j:=1 to   Stopka[1].KolCif do
          Stopka[1].Col[j]:=1;
      for j:=1 to   Stopka[m].KolCif do
          Stopka[m].Col[j]:=2;





   end;
procedure TForm1.ShowMass;
   var i,j,x1, x2, y1, y2, x:integer;

   begin
    Label1.Visible:=false;
    Label2.Visible:=false;
    Label3.Visible:=false;
    Image3.Picture.LoadFromFile('pic/Банально.bmp');
    d:=image1.Width div n;
    // image1.Width:=d*n;
  //   image1.Height:=d*m;
     Image1.Canvas.Brush.Style:=bsSolid;
     Image1.Canvas.Rectangle(0,0,d*n,d*m);
     Image1.Transparent:=true;
   //  Image1.Canvas.Brush.Color:=clWhite;
   for i:=1 to n do
     for j:=1 to m do
       begin
       Stopka[1].KolCif:=l;
       Stopka[m].KolCif:=l;
       x1:=d*(j-1);
       x2:=x1+d;
       y1:=d*(i-1);
       y2:=y1+d;
    //  image1.Canvas.Rectangle(x1,y1,x2,y2);   //закраска изначальная
       end;

       y2:=d*n;
        for x:=1 to Stopka[1].KolCif do
        begin
          ;
       x1:=0 ;
       x2:=x1+d ;
       y1:=y2 ;
       y2:=y1-d ;
         Ball_pink.Transparent;


         Image1.Canvas.Draw(x1+ d div 2-Ball_pink.Width div 2,y1- d div 2-Ball_pink.Height div 2,Ball_pink)  //закраска еще одна
         end;

           y2:=d*n;
        for x:=1 to Stopka[1].KolCif do
        begin
         ;
       x1:=d*n ;
       x2:=x1-d ;
       y1:=y2 ;
       y2:=y1-d ;
         Ball_blue.Transparent;
         Image1.Canvas.Draw(x2+ d div 2-Ball_pink.Width div 2,y2+ d div 2-Ball_pink.Height div 2,Ball_blue)

         end;

       mouse:=mouse+1;
    end;
     procedure TForm1.Button1Click(Sender: TObject); //   старт
      begin
      mouse:=0;
      win:=0;
      win2:=0;
      //задать начальное значение массива
      InitMass;
      form1.Caption:='';
      image1.Canvas.Pen.Style:=psClear;
      image1.Canvas.Rectangle(0,0, image1.Width, image1.Height);
       GameOver:=false;
       image2.Picture.LoadFromFile('pic/ВВ.bmp');
      //отобразить массив
      ShowMass;

      end;
function TForm1.HowMuchMove(Color,s,f:integer): integer;
var i: integer;
begin
   result:=0;
   for i :=s to f do
        with  stopka[i] do
               If (Col[KolCif]=Color) then  result:=result+1;
end;
procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
    var x1, x2, y1, y2, i,j,t, r, p,q, o, h, h1,q1, q2, h2:integer;
begin
i:=x div d+1;
  o:=0;
 q1:=0;
 h1:=0;
 q2:=0;
 h2:=0;
  h:=0;
     with  Stopka[i]  do
  begin
  If ((i=m) and (Col[KolCif]=1)) or ((i=1) and (Col[KolCif]=2))
   then {ход не делается }
    else

If not GameOver  then
begin
    t:=HowMuchMove(1,1,m-1);
    r:=HowMuchMove(2,2,m);

     If KolCif>0 then
       begin
       If ((t=0) and (odd(mouse)))  or ((r=0) and (not odd(mouse)))
             then
             mouse:=mouse+1;
       if (Col[KolCif]=1) and  (odd(mouse))
             then
               begin
                     image2.Picture.LoadFromFile('pic/ФФ.bmp');
               mouse:=mouse+1;
               moveBall (i, i+1)

               end
             else
             If (Col[KolCif]=2) and (not odd(mouse))then
                              begin
               image2.Picture.LoadFromFile('pic/ВВ.bmp');
                              mouse:=mouse+1;
                              moveBall (i, i-1);

                              end
      end;
  end;

  for p:= 1 to n do       //цикл условия победы красных или ничьей
     if stopka[m].Col[p]=1 then  //посмотрим на последнюю строчку
      o:=o+1;
  for q:= 1 to n do
      if stopka[1].Col[q]=2 then
        h:=h+1;

  if (o=l) and (win=0) then win:=mouse; //если на последней строчке kolCif красных
  if (h=l) and (win2=0) then win2:=mouse;
  if (win2-win)=1 then
         begin
          Label3.Visible:=true;
          form1.Caption:= 'DRAW'               ;
          GameOver:=true;
          end
          else
          begin
             if  (win2>win)  then
               begin
                Label2.Visible:=true;
                form1.Caption:='BLUE_WIN'  ;
                  GameOver:=true;
                      end;
   if  (win<>0) and (mouse-win>0) then
             begin
           Label1.Visible:=true;
           form1.Caption:='RED_WIN' ;
              GameOver:=true;
             end;
             end;
   for q:= 1 to n do
      if stopka[m].Col[q]=2 then
        h1:=h1+1
        else
        If stopka[m].Col[q]=1 then
        q1:=q1+1;
    If (q1=l) and (h1>0) then
        begin
           Label1.Visible:=true;
           form1.Caption:='RED_WIN' ;
              GameOver:=true;
             end;
    for q:= 1 to n do
      if stopka[1].Col[q]=1 then
         q2:=q2+1
         else
         If stopka[1].Col[q]=2 then
         h2:=h2+1;
     If (h1>0) and (q2>0) and (h2>0) and (q1>0)  then
          begin
          Label3.Visible:=true;
          form1.Caption:= 'DRAW'               ;
          GameOver:=true;
          end

       end;

    t:=HowMuchMove(1,1,m-1);
    r:=HowMuchMove(2,2,m);
    If (r=0) then
                   image2.Picture.LoadFromFile('pic/ВВ.bmp') ;


     If (t=0)
                              then
                              image2.Picture.LoadFromFile('pic/ФФ.bmp') ;





  end;



procedure TForm1.FormCreate(Sender: TObject);
begin
 Stakan:=TBitMap.Create;
 Stakan.LoadFromFile('pic/Банально.bmp');
 Ball_blue:=TBitMap.Create;
   Ball_blue.LoadFromFile('pic/ФФ.bmp');
   Ball_pink:=TBitMap.Create;
   Ball_pink.LoadFromFile('pic/ВВ.bmp') ;
end;

end.




