Blame lazarus/click/unit1.pas

62606f
unit Unit1;
62606f
62606f
{$mode objfpc}{$H+}
62606f
62606f
interface
62606f
62606f
uses
62606f
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Math;
62606f
62606f
type
62606f
62606f
  { TForm1 }
62606f
62606f
  TForm1 = class(TForm)
62606f
    procedure FormCreate(Sender: TObject);
62606f
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
62606f
      Shift: TShiftState; X, Y: Integer);
62606f
    procedure FormPaint(Sender: TObject);
62606f
  private
62606f
    { private declarations }
62606f
  public
62606f
    { public declarations }
62606f
  end;
62606f
62606f
const
62606f
  cols = 10;
62606f
  rows = 20;
62606f
  size = 30;
62606f
  colorsCount = 3;
62606f
  colors: array [0..5] of TColor = (clWhite, clYellow, clRed, clBlue, clGray, clLtGray);
62606f
62606f
var
62606f
  Form1: TForm1;
62606f
  board: array[0..cols, 0..rows] of TColor;
62606f
62606f
implementation
62606f
62606f
{$R *.lfm}
62606f
62606f
function getBlock(x, y: integer): TColor;
62606f
begin
62606f
  Result := clBlack;
62606f
  if (x >= 0) and (x < cols) and (y >= 0) and (y < rows) then
62606f
    Result := board[x, y];
62606f
end;
62606f
62606f
procedure Check(x, y: integer);
62606f
var
62606f
  color: TColor;
62606f
begin
62606f
  color := getBlock(x, y);
62606f
  if (color <> colors[0]) and (color <> clBlack) then begin
62606f
    if getBlock(x-1, y) = color then begin
62606f
      board[x, y] := colors[0];
62606f
      Check(x-1, y);
62606f
      board[x-1, y] := colors[0];
62606f
    end;
62606f
    if getBlock(x+1, y) = color then begin
62606f
      board[x, y] := colors[0];
62606f
      Check(x+1, y);
62606f
      board[x+1, y] := colors[0];
62606f
    end;
62606f
    if getBlock(x, y-1) = color then begin
62606f
      board[x, y] := colors[0];
62606f
      Check(x, y-1);
62606f
      board[x, y-1] := colors[0];
62606f
    end;
62606f
    if getBlock(x, y+1) = color then begin
62606f
      board[x, y] := colors[0];
62606f
      Check(x, y+1);
62606f
      board[x, y+1] := colors[0];
62606f
    end;
62606f
  end;
62606f
end;
62606f
62606f
procedure Fall;
62606f
var
62606f
  x, y, dx, dy: integer;
62606f
begin
62606f
  dx := 0;
62606f
  for x := 0 to cols-1 do begin
62606f
    dy := 0;
62606f
    for y := 0 to rows-1 do begin
62606f
      if board[x, y] <> colors[0] then begin
62606f
        if (dx<>0) or (dy<>0) then begin
62606f
          board[x-dx, y-dy] := board[x, y];
62606f
          board[x, y] := colors[0];
62606f
        end;
62606f
      end else begin
62606f
        Inc(dy);
62606f
      end;
62606f
    end;
62606f
    if dy=rows then Inc(dx);
62606f
  end;
62606f
end;
62606f
62606f
{ TForm1 }
62606f
62606f
procedure TForm1.FormCreate(Sender: TObject);
62606f
var
62606f
  x, y: integer;
62606f
begin
62606f
  randomize;
62606f
  for x := 0 to cols-1 do begin
62606f
    for y := 0 to rows-1 do begin
62606f
      board[x, y] := colors[ RandomRange(1, colorsCount+1) ];
62606f
    end;
62606f
  end;
62606f
end;
62606f
62606f
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
62606f
  Shift: TShiftState; X, Y: Integer);
62606f
begin
62606f
  Check(x div size, rows-1-(y div size));
62606f
  Fall;
62606f
  if ssCtrl in Shift then FormCreate(nil);
62606f
  Refresh;
62606f
end;
62606f
62606f
procedure TForm1.FormPaint(Sender: TObject);
62606f
var
62606f
  x, y: integer;
62606f
begin
62606f
  for x := 0 to cols-1 do begin
62606f
    for y := 0 to rows-1 do begin
62606f
      Canvas.Brush.Color := board[x, rows-y-1];
62606f
      Canvas.FillRect(Rect(x*size, y*size, (x+1)*size, (y+1)*size));
62606f
    end;
62606f
  end;
62606f
end;
62606f
62606f
end.
62606f