Subversion Repositories jumper

Rev

Rev 22 | Rev 24 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 22 Rev 23
Line 4... Line 4...
4
 
4
 
5
uses
5
uses
6
  SysUtils, Dialogs, Functions, ExtCtrls, Classes, Math;
6
  SysUtils, Dialogs, Functions, ExtCtrls, Classes, Math;
7
 
7
 
8
type
8
type
-
 
9
  TCoord = record
-
 
10
    X: integer;
-
 
11
    Y: integer;
-
 
12
  end;
-
 
13
 
9
  TFieldType = (ftUndefined, ftFullSpace, ftEmpty, ftRed, ftYellow, ftGreen);
14
  TFieldType = (ftUndefined, ftFullSpace, ftEmpty, ftRed, ftYellow, ftGreen);
10
 
15
 
11
  TFieldProperties = record
16
  TFieldProperties = record
12
    Typ: TFieldType;
17
    Typ: TFieldType;
13
    Goal: Boolean;
18
    Goal: Boolean;
Line 50... Line 55...
50
  TFieldState = (fsUndefined, fsError, fsLocked, fsAvailable, fsStone);
55
  TFieldState = (fsUndefined, fsError, fsLocked, fsAvailable, fsStone);
51
 
56
 
52
  TPlayGroundMatrix = record
57
  TPlayGroundMatrix = record
53
    Fields: array of array of TField;
58
    Fields: array of array of TField;
54
  public
59
  public
-
 
60
    procedure InitFieldArray(width, height: integer);
55
    function MatrixHasGoal: boolean;
61
    function MatrixHasGoal: boolean;
56
    function GoalFieldType: TFieldType;
62
    function GoalFieldType: TFieldType;
57
    function MatrixWorth: integer;
63
    function MatrixWorth: integer;
58
    procedure ClearMatrix(FreeVCL: boolean);
64
    procedure ClearMatrix(FreeVCL: boolean);
59
    function CloneMatrix: TPlayGroundMatrix;
65
    function CloneMatrix: TPlayGroundMatrix;
60
    function FieldState(t: TFieldType): TFieldState; overload;
66
    class function FieldState(t: TFieldType): TFieldState; overload; static;
61
    function FieldState(f: TField): TFieldState; overload;
67
    function FieldState(f: TField): TFieldState; overload;
62
    function FieldState(x, y: integer): TFieldState; overload;
68
    function FieldState(x, y: integer): TFieldState; overload;
63
    function CanJump(SourceX, SourceY, DestX, DestY: integer; DiagonalOK: boolean): boolean; overload;
69
    function CanJump(SourceX, SourceY, DestX, DestY: integer; DiagonalOK: boolean): boolean; overload;
64
    function CanJump(SourceX, SourceY: integer; DiagonalOK: boolean): boolean; overload;
70
    function CanJump(SourceX, SourceY: integer; DiagonalOK: boolean): boolean; overload;
65
    function CanJump(DiagonalOK: boolean): boolean; overload;
71
    function CanJump(DiagonalOK: boolean): boolean; overload;
-
 
72
    function IndexToCoord(index: integer): TCoord;
-
 
73
    function CoordToIndex(coord: TCoord): integer; overload;
-
 
74
    function CoordToIndex(x, y: integer): integer; overload;
-
 
75
    function Width: integer;
-
 
76
    function Height: integer;
66
  end;
77
  end;
67
 
78
 
68
function FieldTypeWorth(t: TFieldType): integer;
79
function FieldTypeWorth(t: TFieldType): integer;
69
 
80
 
70
implementation
81
implementation
Line 86... Line 97...
86
  result := false;
97
  result := false;
87
  for x := Low(Fields) to High(Fields) do
98
  for x := Low(Fields) to High(Fields) do
88
  begin
99
  begin
89
    for y := Low(Fields[x]) to High(Fields[x]) do
100
    for y := Low(Fields[x]) to High(Fields[x]) do
90
    begin
101
    begin
91
      result := result or Fields[x][y].Goal;
