unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls;
type
TfrmMain = class(TForm)
btnExe: TButton;
btnExit: TButton;
StringGrid1: TStringGrid;
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure btnExitClick(Sender: TObject);
function TryNumber(SG:TStringGrid; n,ColPos,RowPos:integer):boolean;
procedure FillUp(SG:TStringGrid;Col,Row:integer);
procedure btnExeClick(Sender: TObject);
procedure TestValues(SG:TStringGrid;Col,Row:integer);
function IsFilled(SG:TStringGrid):boolean;
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
Test:boolean;
implementation
{$R *.dfm}
{**************************************************************}
function TfrmMain.TryNumber(SG:TStringGrid; n,ColPos,RowPos:integer):boolean;
var
i,j,ColPosCon,RowPosCon:integer;
verif:boolean;
begin
verif:=true;
for i:=0 to SG.ColCount-1 do
if SG.Cells[i,RowPos]=InttoStr(n) then
verif:=false;
for i:=0 to SG.RowCount-1 do
if SG.Cells[ColPos,i]=InttoStr(n) then
verif:=false;
case ColPos of
0: ColPosCon:=0;
1: ColPosCon:=0;
2: ColPosCon:=0;
3: ColPosCon:=3;
4: ColPosCon:=3;
5: ColPosCon:=3;
6: ColPosCon:=6;
7: ColPosCon:=6;
8: ColPosCon:=6;
end;
case RowPos of
0: RowPosCon:=0;
1: RowPosCon:=0;
2: RowPosCon:=0;
3: RowPosCon:=3;
4: RowPosCon:=3;
5: RowPosCon:=3;
6: RowPosCon:=6;
7: RowPosCon:=6;
8: RowPosCon:=6;
end;
for i:=ColPosCon to ColPosCon+2 do
begin
for j:=RowPosCon to RowPosCon+2 do
if SG.Cells[i,j]=InttoStr(n) then
verif:=false;
end;
result:=verif;
end;
procedure TfrmMain.TestValues(SG:TStringGrid;Col,Row:integer);
var
n:integer;
begin
Test:=false;
for n:=1 to 9 do
begin
if (TryNumber(SG,n,Col,Row)=true) and (SG.Cells[Col,Row]='') then
begin
Test:=true;
SG.Cells[Col,Row]:=InttoStr(n);
end;
end;
end;
function TfrmMain.IsFilled(SG:TStringGrid):boolean;
var
Row,Col:integer;
begin
result:=true;
for Row:=0 to SG.RowCount-1 do
begin
for Col:=0 to SG.ColCount-1 do
begin
if SG.Cells[Col,Row]='' then
result:=false;
end;
end;
end;
procedure TfrmMain.FillUp(SG:TStringGrid;Col,Row:integer);
var
n:integer;
begin
n:=Random(9)+1;
if (SG.Cells[Col,Row]<>'') then
begin
Col:=Col+1;
end
else if (TryNumber(SG,n,Col,Row)=true) and (SG.Cells[Col,Row]='') then
begin
SG.Cells[Col,Row]:=InttoStr(n);
Col:=Col+1;
end
else if (TryNumber(SG,n,Col,Row)=false) and (SG.Cells[Col,Row]='') then
begin
TestValues(SG,Col,Row);
if not Test then
Col:=Col-1
else if Test then
Col:=Col+1;
end;
if Col<0 then
begin
Row:=Row-1;
Col:=8;
SG.Cells[Col,Row]:='';
end;
if Row<0 then
Row:=0;
if Col>8 then
begin
Row:=Row+1;
Col:=0;
end;
if IsFilled(SG)=false then FillUp(SG,Col,Row);
end;