Blob Blame Raw
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormPaint(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

const
  columns = 4;
  rows = 4;
  diameter = 40;

var
  Form1: TForm1;
  board: array[0..rows, 0..columns] of boolean;

implementation

{$R *.lfm}

procedure TForm1.FormPaint(Sender: TObject);
var
  r, c: integer;
begin
  for r := 0 to rows-1 do begin
    for c := 0 to columns-1 do begin
      if board[r, c] then begin
        Canvas.Brush.Color := clBlue;
      end else begin
        Canvas.Brush.Color := clYellow;
      end;
      Canvas.Ellipse(c*diameter, r*diameter, (c+1)*diameter, (r+1)*diameter);
    end;
  end;
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  r, c: integer;
begin
  c := X div diameter;
  r := Y div diameter;

  if (r>=0) and (c>=0) and (r<rows) and (c<columns) then begin
    board[r, c] := not board[r, c];
    if r > 0 then board[r-1, c] := not board[r-1, c];
    if r < rows-1 then board[r+1, c] := not board[r+1, c];
    if c > 0 then board[r, c-1] := not board[r, c-1];
    if c < columns-1 then board[r, c+1] := not board[r, c+1];
  end;

  Refresh;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  i: integer;
  x, y: integer;
begin
  for i := 0 to 100 do begin
    x := round(Random * columns * diameter);
    y := round(Random * rows * diameter);
    FormMouseDown(nil, mbLeft, [], x, y);
  end;
end;

end.