102
      result := result or Fields[x,y].Goal;
92
    end;
103
    end;
93
  end;
104
  end;
94
end;
105
end;
95
 
106
 
96
function TPlayGroundMatrix.GoalFieldType: TFieldType;
107
function TPlayGroundMatrix.GoalFieldType: TFieldType;
Line 100... Line 111...
100
  result := ftEmpty; // Damit der Compiler nicht meckert
111
  result := ftEmpty; // Damit der Compiler nicht meckert
101
  for x := Low(Fields) to High(Fields) do
112
  for x := Low(Fields) to High(Fields) do
102
  begin
113
  begin
103
    for y := Low(Fields[x]) to High(Fields[x]) do
114
    for y := Low(Fields[x]) to High(Fields[x]) do
104
    begin
115
    begin
105
      if Fields[x][y].Goal then result := Fields[x][y].FieldType
116
      if Fields[x,y].Goal then result := Fields[x,y].FieldType
-
 
117
    end;
-
 
118
  end;
-
 
119
end;
-
 
120
 
-
 
121
function TPlayGroundMatrix.Height: integer;
-
 
122
begin
-
 
123
  if Length(Fields) = 0 then
-
 
124
    result := 0
-
 
125
  else
-
 
126
    result := Length(Fields[0]);
-
 
127
end;
-
 
128
 
-
 
129
function TPlayGroundMatrix.IndexToCoord(index: integer): TCoord;
-
 
130
begin
-
 
131
  result.X := index mod Width;
-
 
132
  result.Y := index div Width;
-
 
133
end;
-
 
134
 
-
 
135
procedure TPlayGroundMatrix.InitFieldArray(width, height: integer);
-
 
136
var
-
 
137
  x, y: integer;
-
 
138
begin
-
 
139
  SetLength(Fields, width, height);
-
 
140
  for x := Low(Fields) to High(Fields) do
-
 
141
  begin
-
 
142
    for y := Low(Fields[x]) to High(Fields[x]) do
-
 
143
    begin
-
 
144
      Fields[x,y].FieldType := ftUndefined;
106
    end;
145
    end;
107
  end;
146
  end;
108
end;
147
end;
109
 
148
 
110
function TPlayGroundMatrix.MatrixWorth: integer;
149
function TPlayGroundMatrix.MatrixWorth: integer;
Line 114... Line 153...
114
  result := 0;
153
  result := 0;
115
  for x := Low(Fields) to High(Fields) do
154
  for x := Low(Fields) to High(Fields) do
116
  begin
155
  begin
117
    for y := Low(Fields[x]) to High(Fields[x]) do
156
    for y := Low(Fields[x]) to High(Fields[x]) do
118
    begin
157
    begin
119
      Inc(result, FieldTypeWorth(Fields[x][y].FieldType));
158
      Inc(result, FieldTypeWorth(Fields[x,y].FieldType));
120
    end;
159
    end;
121
  end;
160
  end;
122
end;
161
end;
123
 
162
 
-
 
163
function TPlayGroundMatrix.Width: integer;
-
 
164
begin
-
 
165
  result := Length(Fields);
-
 
166
end;
-
 
167
 
124
procedure TPlayGroundMatrix.ClearMatrix(FreeVCL: boolean);
168
procedure TPlayGroundMatrix.ClearMatrix(FreeVCL: boolean);
125
var
169
var
126
  x, y: integer;
170
  x, y: integer;
127
begin
171
begin
128
  for x := Low(Fields) to High(Fields) do
172
  for x := Low(Fields) to High(Fields) do
129
  begin
173
  begin
130
    for y := Low(Fields[x]) to High(Fields[x]) do
174
    for y := Low(Fields[x]) to High(Fields[x]) do
131
    begin
175
    begin
132
      if FreeVCL then
176
      if FreeVCL then
133
      begin
177
      begin
134
        if Assigned(Fields[x][y].Stone) then Fields[x][y].Stone.Free;
178
        if Assigned(Fields[x,y].Stone) then Fields[x,y].Stone.Free;
