Rev 7 | Rev 9 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | daniel-mar | 1 | unit Main; |
2 | |||
3 | interface |
||
4 | |||
5 | uses |
||
6 | Windows, SysUtils, Classes, Graphics, Dialogs, StdCtrls, Menus, Controls, |
||
7 | ComCtrls, ExtCtrls, Forms, MMSystem, LevelFunctions, Registry; |
||
8 | |||
9 | type |
||
10 | TField = record |
||
11 | FieldType: TFieldType; |
||
12 | Goal: Boolean; |
||
13 | Panel: TPanel; |
||
14 | Stone: TImage; |
||
15 | end; |
||
16 | |||
8 | daniel-mar | 17 | TGoalStatus = (gsNoGoal, gsMultipleStonesRemaining, gsLastStoneInGoalRed, gsLastStoneInGoalYellow, gsLastStoneInGoalGreen, gsLastStoneOutsideGoal); |
18 | |||
1 | daniel-mar | 19 | TFieldState = (fsError, fsLocked, fsAvailable, fsStone); |
20 | |||
21 | TPlayGroundMatrix = array of array of TField; |
||
22 | |||
23 | TMainForm = class(TForm) |
||
24 | Playground: TPanel; |
||
25 | MainMenu: TMainMenu; |
||
26 | Help1: TMenuItem; |
||
27 | MExit: TMenuItem; |
||
28 | Statistics: TStatusBar; |
||
29 | Timer: TTimer; |
||
30 | MNewGame: TMenuItem; |
||
31 | Help2: TMenuItem; |
||
32 | MAbout: TMenuItem; |
||
33 | MHelp: TMenuItem; |
||
34 | N5: TMenuItem; |
||
35 | MJumpHistory: TMenuItem; |
||
36 | N2: TMenuItem; |
||
37 | N4: TMenuItem; |
||
38 | MHighScores: TMenuItem; |
||
39 | MRestartGame: TMenuItem; |
||
40 | MSettings: TMenuItem; |
||
41 | MEnableSound: TMenuItem; |
||
42 | MPauseTime: TMenuItem; |
||
43 | N1: TMenuItem; |
||
44 | MUndo: TMenuItem; |
||
45 | N3: TMenuItem; |
||
46 | procedure MExitClick(Sender: TObject); |
||
47 | procedure TimerTimer(Sender: TObject); |
||
48 | procedure MNewGameClick(Sender: TObject); |
||
49 | procedure MAboutClick(Sender: TObject); |
||
50 | procedure FormShow(Sender: TObject); |
||
51 | procedure FormCreate(Sender: TObject); |
||
52 | procedure FormDestroy(Sender: TObject); |
||
53 | procedure MJumpHistoryClick(Sender: TObject); |
||
54 | procedure MRestartGameClick(Sender: TObject); |
||
55 | procedure MHighScoresClick(Sender: TObject); |
||
56 | procedure MPauseTimeClick(Sender: TObject); |
||
57 | procedure FormClose(Sender: TObject; var Action: TCloseAction); |
||
58 | procedure MHelpClick(Sender: TObject); |
||
59 | procedure MEnableSoundClick(Sender: TObject); |
||
6 | daniel-mar | 60 | procedure MUndoClick(Sender: TObject); |
1 | daniel-mar | 61 | private |
62 | CountedSeconds: Integer; |
||
63 | LevelFile: String; |
||
64 | LookupFieldCoordinateArray: array of TPoint; |
||
65 | OriginalPlayGroundMatrix: TPlayGroundMatrix; |
||
6 | daniel-mar | 66 | PrevPlaygroundMatrixes: array of TPlayGroundMatrix; |
1 | daniel-mar | 67 | PlaygroundMatrix: TPlayGroundMatrix; |
68 | Points: Integer; |
||
69 | LevelTotalStones: Integer; |
||
70 | LevelRemovedStones: Integer; |
||
71 | JumpHistory: TStringList; |
||
72 | procedure LoadSettings; |
||
73 | procedure SaveSettings; |
||
74 | procedure RestartLevel; |
||
75 | procedure SetNewPlayGroundMatrix(Matrix: TPlayGroundMatrix); |
||
76 | procedure RedrawStonesFromMatrix(Matrix: TPlayGroundMatrix); |
||
77 | function AskForLevel: String; |
||
78 | function AreJumpsPossible: boolean; |
||
79 | procedure StoneDraggingAllow(Stone: TImage; Allow: boolean); |
||
80 | procedure NewGame(Filename: string); |
||
81 | function LevelTime: String; |
||
82 | procedure DestroyLevel; |
||
83 | procedure RefreshTime; |
||
84 | procedure RefreshPoints; |
||
85 | procedure RefreshStonesRemoved; |
||
86 | procedure CountPoints(t: TFieldType); |
||
87 | procedure RemoveStone(x, y: integer; count_points: boolean); |
||
88 | procedure DoJump(SourceTag, DestTag: integer); |
||
89 | function CanJump(x, y: integer): boolean; |
||
90 | function MayJump(SourceX, SourceY, DestX, DestY: integer): boolean; overload; |
||
91 | function MayJump(SourceTag, DestTag: integer): boolean; overload; |
||
92 | procedure StoneDragOver(Sender, Source: TObject; X, |
||
93 | Y: Integer; State: TDragState; var Accept: Boolean); |
||
94 | procedure StoneDragDrop(Sender, Source: TObject; X, Y: Integer); |
||
95 | procedure DrawField(x, y: integer; t: TFieldProperties; halftabs: integer); |
||
96 | function DrawStone(fieldtype: TFieldType; panel: TPanel): TImage; |
||
97 | function DrawStoneBox(x, y, tag, halftabs: integer; isGoal: boolean): TPanel; |
||
98 | procedure BuildPlayground(LevelArray: TLevelArray); |
||
99 | function FieldState(t: TFieldType): TFieldState; overload; |
||
100 | function FieldState(f: TField): TFieldState; overload; |
||
101 | function FieldState(x, y: integer): TFieldState; overload; |
||
102 | procedure ClearMatrix(Matrix: TPlayGroundMatrix; FreeVCL: boolean); |
||
103 | function CloneMatrix(Source: TPlayGroundMatrix): TPlayGroundMatrix; |
||
8 | daniel-mar | 104 | function MatrixHasGoal(Matrix: TPlayGroundMatrix): boolean; |
1 | daniel-mar | 105 | procedure LoadPictureForType(FieldType: TFieldType; Picture: TPicture); |
6 | daniel-mar | 106 | function MatrixWorth(Matrix: TPlayGroundMatrix): integer; |
8 | daniel-mar | 107 | function GoalStatus: TGoalStatus; |
108 | function GoalFieldType(Matrix: TPlayGroundMatrix): TFieldType; |
||
1 | daniel-mar | 109 | end; |
110 | |||
111 | var |
||
112 | MainForm: TMainForm; |
||
113 | |||
114 | implementation |
||
115 | |||
116 | uses |
||
117 | About, Finish, Choice, Functions, History, HighScore, Help, Constants; |
||
118 | |||
119 | {$R *.dfm} |
||
120 | |||
8 | daniel-mar | 121 | function TMainForm.MatrixHasGoal(Matrix: TPlayGroundMatrix): boolean; |
122 | var |
||
123 | i, j: integer; |
||
124 | begin |
||
125 | result := false; |
||
126 | for i := Low(Matrix) to High(Matrix) do |
||
127 | begin |
||
128 | for j := Low(Matrix[i]) to High(Matrix[i]) do |
||
129 | begin |
||
130 | result := result or Matrix[i][j].Goal; |
||
131 | end; |
||
132 | end; |
||
133 | end; |
||
134 | |||
135 | function TMainForm.GoalFieldType(Matrix: TPlayGroundMatrix): TFieldType; |
||
136 | var |
||
137 | i, j: integer; |
||
138 | begin |
||
139 | for i := Low(Matrix) to High(Matrix) do |
||
140 | begin |
||
141 | for j := Low(Matrix[i]) to High(Matrix[i]) do |
||
142 | begin |
||
143 | if Matrix[i][j].Goal then result := Matrix[i][j].FieldType |
||
144 | end; |
||
145 | end; |
||
146 | end; |
||
147 | |||
6 | daniel-mar | 148 | function TMainForm.MatrixWorth(Matrix: TPlayGroundMatrix): integer; |
149 | var |
||
150 | i, j: integer; |
||
151 | begin |
||
152 | result := 0; |
||
153 | for i := Low(Matrix) to High(Matrix) do |
||
154 | begin |
||
155 | for j := Low(Matrix[i]) to High(Matrix[i]) do |
||
156 | begin |
||
157 | Inc(result, FieldTypeWorth(Matrix[i][j].FieldType)); |
||
158 | end; |
||
159 | end; |
||
160 | end; |
||
161 | |||
1 | daniel-mar | 162 | procedure TMainForm.ClearMatrix(Matrix: TPlayGroundMatrix; FreeVCL: boolean); |
163 | var |
||
164 | i, j: integer; |
||
165 | begin |
||
166 | for i := Low(Matrix) to High(Matrix) do |
||
167 | begin |
||
168 | for j := Low(Matrix[i]) to High(Matrix[i]) do |
||
169 | begin |
||
170 | if FreeVCL then |
||
171 | begin |
||
172 | if Assigned(Matrix[i][j].Stone) then Matrix[i][j].Stone.Free; |
||
173 | if Assigned(Matrix[i][j].Panel) then Matrix[i][j].Panel.Free; |
||
174 | end; |
||
175 | end; |
||
176 | SetLength(Matrix[i], 0); |
||
177 | end; |
||
178 | SetLength(Matrix, 0); |
||
179 | end; |
||
180 | |||
181 | procedure TMainForm.RedrawStonesFromMatrix(Matrix: TPlayGroundMatrix); |
||
182 | var |
||
183 | i, j: integer; |
||
184 | begin |
||
185 | for i := Low(Matrix) to High(Matrix) do |
||
186 | begin |
||
187 | for j := Low(Matrix[i]) to High(Matrix[i]) do |
||
188 | begin |
||
189 | if Assigned(Matrix[i][j].Stone) then |
||
190 | begin |
||
191 | LoadPictureForType(Matrix[i][j].FieldType, Matrix[i][j].Stone.Picture); |
||
192 | StoneDraggingAllow(Matrix[i][j].Stone, FieldState(Matrix[i][j].FieldType) <> fsAvailable); |
||
193 | end; |
||
194 | end; |
||
195 | end; |
||
196 | end; |
||
197 | |||
198 | procedure TMainForm.DestroyLevel; |
||
6 | daniel-mar | 199 | var |
200 | i: Integer; |
||
1 | daniel-mar | 201 | begin |
202 | MPauseTime.Enabled := false; |
||
203 | Timer.Enabled := false; |
||
204 | |||
205 | MRestartGame.Enabled := false; |
||
206 | |||
207 | LevelFile := ''; |
||
208 | |||
209 | CountedSeconds := 0; |
||
210 | RefreshTime; |
||
211 | |||
212 | Points := 0; |
||
213 | RefreshPoints; |
||
214 | |||
215 | LevelRemovedStones := 0; |
||
216 | LevelTotalStones := 0; |
||
217 | RefreshStonesRemoved; |
||
218 | |||
219 | JumpHistory.Clear; |
||
220 | |||
221 | ClearMatrix(PlayGroundMatrix, true); |
||
222 | ClearMatrix(OriginalPlayGroundMatrix, false); |
||
6 | daniel-mar | 223 | for i := 0 to Length(PrevPlaygroundMatrixes)-1 do |
224 | ClearMatrix(PrevPlaygroundMatrixes[i], false); |
||
225 | SetLength(PrevPlaygroundMatrixes, 0); |
||
226 | MUndo.Enabled := false; |
||
1 | daniel-mar | 227 | |
228 | SetLength(LookupFieldCoordinateArray, 0); |
||
229 | end; |
||
230 | |||
231 | procedure TMainForm.LoadPictureForType(FieldType: TFieldType; Picture: TPicture); |
||
232 | begin |
||
233 | case FieldType of |
||
234 | ftEmpty: Picture.Bitmap.LoadFromResourceName(HInstance, RES_EMPTY); |
||
235 | ftGreen: Picture.Bitmap.LoadFromResourceName(HInstance, RES_GREEN); |
||
236 | ftYellow: Picture.Bitmap.LoadFromResourceName(HInstance, RES_YELLOW); |
||
237 | ftRed: Picture.Bitmap.LoadFromResourceName(HInstance, RES_RED); |
||
238 | end; |
||
239 | end; |
||
240 | |||
241 | function TMainForm.DrawStone(fieldtype: TFieldType; panel: TPanel): TImage; |
||
242 | begin |
||
243 | result := TImage.Create(panel); |
||
244 | result.Parent := panel; |
||
245 | LoadPictureForType(fieldtype, result.Picture); |
||
246 | result.Width := panel.Width - 2*MET_SHAPE_MARGIN; |
||
247 | result.Height := panel.Height - 2*MET_SHAPE_MARGIN; |
||
248 | result.Left := MET_SHAPE_MARGIN; |
||
249 | result.Top := MET_SHAPE_MARGIN; |
||
250 | result.Center := true; |
||
251 | result.Transparent := true; |
||
252 | |||
253 | result.Tag := panel.Tag; |
||
254 | result.OnDragOver := panel.OnDragOver; |
||
255 | result.OnDragDrop := panel.OnDragDrop; |
||
256 | |||
257 | StoneDraggingAllow(result, FieldState(fieldtype) <> fsAvailable); |
||
258 | end; |
||
259 | |||
260 | procedure TMainForm.StoneDraggingAllow(Stone: TImage; Allow: boolean); |
||
261 | begin |
||
262 | if Allow then |
||
263 | begin |
||
264 | Stone.DragMode := dmAutomatic; |
||
265 | (Stone.Parent as TPanel).DragMode := dmAutomatic; |
||
266 | end |
||
267 | else |
||
268 | begin |
||
269 | Stone.DragMode := dmManual; |
||
270 | (Stone.Parent as TPanel).DragMode := dmManual; |
||
271 | end; |
||
272 | end; |
||
273 | |||
274 | function TMainForm.DrawStoneBox(x, y, tag, halftabs: integer; isGoal: boolean): TPanel; |
||
275 | begin |
||
276 | result := TPanel.Create(Playground); |
||
277 | result.Parent := Playground; |
||
278 | if isGoal then |
||
279 | begin |
||
280 | result.BevelInner := bvLowered; |
||
281 | end; |
||
282 | result.Color := Playground.Color; |
||
283 | result.BevelOuter := bvLowered; |
||
284 | result.Width := MET_FIELD_SIZE; |
||
285 | result.Height := MET_FIELD_SIZE; |
||
286 | result.Left := x * (MET_FIELD_SIZE+MET_FIELD_SPACE) + MET_FIELD_SPACE - (halftabs*MET_HALFTAB_SIZE); |
||
287 | result.Top := y * (MET_FIELD_SIZE+MET_FIELD_SPACE) + MET_FIELD_SPACE; |
||
288 | |||
289 | result.Tag := tag; |
||
290 | result.OnDragOver := StoneDragOver; |
||
291 | result.OnDragDrop := StoneDragDrop; |
||
292 | end; |
||
293 | |||
294 | procedure TMainForm.MExitClick(Sender: TObject); |
||
295 | begin |
||
296 | Close; |
||
297 | end; |
||
298 | |||
299 | function TMainForm.FieldState(t: TFieldType): TFieldState; |
||
300 | begin |
||
301 | result := fsError; |
||
302 | case t of |
||
303 | ftLocked: result := fsLocked; |
||
304 | ftLockedWithTab: result := fsLocked; |
||
305 | ftEmpty: result := fsAvailable; |
||
306 | ftGreen: result := fsStone; |
||
307 | ftYellow: result := fsStone; |
||
308 | ftRed: result := fsStone; |
||
309 | end; |
||
310 | end; |
||
311 | |||
312 | function TMainForm.FieldState(f: TField): TFieldState; |
||
313 | begin |
||
314 | result := FieldState(f.FieldType); |
||
315 | end; |
||
316 | |||
317 | function TMainForm.FieldState(x, y: integer): TFieldState; |
||
318 | begin |
||
319 | result := fsError; |
||
320 | if (x < Low(PlayGroundMatrix)) or (x > High(PlayGroundMatrix)) then exit; |
||
321 | if (y < Low(PlayGroundMatrix[x])) or (y > High(PlayGroundMatrix[x])) then exit; |
||
322 | |||
323 | result := FieldState(PlayGroundMatrix[x][y]); |
||
324 | end; |
||
325 | |||
326 | procedure TMainForm.RefreshTime; |
||
327 | begin |
||
328 | Statistics.Panels.Items[0].Text := Format(LNG_TIME, [LevelTime]); |
||
329 | end; |
||
330 | |||
331 | procedure TMainForm.RefreshStonesRemoved; |
||
332 | begin |
||
333 | Statistics.Panels.Items[1].Text := Format(LNG_STONES_REMOVED, [LevelRemovedStones, LevelTotalStones]); |
||
334 | end; |
||
335 | |||
336 | procedure TMainForm.RefreshPoints; |
||
337 | begin |
||
338 | Statistics.Panels.Items[2].Text := Format(LNG_POINTS, [Points]); |
||
339 | end; |
||
340 | |||
341 | procedure TMainForm.CountPoints(t: TFieldType); |
||
342 | begin |
||
6 | daniel-mar | 343 | inc(Points, FieldTypeWorth(t)); |
1 | daniel-mar | 344 | RefreshPoints; |
345 | end; |
||
346 | |||
347 | procedure TMainForm.RemoveStone(x, y: integer; count_points: boolean); |
||
348 | begin |
||
349 | if count_points then |
||
350 | begin |
||
351 | CountPoints(PlayGroundMatrix[x, y].FieldType); |
||
352 | Inc(LevelRemovedStones); |
||
353 | RefreshStonesRemoved; |
||
354 | end; |
||
355 | PlayGroundMatrix[x, y].FieldType := ftEmpty; |
||
356 | LoadPictureForType(PlayGroundMatrix[x, y].FieldType, PlayGroundMatrix[x, y].Stone.Picture); |
||
357 | StoneDraggingAllow(PlayGroundMatrix[x, y].Stone, false); |
||
358 | end; |
||
359 | |||
360 | function TMainForm.CanJump(x, y: integer): boolean; |
||
361 | begin |
||
362 | if FieldState(x, y) <> fsStone then |
||
363 | begin |
||
364 | result := false; |
||
365 | exit; |
||
366 | end; |
||
367 | |||
368 | result := true; |
||
369 | |||
370 | if MayJump(x, y, x+2, y) then exit; |
||
371 | if MayJump(x, y, x-2, y) then exit; |
||
372 | if MayJump(x, y, x, y+2) then exit; |
||
373 | if MayJump(x, y, x, y-2) then exit; |
||
374 | |||
375 | if AllowDiagonalMoves then |
||
376 | begin |
||
377 | if MayJump(x, y, x-2, y-2) then exit; |
||
378 | if MayJump(x, y, x+2, y-2) then exit; |
||
379 | if MayJump(x, y, x-2, y+2) then exit; |
||
380 | if MayJump(x, y, x+2, y+2) then exit; |
||
381 | end; |
||
382 | |||
383 | result := false; |
||
384 | end; |
||
385 | |||
386 | function TMainForm.AreJumpsPossible: boolean; |
||
387 | var |
||
388 | i, j: integer; |
||
389 | begin |
||
390 | result := false; |
||
391 | for i := Low(PlayGroundMatrix) to High(PlayGroundMatrix) do |
||
392 | begin |
||
393 | for j := Low(PlayGroundMatrix[i]) to High(PlayGroundMatrix[i]) do |
||
394 | begin |
||
395 | if CanJump(i, j) then |
||
396 | begin |
||
397 | result := true; |
||
398 | break; |
||
399 | end; |
||
400 | if result then break; |
||
401 | end; |
||
402 | end; |
||
403 | end; |
||
404 | |||
405 | procedure TMainForm.DoJump(SourceTag, DestTag: integer); |
||
406 | var |
||
407 | d, s: TPoint; |
||
408 | old_fieldtype: TFieldType; |
||
409 | res: Integer; |
||
410 | begin |
||
411 | if not MayJump(SourceTag, DestTag) then exit; |
||
412 | |||
413 | d := LookupFieldCoordinateArray[DestTag]; |
||
414 | s := LookupFieldCoordinateArray[SourceTag]; |
||
415 | |||
416 | JumpHistory.Add(Format(LNG_JUMP_LOG, [SourceTag+1, s.x+1, s.y+1, DestTag+1, d.x+1, d.y+1])); |
||
417 | |||
6 | daniel-mar | 418 | {$REGION 'Stein entfernen und Punkte vergeben'} |
1 | daniel-mar | 419 | if AllowDiagonalMoves then |
420 | begin |
||
421 | if (s.X-2 = d.X) and (s.Y-2 = d.Y) and (FieldState(s.X-1, s.Y-1) = fsStone) then RemoveStone(s.X-1, s.Y-1, true); |
||
422 | if (s.X-2 = d.X) and (s.Y+2 = d.Y) and (FieldState(s.X-1, s.Y+1) = fsStone) then RemoveStone(s.X-1, s.Y+1, true); |
||
423 | if (s.X+2 = d.X) and (s.Y-2 = d.Y) and (FieldState(s.X+1, s.Y-1) = fsStone) then RemoveStone(s.X+1, s.Y-1, true); |
||
424 | if (s.X+2 = d.X) and (s.Y+2 = d.Y) and (FieldState(s.X+1, s.Y+1) = fsStone) then RemoveStone(s.X+1, s.Y+1, true); |
||
425 | end; |
||
426 | |||
427 | if (s.X+2 = d.X) and (s.Y = d.Y) and (FieldState(s.X+1, s.Y ) = fsStone) then RemoveStone(s.X+1, s.Y, true); |
||
428 | if (s.X-2 = d.X) and (s.Y = d.Y) and (FieldState(s.X-1, s.Y ) = fsStone) then RemoveStone(s.X-1, s.Y, true); |
||
429 | if (s.X = d.X) and (s.Y+2 = d.Y) and (FieldState(s.X , s.Y+1) = fsStone) then RemoveStone(s.X, s.Y+1, true); |
||
430 | if (s.X = d.X) and (s.Y-2 = d.Y) and (FieldState(s.X , s.Y-1) = fsStone) then RemoveStone(s.X, s.Y-1, true); |
||
6 | daniel-mar | 431 | {$ENDREGION} |
1 | daniel-mar | 432 | |
433 | // Den Timer erst nach dem ersten Zug starten |
||
434 | // oder nach einer Pause neustarten |
||
435 | if not Timer.Enabled then |
||
436 | begin |
||
437 | MPauseTime.Enabled := true; |
||
438 | Timer.Enabled := true; |
||
439 | end; |
||
440 | |||
6 | daniel-mar | 441 | MRestartGame.Enabled := true; |
1 | daniel-mar | 442 | |
443 | // Sound abspielen |
||
444 | if MEnableSound.Checked then PlaySound(RES_JUMP, HInstance, SND_ASYNC or SND_NOWAIT or SND_RESOURCE); |
||
445 | |||
6 | daniel-mar | 446 | {$REGION 'Nun den Stein springen lassen'} |
1 | daniel-mar | 447 | old_fieldtype := PlayGroundMatrix[s.X, s.Y].FieldType; // Steinfarbe merken |
6 | daniel-mar | 448 | RemoveStone(s.X, s.Y, false); // Eigenen Stein entfernen. Keine Punkte zählen, da das unser eigener Stein ist, der springt |
1 | daniel-mar | 449 | PlayGroundMatrix[d.X, d.Y].FieldType := old_fieldtype; // Farbe wiederherstellen |
450 | LoadPictureForType(PlayGroundMatrix[d.X, d.Y].FieldType, PlayGroundMatrix[d.X, d.Y].Stone.Picture); // Stein an neue Position malen |
||
451 | StoneDraggingAllow(PlayGroundMatrix[d.X, d.Y].Stone, true); // Und die Drag-Eigenschaft erneuern |
||
6 | daniel-mar | 452 | {$ENDREGION} |
1 | daniel-mar | 453 | |
6 | daniel-mar | 454 | {$REGION 'Sind weitere Sprünge möglich oder ist das Spiel vorbei?'} |
1 | daniel-mar | 455 | if not AreJumpsPossible then |
456 | begin |
||
457 | MPauseTime.Enabled := false; |
||
458 | Timer.Enabled := false; |
||
459 | RefreshTime; |
||
4 | daniel-mar | 460 | if MEnableSound.Checked then |
461 | begin |
||
8 | daniel-mar | 462 | if LevelRemovedStones = LevelTotalStones-1 then |
463 | begin |
||
464 | if GoalStatus in [gsLastStoneInGoalRed, gsLastStoneInGoalYellow, gsLastStoneInGoalGreen] then |
||
465 | PlaySound(RES_WIN2, HInstance, SND_ASYNC or SND_NOWAIT or SND_RESOURCE) |
||
466 | else |
||
467 | PlaySound(RES_WIN1, HInstance, SND_ASYNC or SND_NOWAIT or SND_RESOURCE) |
||
468 | end |
||
4 | daniel-mar | 469 | else |
470 | PlaySound(RES_LOSE, HInstance, SND_ASYNC or SND_NOWAIT or SND_RESOURCE); |
||
471 | end; |
||
8 | daniel-mar | 472 | res := FinishForm.Execute(ExtractFileNameWithoutExt(LevelFile), Points, LevelTotalStones, LevelRemovedStones, CountedSeconds, GoalStatus, JumpHistory); |
1 | daniel-mar | 473 | if (res = mrOK) and FinishForm.ReplayCheckbox.Checked then RestartLevel; |
474 | end; |
||
6 | daniel-mar | 475 | {$ENDREGION} |
476 | |||
477 | SetLength(PrevPlaygroundMatrixes, Length(PrevPlaygroundMatrixes)+1); |
||
478 | PrevPlaygroundMatrixes[Length(PrevPlaygroundMatrixes)-1] := CloneMatrix(PlaygroundMatrix); |
||
479 | MUndo.Enabled := true; |
||
1 | daniel-mar | 480 | end; |
481 | |||
482 | function TMainForm.MayJump(SourceX, SourceY, DestX, DestY: integer): boolean; |
||
483 | begin |
||
484 | result := false; |
||
485 | |||
486 | // Check 1: Ist das Zielfeld überhaupt leer? |
||
487 | if FieldState(DestX, DestY) <> fsAvailable then exit; |
||
488 | |||
489 | // Check 2: Befindet sich ein Stein zwischen Source und Destination und ist der Abstand 2? |
||
490 | if AllowDiagonalMoves then |
||
491 | begin |
||
492 | if (SourceX-2 = DestX) and (SourceY-2 = DestY) and (FieldState(SourceX-1, SourceY-1) = fsStone) then result := true; |
||
493 | if (SourceX-2 = DestX) and (SourceY+2 = DestY) and (FieldState(SourceX-1, SourceY+1) = fsStone) then result := true; |
||
494 | if (SourceX+2 = DestX) and (SourceY-2 = DestY) and (FieldState(SourceX+1, SourceY-1) = fsStone) then result := true; |
||
495 | if (SourceX+2 = DestX) and (SourceY+2 = DestY) and (FieldState(SourceX+1, SourceY+1) = fsStone) then result := true; |
||
496 | end; |
||
497 | |||
498 | if (SourceX+2 = DestX) and (SourceY = DestY) and (FieldState(SourceX+1, SourceY ) = fsStone) then result := true; |
||
499 | if (SourceX-2 = DestX) and (SourceY = DestY) and (FieldState(SourceX-1, SourceY ) = fsStone) then result := true; |
||
500 | if (SourceX = DestX) and (SourceY+2 = DestY) and (FieldState(SourceX , SourceY+1) = fsStone) then result := true; |
||
501 | if (SourceX = DestX) and (SourceY-2 = DestY) and (FieldState(SourceX , SourceY-1) = fsStone) then result := true; |
||
502 | end; |
||
503 | |||
504 | function TMainForm.MayJump(SourceTag, DestTag: integer): boolean; |
||
505 | var |
||
506 | s, d: TPoint; |
||
507 | begin |
||
508 | d := LookupFieldCoordinateArray[DestTag]; |
||
509 | s := LookupFieldCoordinateArray[SourceTag]; |
||
510 | |||
511 | result := MayJump(s.X, s.Y, d.X, d.Y); |
||
512 | end; |
||
513 | |||
514 | procedure TMainForm.StoneDragDrop(Sender, Source: TObject; X, Y: Integer); |
||
515 | begin |
||
516 | DoJump(TComponent(Source).Tag, TComponent(Sender).Tag); |
||
517 | end; |
||
518 | |||
519 | procedure TMainForm.StoneDragOver(Sender, Source: TObject; X, |
||
520 | Y: Integer; State: TDragState; var Accept: Boolean); |
||
521 | begin |
||
522 | Accept := MayJump(TComponent(Source).Tag, TComponent(Sender).Tag); |
||
523 | end; |
||
524 | |||
525 | procedure TMainForm.DrawField(x, y: integer; t: TFieldProperties; halftabs: integer); |
||
526 | var |
||
527 | newField: TField; |
||
528 | index: integer; |
||
529 | begin |
||
530 | if (t.Typ = ftLocked) or (t.Typ = ftLockedWithTab) then exit; |
||
531 | |||
532 | index := Length(LookupFieldCoordinateArray); |
||
533 | |||
534 | newField.FieldType := t.Typ; |
||
535 | newField.Goal := t.Goal; |
||
536 | newField.Panel := DrawStoneBox(x, y, index, halftabs, t.Goal); |
||
537 | newField.Stone := DrawStone(t.Typ, newField.Panel); |
||
538 | if FieldState(t.Typ) = fsStone then Inc(LevelTotalStones); |
||
539 | |||
540 | SetLength(LookupFieldCoordinateArray, index + 1); |
||
541 | LookupFieldCoordinateArray[index].X := x; |
||
542 | LookupFieldCoordinateArray[index].Y := y; |
||
543 | |||
544 | if Length(PlayGroundMatrix) < x+1 then SetLength(PlayGroundMatrix, x+1); |
||
545 | if Length(PlayGroundMatrix[x]) < y+1 then SetLength(PlayGroundMatrix[x], y+1); |
||
546 | PlaygroundMatrix[x, y] := newField; |
||
547 | end; |
||
548 | |||
549 | function TMainForm.CloneMatrix(Source: TPlayGroundMatrix): TPlayGroundMatrix; |
||
550 | var |
||
551 | i, j: integer; |
||
552 | begin |
||
553 | SetLength(result, Length(Source)); |
||
554 | for i := Low(Source) to High(Source) do |
||
555 | begin |
||
556 | SetLength(result[i], Length(Source[i])); |
||
557 | for j := Low(Source[i]) to High(Source[i]) do |
||
558 | begin |
||
559 | result[i][j].FieldType := Source[i][j].FieldType; |
||
560 | result[i][j].Goal := Source[i][j].Goal; |
||
561 | result[i][j].Panel := Source[i][j].Panel; |
||
562 | result[i][j].Stone := Source[i][j].Stone; |
||
563 | end; |
||
564 | end; |
||
565 | end; |
||
566 | |||
567 | procedure TMainForm.BuildPlayground(LevelArray: TLevelArray); |
||
568 | var |
||
569 | i, j, halftabs, cur_x: integer; |
||
570 | max_x, max_y, old_cw, old_ch: integer; |
||
571 | begin |
||
572 | PlayGround.Visible := false; |
||
573 | |||
574 | // Die Dimensionen ermitteln |
||
575 | max_x := 0; |
||
576 | for i := Low(LevelArray) to High(LevelArray) do |
||
577 | begin |
||
578 | halftabs := 0; |
||
579 | for j := Low(LevelArray[i]) to High(LevelArray[i]) do |
||
580 | begin |
||
581 | if LevelArray[i][j].Typ = ftLockedWithTab then inc(halftabs); |
||
582 | DrawField(j, i, LevelArray[i][j], halftabs); |
||
583 | end; |
||
584 | cur_x := High(LevelArray[i]) + 1; |
||
585 | if cur_x > max_x then max_x := cur_x; |
||
586 | end; |
||
587 | max_y := High(LevelArray) + 1; |
||
588 | |||
589 | PlayGround.Visible := true; |
||
590 | |||
591 | // Die aktuellen Dimensionen merken |
||
592 | old_cw := ClientWidth; |
||
593 | old_ch := ClientHeight; |
||
594 | |||
595 | // Das Form an das Level anpassen |
||
596 | PlayGround.Width := MET_FIELD_SPACE + max_x * (MET_FIELD_SPACE + MET_FIELD_SIZE); |
||
597 | PlayGround.Height := MET_FIELD_SPACE + max_y * (MET_FIELD_SPACE + MET_FIELD_SIZE); |
||
598 | ClientWidth := 2 * MET_OUTER_MARGIN + PlayGround.Width; |
||
599 | ClientHeight := 2 * MET_OUTER_MARGIN + PlayGround.Height + Statistics.Height; |
||
600 | |||
601 | Statistics.Panels.Items[0].Width := Round(ClientWidth*MET_PERCENT_PNL_TIME); |
||
602 | Statistics.Panels.Items[1].Width := Round(ClientWidth*MET_PERCENT_PNL_STONES); |
||
603 | |||
604 | // Wenn sich das Form vergrößert oder verkleinert hat, neu justieren |
||
605 | if (old_cw <> ClientWidth) or (old_ch <> ClientHeight) then |
||
606 | begin |
||
607 | Left := Screen.Width div 2 - Width div 2; |
||
608 | Top := Screen.Height div 2 - Height div 2; |
||
609 | |||
610 | // Playground mittig setzen, falls die Mindestgröße für die |
||
611 | // Punkteanzeige unterschritten wurde, |
||
612 | PlayGround.Left := ClientWidth div 2 - PlayGround.Width div 2; |
||
613 | PlayGround.Top := ClientHeight div 2 - PlayGround.Height div 2; |
||
614 | end; |
||
615 | |||
616 | OriginalPlayGroundMatrix := CloneMatrix(PlayGroundMatrix); |
||
6 | daniel-mar | 617 | SetLength(PrevPlaygroundMatrixes,1); |
618 | PrevPlaygroundMatrixes[0] := CloneMatrix(PlayGroundMatrix); |
||
619 | MUndo.Enabled := false; |
||
1 | daniel-mar | 620 | end; |
621 | |||
622 | procedure TMainForm.TimerTimer(Sender: TObject); |
||
623 | begin |
||
624 | if mainform.Focused then Inc(CountedSeconds); |
||
625 | RefreshTime; |
||
626 | end; |
||
627 | |||
628 | function TMainForm.LevelTime: String; |
||
629 | begin |
||
630 | result := SecondsToTimeString(CountedSeconds); |
||
631 | end; |
||
632 | |||
633 | procedure TMainForm.NewGame(Filename: string); |
||
634 | var |
||
635 | LevelString: String; |
||
636 | LevelArray: TLevelArray; |
||
637 | begin |
||
638 | DestroyLevel; |
||
639 | LevelFile := Filename; |
||
640 | LevelString := ReadFile(LevelFile); |
||
641 | LevelArray := LevelStringToLevelArray(LevelString, true); |
||
642 | if Length(LevelArray) = 0 then Exit; |
||
643 | BuildPlayground(LevelArray); |
||
644 | if not AreJumpsPossible then |
||
645 | begin |
||
646 | ShowMessage(LNG_LVL_INVALID_NO_JUMP); |
||
647 | end; |
||
648 | RefreshTime; |
||
649 | RefreshStonesRemoved; |
||
650 | RefreshPoints; |
||
651 | end; |
||
652 | |||
653 | procedure TMainForm.MNewGameClick(Sender: TObject); |
||
654 | begin |
||
655 | LevelFile := AskForLevel; |
||
656 | if LevelFile <> '' then |
||
657 | begin |
||
658 | NewGame(LevelFile); |
||
659 | end; |
||
660 | end; |
||
661 | |||
662 | procedure TMainForm.MAboutClick(Sender: TObject); |
||
663 | begin |
||
664 | AboutBox.ShowModal; |
||
665 | end; |
||
666 | |||
667 | function TMainForm.AskForLevel: String; |
||
668 | begin |
||
669 | LevelChoice.ShowModal; |
||
670 | |||
671 | if LevelChoice.ModalResult <> mrOK then |
||
672 | begin |
||
673 | result := ''; |
||
674 | exit; |
||
675 | end; |
||
676 | |||
677 | result := LevelChoice.SelectedLevel; |
||
678 | end; |
||
679 | |||
680 | procedure TMainForm.FormShow(Sender: TObject); |
||
681 | begin |
||
682 | LevelFile := AskForLevel; |
||
683 | if LevelFile <> '' then |
||
684 | begin |
||
685 | NewGame(LevelFile); |
||
686 | end |
||
687 | else Close(); |
||
688 | end; |
||
689 | |||
8 | daniel-mar | 690 | function TMainForm.GoalStatus: TGoalStatus; |
691 | var |
||
692 | ft: TFieldType; |
||
693 | begin |
||
694 | if not MatrixHasGoal(PlaygroundMatrix) then |
||
695 | result := gsNoGoal |
||
696 | else if LevelRemovedStones < LevelTotalStones-1 then |
||
697 | Result := gsMultipleStonesRemaining |
||
698 | else |
||
699 | begin |
||
700 | ft := GoalFieldType(PlaygroundMatrix); |
||
701 | if ft = ftRed then |
||
702 | result := gsLastStoneInGoalRed |
||
703 | else if ft = ftYellow then |
||
704 | result := gsLastStoneInGoalYellow |
||
705 | else if ft = ftGreen then |
||
706 | result := gsLastStoneInGoalGreen; |
||
707 | end; |
||
708 | end; |
||
709 | |||
1 | daniel-mar | 710 | procedure TMainForm.FormCreate(Sender: TObject); |
711 | begin |
||
712 | JumpHistory := TStringList.Create; |
||
713 | LoadSettings; |
||
714 | end; |
||
715 | |||
716 | procedure TMainForm.FormDestroy(Sender: TObject); |
||
717 | begin |
||
718 | JumpHistory.Free; |
||
719 | end; |
||
720 | |||
721 | procedure TMainForm.MJumpHistoryClick(Sender: TObject); |
||
722 | begin |
||
723 | HistoryForm.JumpMemo.Lines.Assign(JumpHistory); |
||
724 | HistoryForm.ShowModal; |
||
725 | end; |
||
726 | |||
727 | procedure TMainForm.RestartLevel; |
||
728 | begin |
||
729 | MPauseTime.Enabled := false; |
||
730 | Timer.Enabled := false; |
||
731 | |||
732 | MRestartGame.Enabled := false; |
||
733 | |||
734 | CountedSeconds := 0; |
||
735 | RefreshTime; |
||
736 | |||
737 | Points := 0; |
||
738 | RefreshPoints; |
||
739 | |||
740 | LevelRemovedStones := 0; |
||
741 | RefreshStonesRemoved; |
||
742 | |||
743 | JumpHistory.Clear; |
||
744 | |||
745 | RedrawStonesFromMatrix(OriginalPlayGroundMatrix); |
||
746 | SetNewPlayGroundMatrix(OriginalPlayGroundMatrix); |
||
6 | daniel-mar | 747 | SetLength(PrevPlaygroundMatrixes,1); |
748 | PrevPlaygroundMatrixes[0] := CloneMatrix(OriginalPlayGroundMatrix); |
||
749 | MUndo.Enabled := false; |
||
1 | daniel-mar | 750 | end; |
751 | |||
752 | procedure TMainForm.SetNewPlayGroundMatrix(Matrix: TPlayGroundMatrix); |
||
753 | begin |
||
754 | ClearMatrix(PlayGroundMatrix, false); // Memory Leak verhindern |
||
755 | PlayGroundMatrix := CloneMatrix(Matrix); |
||
756 | end; |
||
757 | |||
758 | procedure TMainForm.MRestartGameClick(Sender: TObject); |
||
759 | begin |
||
760 | RestartLevel; |
||
761 | end; |
||
762 | |||
6 | daniel-mar | 763 | procedure TMainForm.MUndoClick(Sender: TObject); |
764 | var |
||
765 | PrevWorth: integer; |
||
766 | NewWorth: integer; |
||
767 | begin |
||
768 | if Length(PrevPlaygroundMatrixes) > 1 then |
||
769 | begin |
||
770 | PrevWorth := MatrixWorth(PrevPlaygroundMatrixes[Length(PrevPlaygroundMatrixes)-1]); |
||
771 | |||
772 | ClearMatrix(PrevPlaygroundMatrixes[Length(PrevPlaygroundMatrixes)-1], false); |
||
773 | SetLength(PrevPlaygroundMatrixes, Length(PrevPlaygroundMatrixes)-1); |
||
774 | |||
775 | NewWorth := MatrixWorth(PrevPlaygroundMatrixes[Length(PrevPlaygroundMatrixes)-1]); |
||
776 | RedrawStonesFromMatrix(PrevPlaygroundMatrixes[Length(PrevPlaygroundMatrixes)-1]); |
||
777 | SetNewPlayGroundMatrix(PrevPlaygroundMatrixes[Length(PrevPlaygroundMatrixes)-1]); |
||
778 | |||
779 | JumpHistory.Delete(JumpHistory.Count-1); |
||
780 | |||
781 | Dec(LevelRemovedStones); |
||
782 | RefreshStonesRemoved; |
||
783 | |||
784 | Dec(Points, NewWorth-PrevWorth); |
||
785 | RefreshPoints; |
||
7 | daniel-mar | 786 | |
787 | // Sound abspielen |
||
788 | if MEnableSound.Checked then PlaySound(RES_UNDO, HInstance, SND_ASYNC or SND_NOWAIT or SND_RESOURCE); |
||
6 | daniel-mar | 789 | end; |
790 | |||
791 | MUndo.Enabled := Length(PrevPlaygroundMatrixes) > 1; |
||
792 | end; |
||
793 | |||
1 | daniel-mar | 794 | procedure TMainForm.MHighScoresClick(Sender: TObject); |
795 | begin |
||
796 | HighScoreForm.Execute(ExtractFileNameWithoutExt(LevelFile)); |
||
797 | end; |
||
798 | |||
799 | procedure TMainForm.MPauseTimeClick(Sender: TObject); |
||
800 | begin |
||
801 | MPauseTime.Enabled := false; |
||
802 | Timer.Enabled := false; |
||
803 | end; |
||
804 | |||
805 | procedure TMainForm.LoadSettings; |
||
806 | var |
||
807 | reg: TRegistry; |
||
808 | begin |
||
809 | reg := TRegistry.Create; |
||
810 | try |
||
811 | reg.RootKey := HKEY_CURRENT_USER; |
||
812 | if reg.OpenKeyReadOnly(REG_KEY) then |
||
813 | begin |
||
814 | if reg.ValueExists(REG_SOUND) then |
||
815 | MEnableSound.Checked := reg.ReadBool(REG_SOUND); |
||
816 | reg.CloseKey; |
||
817 | end; |
||
818 | finally |
||
819 | reg.Free; |
||
820 | end; |
||
821 | end; |
||
822 | |||
823 | procedure TMainForm.SaveSettings; |
||
824 | var |
||
825 | reg: TRegistry; |
||
826 | begin |
||
827 | reg := TRegistry.Create; |
||
828 | try |
||
829 | reg.RootKey := HKEY_CURRENT_USER; |
||
830 | if reg.OpenKey(REG_KEY, true) then |
||
831 | begin |
||
832 | reg.WriteBool(REG_SOUND, MEnableSound.Checked); |
||
833 | reg.CloseKey; |
||
834 | end; |
||
835 | finally |
||
836 | reg.Free; |
||
837 | end; |
||
838 | end; |
||
839 | |||
840 | |||
841 | procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction); |
||
842 | begin |
||
843 | SaveSettings; |
||
844 | if FinishForm.NameEdit.Text <> '' then |
||
845 | begin |
||
846 | FinishForm.SaveSettings; |
||
847 | end; |
||
848 | end; |
||
849 | |||
850 | procedure TMainForm.MHelpClick(Sender: TObject); |
||
851 | begin |
||
852 | HelpForm.ShowModal; |
||
853 | end; |
||
854 | |||
855 | procedure TMainForm.MEnableSoundClick(Sender: TObject); |
||
856 | begin |
||
857 | MEnableSound.Checked := not MEnableSound.Checked; |
||
858 | end; |
||
859 | |||
860 | end. |