1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
| unit Unit3;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Grids,
StdCtrls, ExtCtrls, CheckLst, Types;
type
ms = array of integer;
{ TForm3 }
TForm3 = class(TForm)
Label1: TLabel;
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure Label1Click(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject;
ACol, ARow: Integer; Rect: TRect;
State: TGridDrawState);
procedure StringGrid1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
a : array [1..9, 1..9] of integer;
procedure open(i, k : integer; b : ms);
public
end;
var
Form3: TForm3;
StringGrid1: TStringGrid;
implementation
{$R *.lfm}
{ TForm3 }
procedure open(i, k : integer; b : ms);
begin
StringGrid1.Cells[k - 1, i - 1] := ' ';
if (k <> 1) and (b[i, k - 1] = 0) and (StringGrid1.Cells[k - 2, i - 1] <> ' ') then open(i, k - 1, b);
if (k <> 9) and (b[i, k + 1] = 0) and (StringGrid1.Cells[k, i - 1] <> ' ') then open(i, k + 1, b);
if (i <> 1) and (b[i - 1, k] = 0) and (StringGrid1.Cells[k - 1, i - 2] <> ' ') then open(i - 1, k, b);
if (i <> 9) and (b[i + 1, k] = 0) and (StringGrid1.Cells[k - 1, i] <> ' ') then open(i + 1, k, b);
if (i <> 1) and (k <> 1) and (b[i - 1, k - 1] = 0) and (StringGrid1.Cells[k - 2, i - 2] <> ' ') then open(i - 1, k -1, b);
if (i <> 1) and (k <> 9) and (b[i - 1, k + 1] = 0) and (StringGrid1.Cells[k, i - 1] <> ' ') then open(i - 1, k + 1, b);
if (i <> 9) and (k <> 1) and (b[i + 1, k - 1] = 0) and (StringGrid1.Cells[k - 2, i] <> ' ') then open(i + 1, k - 1, b);
if (i <> 9) and (k <> 9) and (b[i + 1, k + 1] = 0) and (StringGrid1.Cells[k, i] <> ' ') then open(i + 1, k + 1, b);
end;
procedure mass(a:array of integer);
var
i, k : integer;
l, b : integer;
begin
randomize;
b := 0;
for i := 1 to 9 do
for k := 1 to 9 do
a[i, k] := 0;
while b <= 10 do
for i := 1 to 9 do
for k := 1 to 9 do
begin
l := random(10);
if l = 5 then
begin
b := b + 1;
a[i, k] := -1;
end;
end;
for i := 1 to 9 do
for k := 1 to 9 do
if a[i, k] = -1 then
begin
if (k <> 1) and (a[i, k - 1] <> -1) then a[i, k - 1] := a[i, k - 1] + 1;
if (k <> 9) and (a[i, k + 1] <> -1) then a[i, k + 1] := a[i, k + 1] + 1;
if (i <> 1) and (a[i - 1, k] <> -1) then a[i - 1, k] := a[i - 1, k] + 1;
if (i <> 9) and (a[i + 1, k] <> -1) then a[i + 1, k] := a[i + 1, k] + 1;
if (i <> 1) and (k <> 1) and (a[i - 1, k - 1] <> -1) then a[i - 1, k - 1] := a[i - 1, k - 1] + 1;
if (i <> 1) and (k <> 9) and (a[i - 1, k + 1] <> -1) then a[i - 1, k + 1] := a[i - 1, k + 1] + 1;
if (i <> 9) and (k <> 1) and (a[i + 1, k - 1] <> -1) then a[i + 1, k - 1] := a[i + 1, k - 1] + 1;
if (i <> 9) and (k <> 9) and (a[i + 1, k + 1] <> -1) then a[i + 1, k + 1] := a[i + 1, k + 1] + 1;
end;
end;
procedure TForm3.FormCreate(Sender: TObject);
var
i, k : integer;
l, b : integer;
begin
randomize;
b := 0;
for i := 1 to 9 do
for k := 1 to 9 do
a[i, k] := 0;
while b <= 10 do
for i := 1 to 9 do
for k := 1 to 9 do
begin
l := random(10);
if l = 5 then
begin
b := b + 1;
a[i, k] := -1;
end;
end;
for i := 1 to 9 do
for k := 1 to 9 do
if a[i, k] = -1 then
begin
if (k <> 1) and (a[i, k - 1] <> -1) then a[i, k - 1] := a[i, k - 1] + 1;
if (k <> 9) and (a[i, k + 1] <> -1) then a[i, k + 1] := a[i, k + 1] + 1;
if (i <> 1) and (a[i - 1, k] <> -1) then a[i - 1, k] := a[i - 1, k] + 1;
if (i <> 9) and (a[i + 1, k] <> -1) then a[i + 1, k] := a[i + 1, k] + 1;
if (i <> 1) and (k <> 1) and (a[i - 1, k - 1] <> -1) then a[i - 1, k - 1] := a[i - 1, k - 1] + 1;
if (i <> 1) and (k <> 9) and (a[i - 1, k + 1] <> -1) then a[i - 1, k + 1] := a[i - 1, k + 1] + 1;
if (i <> 9) and (k <> 1) and (a[i + 1, k - 1] <> -1) then a[i + 1, k - 1] := a[i + 1, k - 1] + 1;
if (i <> 9) and (k <> 9) and (a[i + 1, k + 1] <> -1) then a[i + 1, k + 1] := a[i + 1, k + 1] + 1;
end;
end;
procedure TForm3.StringGrid1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var x1, y1, b, l : integer;
begin
b := 10;
l := 0;
StringGrid1.MouseToCell(X,Y,X1,Y1);
while a[y1 + 1, x1 + 1] <> 0 do
mass(a);
Label1.Caption := 'Нажата ячейка ' + inttostr(x1 + 1) + ' ' + inttostr(y1 + 1);
if a[y1 + 1, x1 + 1] = 0 then open(x1, y1, a)
else StringGrid1.Cells[x1, y1] := inttostr(a[y1 + 1, x1 + 1]);
end;
end. |