135
        if Assigned(Fields[x][y].Panel) then Fields[x][y].Panel.Free;
179
        if Assigned(Fields[x,y].Panel) then Fields[x,y].Panel.Free;
136
      end;
180
      end;
137
    end;
181
    end;
138
    SetLength(Fields[x], 0);
-
 
139
  end;
182
  end;
140
  SetLength(Fields, 0);
183
  SetLength(Fields, 0, 0);
141
end;
184
end;
142
 
185
 
143
function TPlayGroundMatrix.CloneMatrix: TPlayGroundMatrix;
186
function TPlayGroundMatrix.CloneMatrix: TPlayGroundMatrix;
144
var
187
var
145
  x, y: integer;
188
  x, y: integer;
Line 148... Line 191...
148
  for x := Low(Fields) to High(Fields) do
191
  for x := Low(Fields) to High(Fields) do
149
  begin
192
  begin
150
    SetLength(result.Fields[x], Length(Fields[x]));
193
    SetLength(result.Fields[x], Length(Fields[x]));
151
    for y := Low(Fields[x]) to High(Fields[x]) do
194
    for y := Low(Fields[x]) to High(Fields[x]) do
152
    begin
195
    begin
153
      result.Fields[x][y].FieldType := Fields[x][y].FieldType;
196
      result.Fields[x,y].FieldType := Fields[x,y].FieldType;
154
      result.Fields[x][y].Goal      := Fields[x][y].Goal;
197
      result.Fields[x,y].Goal      := Fields[x,y].Goal;
155
      result.Fields[x][y].Panel     := Fields[x][y].Panel;
198
      result.Fields[x,y].Panel     := Fields[x,y].Panel;
156
      result.Fields[x][y].Stone     := Fields[x][y].Stone;
199
      result.Fields[x,y].Stone     := Fields[x,y].Stone;
157
    end;
200
    end;
158
  end;
201
  end;
159
end;
202
end;
160
 
203
 
-
 
204
function TPlayGroundMatrix.CoordToIndex(x, y: integer): integer;
-
 
205
var
-
 
206
  c: TCoord;
-
 
207
begin
-
 
208
  c.X := x;
-
 
209
  c.Y := y;
-
 
210
  result := CoordToIndex(c);
-
 
211
end;
-
 
212
 
-
 
213
function TPlayGroundMatrix.CoordToIndex(coord: TCoord): integer;
-
 
214
begin
-
 
215
  result := coord.X + coord.Y * Width;
-
 
216
end;
-
 
217
 
161
function TPlayGroundMatrix.FieldState(t: TFieldType): TFieldState;
218
class function TPlayGroundMatrix.FieldState(t: TFieldType): TFieldState;
162
begin
219
begin
163
  result := fsError;
220
  result := fsError;
164
  case t of
221
  case t of
165
    ftFullSpace: result := fsLocked;
222
    ftFullSpace: result := fsLocked;
166
    ftEmpty:     result := fsAvailable;
223
    ftEmpty:     result := fsAvailable;
Line 179... Line 236...
179
begin
236
begin
180
  result := fsError;
237
  result := fsError;
181
  if (x < Low(Fields)) or (x > High(Fields)) then exit;
238
  if (x < Low(Fields)) or (x > High(Fields)) then exit;
182
  if (y < Low(Fields[x])) or (y > High(Fields[x])) then exit;
239
  if (y < Low(Fields[x])) or (y > High(Fields[x])) then exit;
183
 
240
 
184
  result := FieldState(Fields[x][y]);
241
  result := FieldState(Fields[x,y]);
185
end;
242
end;
186
 
243
 
187
function TPlayGroundMatrix.CanJump(SourceX, SourceY, DestX, DestY: integer; DiagonalOK: boolean): boolean;
244
function TPlayGroundMatrix.CanJump(SourceX, SourceY, DestX, DestY: integer; DiagonalOK: boolean): boolean;
188
begin
245
begin
189
  result := false;
246
  result := false;