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
|
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
States, Problems;
const
CELL_SIZE = 32;
type
{ TForm1 }
TForm1 = class(TForm)
BackBuffer: TImage;
ImageList: TImageList;
MoveList: TListBox;
Screen: TImage;
OpenLevel: TButton;
OpenDialog: TOpenDialog;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure OpenLevelClick(Sender: TObject);
private
public
end;
var
Form1: TForm1;
Board: TBoard; { Игровое поле. }
implementation
{$R *.lfm}
{ Загрузить уровень из файла. }
procedure LoadLevel(FileName: String);
var
f: File of Char;
i, j, ColumnCount, CurrentColumn: Integer;
c: Char;
InitialState: TState;
begin
AssignFile(f, FileName); { Открыть файл. }
Reset(f);
FreeAndNil(Board);
InitialState := TState.Create;
{ Читаем первую строку, чтобы найти количество столбцов. }
ColumnCount := 0;
repeat
Read(f, c);
if c = 'x' then
InitialState.AddWall(ColumnCount, 0);
Inc(ColumnCount)
until c <> 'x';
Dec(ColumnCount);
Read(f, c); { Считываем и пропускаем перевод строки. }
j := 1;
repeat { Цикл по строкам. }
CurrentColumn := 0;
for i := 0 to ColumnCount - 1 do { Цикл по элементам строки. }
begin
Read(f, c); { Считаем текущий эелемент }
case c of
'x':
begin
InitialState.AddWall(i, j);
Inc(CurrentColumn)
end;
's':
InitialState.SetAgent(i, j);
'p': { Если это "место". }
InitialState.AddPlace(i, j);
'b': { Если это "камень". }
InitialState.AddBoulder(i, j)
end
end;
Inc(j);
Read(f, c); { считываем и пропускаем возврат каретки }
Read(f, c) { Считываем и пропускаем перевод строки. }
until CurrentColumn = ColumnCount;
Board := TBoard.Create(ColumnCount, j, InitialState);
CloseFile(f) { закрыть файл }
end;
procedure DrawCell(APosition: TPosition; Bitmap: TBitmap);
begin
Form1.BackBuffer.Canvas.Draw(APosition.X * CELL_SIZE,
APosition.Y * CELL_SIZE,
Bitmap)
end;
procedure RedrawField;
var
Bitmap: TBitmap;
ScreenRect: TRect;
CurrentPosition: TPosition;
begin
Bitmap := TBitmap.Create; { Объект для временного хранения рисунка. }
Form1.BackBuffer.Canvas.Brush.Color := clBlack;
Form1.BackBuffer.Canvas.Clear;
{ Рисуем стены. }
Form1.ImageList.GetBitmap(8, bitmap);
for CurrentPosition in Board.InitialState.Walls do
DrawCell(CurrentPosition, bitmap);
{ Рисуем агента. }
Form1.ImageList.GetBitmap(4, bitmap);
DrawCell(Board.InitialState.Agent, Bitmap);
{ Рисуем камни. }
Form1.ImageList.GetBitmap(2, bitmap);
for CurrentPosition in Board.InitialState.Boulders do
DrawCell(CurrentPosition, bitmap);
{ Рисуем объекты. }
Form1.ImageList.GetBitmap(1, bitmap);
For CurrentPosition in Board.InitialState.Places do
DrawCell(CurrentPosition, bitmap);
Bitmap.Free;
{ Копируем содержимое виртуального экрана на основной. }
ScreenRect := Rect(0, 0, Screen.Width, Screen.Height);
Form1.Screen.Canvas.CopyRect(ScreenRect, Form1.BackBuffer.Canvas, ScreenRect)
end;
{ TForm1 }
procedure TForm1.OpenLevelClick(Sender: TObject);
var
i: Integer;
Solution: TStates;
SolutionLine: String;
begin
if OpenDialog.Execute then
begin
LoadLevel(OpenDialog.FileName);
RedrawField;
Solution := Search(Board.InitialState);
for i := 0 to High(Solution) do
begin
SolutionLine := 'Agent: (' + IntToStr(Solution[i].Agent.X)
+ ', ' + IntToStr(Solution[i].Agent.Y) + ')';
FreeAndNil(Solution[i]);
MoveList.AddItem(SolutionLine, nil)
end
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Board := nil
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FreeAndNil(Board)
end;
end.
|