-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tree.pas
executable file
·185 lines (170 loc) · 5.51 KB
/
Tree.pas
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
unit Tree;
interface
uses
SysUtils{, Stacks};
function CurrAbsBoardPos (CurrLevel, CurrBoardPos, PrevAbsBoardPos: integer): integer;
procedure DrawUnit (CurrLevel, CurrBoardCount, PrevAbsBoardPos, CurrBoardPos: integer);
procedure ImageInit;
procedure MarkBadThread (Level, AbsBoardPos: integer);
procedure MarkGoodThread (Level, AbsBoardPos: integer);
procedure ClearTree;
implementation
uses
Main, Graphics, Types;
const
MaxBoardSizeForTree = 6;
MarkColor = clGreen;
NormalColor = clBlack;
NormalWidth = 1;
NormalBrushColor = clWhite;
BadLineColor = clRed;
BadLineLength = 10;
BadLineWidth = 3;
GoodCircleColor = clGreen;
GoodCircleRadius = 7;
LineWidth = 35;
LineHeight = 50;
UnitRadius = 3;
TextDistance = 3;
Start: TPoint = (x: 300; y: 20);
{âû÷èñëåíèå ìàêñèìàëüíîãî êîëè÷åñòâà âàðèàíòîâ, êîòîðûå ìîãóò áûòü íà îïðåäåë¸ííîé ñòðîêå}
function MaxVariants (value: byte): cardinal;
var
itr, Max: shortint;
begin
if value = 0 then
begin
Result:= 1;
Exit;
end;
Result:= BoardSize;
if value = 1 then
Exit;
Max:= BoardSize - 2;
if BoardSize < 6 then
begin
for itr:= 2 to value do
begin
Result:= Result * Max;
if Max > 1 then
Dec (Max);
end;
end
else
for itr:= 2 to value do
begin
Result:= Result * Max;
if Max > 2 then
Dec (Max);
end;
end;
function CanDrawTree (BoardSize: shortint): boolean;
begin
if BoardSize > MaxBoardSizeForTree then
begin
Main.QueenForm.Image.Visible:= false;
Result:= false;
end
else
begin
Main.QueenForm.Image.Visible:= true;
Result:= true;
end;
end;
procedure ClearTree;
begin
if CanDrawTree (BoardSize) then
Main.QueenForm.Image.Canvas.FillRect (Main.QueenForm.Image.Canvas.ClipRect);
end;
procedure ImageInit;
var
Can: boolean;
begin
Can:=CanDrawTree (BoardSize);
if Can then
with Main.QueenForm.Image do
begin
Canvas.Brush.Color := clWhite;
Picture:= nil;
Height:= (BoardSize + 1) * LineHeight + Start.Y;
Width:= MaxVariants (BoardSize) * LineWidth + Start.X;
Canvas.Font.Color:= clBlue;
end;
Main.QueenForm.ScrollBox.Visible:=Can;
end;
function CurrAbsBoardPos (CurrLevel, CurrBoardPos, PrevAbsBoardPos: integer): integer;
begin
Result:= MaxVariants (CurrLevel) div MaxVariants (CurrLevel - 1) * (PrevAbsBoardPos - 1) + CurrBoardPos;
end;
procedure DrawUnit (CurrLevel, CurrBoardCount, PrevAbsBoardPos, CurrBoardPos: integer);
const
chars = 'ABCDEFGHIJ';
var
PrevCoord, CurrCoord: TPoint;
begin
if CanDrawTree (BoardSize) then
begin
CurrCoord.X:= Main.QueenForm.Image.Width div MaxVariants (CurrLevel) * CurrAbsBoardPos (CurrLevel, CurrBoardCount - 1, PrevAbsBoardPos) +
Main.QueenForm.Image.Width div MaxVariants (CurrLevel) div 2;
CurrCoord.Y:= Start.Y + CurrLevel * LineHeight;
Dec (CurrLevel); //äëÿ âû÷èñëåíèÿ ïðåäûäóùåé êîîðäèíàòû
PrevCoord.X:= Main.QueenForm.Image.Width div MaxVariants (CurrLevel) * (PrevAbsBoardPos - 1) +
Main.QueenForm.Image.Width div MaxVariants (CurrLevel) div 2;
PrevCoord.Y:= Start.Y + CurrLevel * LineHeight;
with Main.QueenForm.Image.Canvas do
begin
MoveTo (PrevCoord.X, PrevCoord.Y);
LineTo (CurrCoord.X, CurrCoord.Y);
Ellipse (CurrCoord.X - UnitRadius, CurrCoord.Y - UnitRadius, CurrCoord.X + UnitRadius, CurrCoord.Y + UnitRadius);
//TextOut (CurrCoord.X + TextDistance, CurrCoord.Y, '(' + IntToStr (CurrLevel + 1) + ', ' + IntToStr (CurrBoardPos) + ')'); //ïåðâîíà÷àëüíîå èìåíîâàíèå óçëîâ äåðàâà
TextOut (CurrCoord.X + TextDistance, CurrCoord.Y, Format('(%s%d)',[chars[CurrBoardPos],BoardSize - CurrLevel])); //èçìåíåíî: óçåë äåðåâà òåïåðü ïîäïèñàí ñîîòâåòñòâóþùåé ïîñòàíîâêîé ôåðçÿ (B4, A3 etc)
end;
with Main.QueenForm.ScrollBox do //ïîêàçûâàåì èçìåíåíèÿ â äåðåâå
begin
HorzScrollBar.Position:= CurrCoord.X - Width div 2;
VertScrollBar.Position:= CurrCoord.Y - Height div 2;
end;
end;
end;
procedure MarkBadThread (Level, AbsBoardPos: integer);
var
X, Y: integer;
begin
if CanDrawTree (BoardSize) then
begin
X:= Main.QueenForm.Image.Width div MaxVariants (Level) * (AbsBoardPos - 1) +
Main.QueenForm.Image.Width div MaxVariants (Level) div 2;
Y:= Start.Y + Level * LineHeight;
with Main.QueenForm.Image.Canvas do
begin
Pen.Color:= BadLineColor;
Pen.Width:= BadLineWidth;
MoveTo (X - BadLineLength div 2, Y - BadLineLength div 2);
LineTo (X + BadLineLength div 2, Y + BadLineLength div 2);
MoveTo (X + BadLineLength div 2, Y - BadLineLength div 2);
LineTo (X - BadLineLength div 2, Y + BadLineLength div 2);
Pen.Color:= NormalColor;
Pen.Width:= NormalWidth;
end;
end;
end;
procedure MarkGoodThread (Level, AbsBoardPos: integer);
var
X, Y: integer;
begin
if CanDrawTree (BoardSize) then
begin
X:= Main.QueenForm.Image.Width div MaxVariants (Level) * (AbsBoardPos - 1) +
Main.QueenForm.Image.Width div MaxVariants (Level) div 2;
Y:= Start.Y + Level * LineHeight;
with Main.QueenForm.Image.Canvas do
begin
Pen.Color:= GoodCircleColor;
Brush.Color:= GoodCircleColor;
Ellipse (X - GoodCircleRadius, Y - GoodCircleRadius, X + GoodCircleRadius, Y + GoodCircleRadius);
Brush.Color:= NormalBrushColor;
Pen.Color:= NormalColor;
end;
end;
end;
end.