https://www.youtube.com/watch?v=Fqgk0wCqZ4Q
https://www.youtube.com/watch?v=Fqgk0wCqZ4Q
Код:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.StdCtrls;
type
//нам нужен экзист как метка для покраски клетки
Inside=record
resurse:Integer;
exist : Boolean;
end;
TField = record // это будет передавать параметры в стринггрид1
private
FWidth: Integer; // Ширина поля в клетках
FHeight: Integer; // Высота поля в клетках
end;
TForm1 = class(TForm)
Button1: TButton;
StringGrid1: TStringGrid;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure FieldCreate(Width, Height: Integer);
procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
procedure getMax();
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
maxRes:integer;
FArea: array of array of Inside;
implementation
{$R *.dfm}
procedure TForm1.getMax();
var
i,j,res:integer;
begin
res:=0;
for i := 3 to 5 do
for j := 3 to 5 do
begin
if (i<>4) and (i<>4) then
if res<Farea[i, j].resurse then res:= Farea[i, j].resurse;
Farea[i, j].exist:=True;
end;
maxRes:=res;
end;
procedure TForm1.FieldCreate(Width, Height: Integer);
var
i, j: Integer;
cell:Inside;
begin
Randomize;
// Задаем размер массива FCells.
SetLength(FArea, Width,Height);
// Создаем объекты клеток.
for i := 0 to Width - 1 do begin
for j := 0 to Height - 1 do begin
FArea[i, j].Resurse := Random(10);
FArea[i, j].exist:=False;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i, j: Integer;
res:integer;
begin
StringGrid1.RowCount:=10;
StringGrid1.ColCount:=10;
i:=0;
j:=0;
FieldCreate (StringGrid1.RowCount,StringGrid1.ColCount);
// Создаем объекты клеток.
begin
for i := 0 to StringGrid1.RowCount-1 do
begin
for j := 0 to StringGrid1.ColCount-1 do
begin
StringGrid1.Cells[i, j]:= inttostr(FArea[i, j].Resurse);
end;
end;
end;
// находим максимальное значение
i:=0;
j:=0;
getMax();
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
text: string;
begin
StringGrid1.Canvas.Brush.Color:=clRed;
if ((ACol=4)and(ARow=4) and (StringGrid1.Cells[ACol, ARow]<>'')) then
StringGrid1.Canvas.FillRect(Rect);
if StringGrid1.Cells[ACol, ARow] = '' then exit;
if (abs(ACol - 4) <= 1) and (abs(ARow-4) <= 1) and ((Acol <> 4) or (ARow<>4))
and (FArea[ACol, ARow].Exist = True) then begin
StringGrid1.Canvas.Brush.Color:=clGreen;
StringGrid1.Canvas.Font.Color := clWhite;
StringGrid1.Canvas.FillRect(Rect);
text := StringGrid1.Cells[ACol, ARow];
DrawText(StringGrid1.Canvas.Handle,
PChar(text), Length(text), Rect, DT_CENTER or DT_VCENTER or DT_SINGLELINE);
end;
end;
procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
var
Resources: String;
begin
Resources := StringGrid1.Cells[ACol, ARow];
Label1.Caption:='Число ресурсов: '+ Resources + ' MaxRes = ' + IntToStr(MaxRes);
end;
end.
Занимаюсь:1.зверьком- покемончиком для компьютерного затончика. 2 IT-грибами,что бы скушать потом их с вами.3. цифровым обезьянками, как куны ударяют за тянками