unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, jpeg, ExtCtrls, StdCtrls;
const pG=20;
type
  TForm1 = class(TForm)
    V2: TImage;
    V3: TImage;
    Alm: TImage;
    V1: TImage;
    Shape1: TShape;
    Fonar: TImage;
    Fon: TImage;
    Shape2: TShape;
    Button1: TButton;
    Timer1: TTimer;
    congr: TImage;
    I1: TImage;
    I2: TImage;

    procedure InitMass(level:integer);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure TakeGir(Sender:TObject);
   
    procedure V1MouseDown(Sender: TObject; Button: TMouseButton;      Shift: TShiftState; X, Y: Integer);
    procedure CreateGir;//создать все гири
    procedure luch;// луч
    procedure changeV;// смена весов
    procedure putgir(num,ncup: integer);//положить нирю с номером num на чашку ncup
    procedure obncup ;//обнулить чашки
    procedure ImageMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure Timer1Timer(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;
  type Tgirya= record
  massa: integer;
  whichcup: integer;
  uroven:integer
  end;
  TCup=record        // чашка
					Count: integer; //кол-во гирек на чашке
					State: integer; //высота чашки (-1, 0, 1)
					SWeight: integer; //сумма гирек на чашке
          Gir: array [1..pG] of integer;
          Xgir: integer;
       end;
   TMAsslabel=array [1..6] of TLAbel;
   TMassBitMap=array [1..pG] of TImage;
   var
    Form1: Tform1;
    Cup : array [1..6] of Tcup;
    level: integer=0;
    Lab: TMAssLabel;
    GirCount:integer;
    Allgir: array [1..20] of Tgirya;
    Image:TMassBitMap;
    take:boolean;     //есть ли взятая
    numTake:integer; //которая взята
implementation

{$R *.dfm}



 procedure tform1.InitMass(level:integer); //читаем из файла
var fIn: textfile;
    w,i,p:integer;
begin
for i:=1 to pg do begin
                  image[i].Visible:=false;
                  end;
for i:= 1 to 6 do  with  cup[i] do
    begin
     Count:= 0;
     Gir[count] := 0;
     Sweight:= 0;
    end;
 //showmessage(IntToStr(level)+'уровень.txt');
  assignFile(fIn,IntToStr(level)+'уровень.txt');
  reset(Fin);
  randomize ;
  readln(fIn,GirCount);
  for i:= 1 to GirCount do
  begin
   read (Fin,w);// вес одной гирьки
   allgir[i].massa:=w;
   putgir(i,random(6)+1);
   p:=random(6)+1;
  image[i].picture.LoadFromFile('картинки/'+inttostr(p)+'.bmp');
  image[i].Transparent:=true;
  image[i].Visible:=false;
 Image[i].OnClick:=takeGir;
   image[i].Visible:=true;
   image[i].Tag:=i;
   image[i].Canvas.Font.Size:=26;
   image[i].Canvas.pen.Style:= psClear;
  image[i].Canvas.font.color:=clwhite;
   image[i].Canvas.Brush.Style:= bsclear;
   image[i].Canvas.Font.Name:='arial';
   image[i].Canvas.TextOut( image[i].Width div 2+19, image[i].Height div 2 ,inttostr(allgir[i].massa));
   end;
   closeFile(fIn);

   end;


procedure TForm1.obncup;
var i,j,t :integer;
begin  // обнулить все чашки
 for i:= 1 to 6 do
  begin
    with  cup[i] do
      begin
       Count:= 0;
       Sweight:= 0;
      end;
  end;
  end;

  procedure TForm1.changeV;
  var i,j,nc,ur, ngi: integer;
  begin
  //весы смена 1,2
  if  cup[1].SWeight < cup[2].SWeight
  then  begin V1.Picture.LoadFromFile('картинки/ВЕСЫПРАВ.bmp');
        cup[1].State:= 1;
        cup[2].State:= -1;
        end
      else if cup[1].SWeight > cup[2].SWeight
           then begin
           V1.Picture.LoadFromFile('картинки/ВЕСЫЛЕ2В.bmp');
                cup[1].State:=-1;
                cup[2].State:=1;
                end
                else  begin
                V1.Picture.LoadFromFile('картинки/ВЕСЫы.bmp') ;
                cup[1].State:= 0 ;
                cup[2].State:= 0 ;
                end;
  //весы смена 3,4
  if  cup[3].SWeight < cup[4].SWeight
  then begin
  V2.Picture.LoadFromFile('картинки/ВЕСЫПРАВ.bmp');
   cup[3].State:=1;
   cup[4].State:= -1;
   end
   else if cup[3].SWeight > cup[4].SWeight
          then begin
          V2.Picture.LoadFromFile('картинки/ВЕСЫЛЕ2В.bmp');
           cup[3].State:= -1 ;
           cup[4].State:= 1;
           end
            else begin
            V2.Picture.LoadFromFile('картинки/ВЕСЫы.bmp') ;
            cup[3].State:=0 ;
            cup[4].State:= 0 ;
            end;
 //весы смена 5,6
  if  cup[5].SWeight < cup[6].SWeight
  then begin
  V3.Picture.LoadFromFile('картинки/ВЕСЫПРАВ.bmp');
   cup[5].State:=1;
   cup[6].State:=-1 ;
   end
   else if cup[5].SWeight > cup[6].SWeight
             then begin
             V3.Picture.LoadFromFile('картинки/ВЕСЫЛЕ2В.bmp');
                  cup[5].State:= -1 ;
                  cup[6].State:=1;
                  end
                  else
                  begin
                  V3.Picture.LoadFromFile('картинки/ВЕСЫы.bmp') ;
                   cup[5].State:=0 ;
                   cup[6].State:=0 ;
                    end;

  for i:=1 to 6 do
      with cup[i] do

      for j:= 1 to Count do
               begin
               ngi:=gir[j];
               image[ngi].BringToFront;
               allgir[ngi].uroven:=j;
               case State of
                   -1: image[ngi].Top:=514-round(j*image[ngi].Height *0.50);
                    0: image[ngi].Top:=474-round(j*image[ngi].Height* 0.50);
                    1: image[ngi].Top:=445-round(j*image[ngi].Height * 0.50);
               end;
              end;

   if take then Image[numTake].BringToFront;

   end;

 procedure TForm1.putgir(num,ncup: integer);//положить нирю с номером num на чашку ncup;
 var t,i,j,h : integer;
 begin

 cup[ncup].Count := cup[ncup].Count+1;
 AllGir[num].uroven:=cup[ncup].Count;
 AllGir[num].whichcup:=ncup;
 cup[ncup].Gir[cup[ncup].Count]:=num;
 Image[num].Left:=cup[ncup].Xgir;
 cup[ncup].SWeight:= cup[ncup].SWeight + AllGir[num].massa;
 changeV;


 {t:=1;
 for i:= 1 to 6 do
 for j:= 1 to cup[i].Count do
      begin
      image[t].Left:=cup[i].Xgir-18;
      if not odd(i) then image[t].Left:=cup[i].Xgir-30 ;
      if odd(i) then image[t].Left:=cup[i].Xgir-10 ;
      if cup[i].State=1 then  image[t].top:=473-j* (image[t].Height div 2)- cup[i].State*29  else
      image[t].top:=473-j* (image[t].Height div 2)- cup[i].State*40 ;
      image[t].Visible:= true;
      t:=t+1;
      end;

      for j:= 1 to gircount do
     begin
       inc(cup[allgir[j].whichcup].SWeight, allgir[j].massa);
       inc(cup[allgir[j].whichcup].Count);//увеличить на 1

     end; }
    for i:= 1 to 6 do
     lab[i].Caption:= inttostr(cup[i].sweight);

                           shape1.Visible:=true;
                           shape2.Visible:=true;
                          if (cup[1].SWeight = cup[2].SWeight)  and (cup[3].SWeight = cup[4].SWeight) and (cup[5].SWeight = cup[6].SWeight)  then
                           begin
                           button1.Caption:='Next';
                           button1.Visible:=true;
                           Alm.Picture.LoadFromFile('картинки/КРИСТАЛЛ2.bmp');

       if level=3 then begin
                            congr.BringToFront;
                            for i := 1 to gircount do image[i].Visible:=false;
                            congr.Picture.LoadFromFile('картинки/images.bmp');
                            button1.Visible:=false;

                            end;
                            end;
      end;


   procedure TForm1.luch;
    begin
    if cup[6].State= -1 then begin
                             Shape1.Left:=960;
                             shape1.Width:=121;
                             Alm.Picture.LoadFromFile('картинки/КРИСТАЛЛ.bmp');
                             end
   else
   if cup[5].State= -1 then begin
                            Shape1.Left:=800;
                            shape1.Width:= 281;
                            Alm.Picture.LoadFromFile('картинки/КРИСТАЛЛ.bmp');
                             end
   else
   if cup[4].State= -1 then begin
                            Shape1.Left:=688;
                            shape1.Width:= 393;
                            Alm.Picture.LoadFromFile('картинки/КРИСТАЛЛ.bmp');
                            end
   else
   if cup[3].State= -1 then begin
                            Shape1.Left:=528;
                            shape1.Width:= 553;
                            Alm.Picture.LoadFromFile('картинки/КРИСТАЛЛ.bmp');
                            end
       else
   if cup[2].State= -1 then begin
                            Shape1.Left:=408;
                            shape1.Width:= 673;
                            Alm.Picture.LoadFromFile('картинки/КРИСТАЛЛ.bmp');
                            end
  else
  if cup[1].State= -1 then begin
                           Shape1.Left:=256;
                           shape1.Width:= 828;
                          Alm.Picture.LoadFromFile('картинки/КРИСТАЛЛ.bmp');
                           end
  else
  if cup[1].State= 0 then begin
                           Shape1.Left:=136;
                           shape1.Width:= 950;


                           end;

  end;

procedure TForm1.FormCreate(Sender: TObject);
var i,j,nc,p: integer;
begin
{form1.WindowState:=wsMaximized;
form1.BorderStyle:=bsNone;
form1.Width:=screen.Width;
form1.Height:=screen.Height; }
 form1.DoubleBuffered:=true;
 cup[1].Xgir:=200;
 cup[2].Xgir:=339;
 cup[3].Xgir:=472;
 cup[4].Xgir:=614;
 cup[5].Xgir:=747;
 cup[6].Xgir:=888;
   //создание верхних лейблов
  for i:= 1 to 6 do
  begin
  lab[i]:= TLabel.Create(Form1);
  lab[i].Parent:=Form1;
  lab[i].AutoSize:=false;
  lab[i].Alignment:=tacenter;
  lab[i].Left:=100+i*137;
  lab[i].Top:=260;
  lab[i].Width:=30;
  lab[i].Height:=25;
  lab[i].color:=clblack;
  lab[i].Font.Color:=clwhite;

 lab[i].Font.Size:= 15;
  end;
  creategir;
  end;

  procedure TForm1.creategir ; //создание гирек
  var i,p: integer;
  begin
  for i:= 1 to pg do
  begin
  image[i]:= TImage.Create(Form1);
  image[i].Parent:=Form1;
  image[i].Width:=90;
  image[i].Height:=50;
  image[i].Proportional:=true;
  image[i].OnMouseMove:=ImageMouseMove;
  end;
  end;




 procedure TForm1.Takegir(Sender: TObject);
var i,ncup,ur, ngi: integer;
begin

  shape1.Visible:=false;
  shape2.Visible:=false;
 button1.Visible:=false;
 alm.Picture.LoadFromFile('картинки/КРИСТАЛЛ.bmp');
 //label1.Caption:=inttostr(n);
  take:=not take;
  with sender as timage do NumTake:=tag;
  ncup:=AllGir[numTake].whichcup;
  ur:= AllGir[numTake].uroven;
  if not take then
  begin
       case image[numTake].Left+ image[numTake].Width div 2 of
            208..288: ncup:=1;
            336..432: ncup:=2;
            472..560: ncup:=3;
            608..700: ncup:=4;
            744..840: ncup:=5;
            888..976: ncup:=6;
       end;
       putgir(NumTake,ncup);

  end
  else
       begin
       with cup[ncup] do begin
       SWeight:= SWeight - AllGir[numTake].massa;
       for i:= ur to count-1 do
           begin
            gir[i]:=gir[i+1];
            ngi:=gir[i];
            AllGir[ngi].uroven:=AllGir[ngi].uroven -1;

      end;
        Count:= Count-1;

       end;
       end;


 { shape1.Visible:=false;
  shape2.Visible:=false; }
  changeV;
  luch;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
button1.Visible:=false;
form1.Caption:=inttostr(level+1);
level:=level+1;
InitMass(level);// рис. 1 уровня
luch;
end;


procedure TForm1.V1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
  var n:integer;
  begin
{   take:=not take ;
  if take then  with sender as Timage do begin
  numtake:=tag;
  Image[numTake].Left:=mouse.cursorpos.X-form1.Left - image[numtake].Width div 2;
  Image[numTake].Top:=mouse.cursorpos.y-form1.top -  image[numtake].height div 2-25;
  end;
 with sender as TImage do begin
 if ((x+left)>192)   and ((x+left)<304) then  n:=1;
 if ((x+left)>304)   and ((x+left)<456) then  n:=2;
 if ((x+left)>456)   and ((x+left)<584) then  n:=3;
 if ((x+left)>584)   and ((x+left)<728) then  n:=4;
 if ((x+left)>728)   and ((x+left)<856) then  n:=5;
 if ((x+left)>856)   and ((x+left)<1000) then  n:=6;
 label1.Caption:=inttostr(n);
   end;
 take:= false;  }
 end;


  procedure TForm1.ImageMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
   if take then
//    With sender as TImage do
       begin
       image[NumTake].Left:=mouse.CursorPos.x-form1.Left-image[NumTake].Width div 2;
       image[NumTake].Top:=mouse.CursorPos.y-form1.Top-image[NumTake].Height div 2-30;
       end;
end;

  procedure TForm1.Timer1Timer(Sender: TObject);
begin
   if take then
       begin
       image[NumTake].Left:=mouse.CursorPos.x-form1.Left-image[NumTake].Width div 2;
       image[NumTake].Top:=mouse.CursorPos.y-form1.Top-image[NumTake].Height div 2-30;
       end;
end;



end.
