-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathunit1.pas
483 lines (445 loc) · 16.5 KB
/
unit1.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
(******************************************************************************)
(* 2048 ??.??.???? *)
(* *)
(* Version : 0.01 *)
(* *)
(* Author : Uwe Schächterle (Corpsman) *)
(* *)
(* Support : www.Corpsman.de *)
(* *)
(* Description : Implementation of the 2048 Game *)
(* *)
(* License : See the file license.md, located under: *)
(* https://github.com/PascalCorpsman/Software_Licenses/blob/main/license.md *)
(* for details about the license. *)
(* *)
(* It is not allowed to change or remove this text from any *)
(* source file of the project. *)
(* *)
(* Warranty : There is no warranty, neither in correctness of the *)
(* implementation, nor anything other that could happen *)
(* or go wrong, use at your own risk. *)
(* *)
(* Known Issues: none *)
(* *)
(* History : 0.01 - Initial version *)
(* *)
(******************************************************************************)
Unit Unit1;
{$MODE objfpc}{$H+}
{$DEFINE DebuggMode} // OpenGL Debugg Meldungen Abfangen und anzeigen.
{$DEFINE UseAILib} // Wenn Aktiv, dann kann mittels ai.dll eine KI nachgeladen werden.
Interface
Uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
ExtCtrls,
OpenGlcontext, LCLType, Menus, IniFiles, math,
dglOpenGL // http://wiki.delphigl.com/index.php/dglOpenGL.pas
, u2048
{$IFDEF UseAILib}
, dynlibs
{$ENDIF}
;
Type
{ TForm1 }
TForm1 = Class(TForm)
ApplicationProperties1: TApplicationProperties;
MenuItem1: TMenuItem;
MenuItem2: TMenuItem;
OpenDialog1: TOpenDialog;
OpenGLControl1: TOpenGLControl;
PopupMenu1: TPopupMenu;
SaveDialog1: TSaveDialog;
Timer1: TTimer;
Timer2: TTimer;
Procedure ApplicationProperties1Idle(Sender: TObject; Var Done: Boolean);
Procedure FormCloseQuery(Sender: TObject; Var CanClose: boolean);
Procedure FormCreate(Sender: TObject);
Procedure MenuItem1Click(Sender: TObject);
Procedure MenuItem2Click(Sender: TObject);
Procedure OpenGLControl1KeyDown(Sender: TObject; Var Key: Word;
Shift: TShiftState);
Procedure OpenGLControl1KeyUp(Sender: TObject; Var Key: Word;
Shift: TShiftState);
Procedure OpenGLControl1MakeCurrent(Sender: TObject; Var Allow: boolean);
Procedure OpenGLControl1Paint(Sender: TObject);
Procedure OpenGLControl1Resize(Sender: TObject);
Procedure Timer1Timer(Sender: TObject);
Procedure Timer2Timer(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
Procedure Go2d();
Procedure Exit2d();
End;
TField = Array Of Array Of int64;
TAiMove = Function(data: TField; Boardsize: integer): integer; cdecl;
TInfo = Function(): PChar; cdecl;
Var
Form1: TForm1;
Initialized: Boolean = false; // Wenn True dann ist OpenGL initialisiert
Board: TBoard;
maxp: int64 = 0;
KeyDebounce: Boolean = True;
KeyPressedDown: Boolean = false;
blocked: Boolean = false;
AiMove: TAiMove = Nil;
info: TInfo = Nil;
lib: TLibHandle = 0;
finMessage: boolean;
Implementation
{$R *.lfm}
Uses LazUTF8;
Var
ini: Tinifile;
{ TForm1 }
Procedure Tform1.Go2d();
Begin
glMatrixMode(GL_PROJECTION);
glPushMatrix(); // Store The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
glOrtho(0, OpenGLControl1.Width, OpenGLControl1.height, 0, -1, 1); // Set Up An Ortho Screen
glMatrixMode(GL_MODELVIEW);
glPushMatrix(); // Store old Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
End;
Procedure Tform1.Exit2d();
Begin
glMatrixMode(GL_PROJECTION);
glPopMatrix(); // Restore old Projection Matrix
glMatrixMode(GL_MODELVIEW);
glPopMatrix(); // Restore old Projection Matrix
End;
Var
allowcnt: Integer = 0;
Procedure TForm1.OpenGLControl1MakeCurrent(Sender: TObject; Var Allow: boolean);
Begin
If allowcnt > 2 Then Begin
exit;
End;
inc(allowcnt);
// Sollen Dialoge beim Starten ausgeführt werden ist hier der Richtige Zeitpunkt
If allowcnt = 1 Then Begin
// Init dglOpenGL.pas , Teil 2
ReadExtensions; // Anstatt der Extentions kann auch nur der Core geladen werden. ReadOpenGLCore;
ReadImplementationProperties;
End;
If allowcnt = 2 Then Begin // Dieses If Sorgt mit dem obigen dafür, dass der Code nur 1 mal ausgeführt wird.
glEnable(GL_DEPTH_TEST); // Tiefentest
glDepthFunc(gl_less);
// Der Anwendung erlauben zu Rendern.
Initialized := True;
If Not fileexists(IncludeTrailingPathDelimiter(ExtractFilePath(ParamStrUTF8(0))) + 'font.ofnt') Then Begin
showmessage('Error could not find : ' + IncludeTrailingPathDelimiter(ExtractFilePath(ParamStrUTF8(0))) + 'font.ofnt');
halt;
End;
CreateFont(IncludeTrailingPathDelimiter(ExtractFilePath(ParamStrUTF8(0))) + 'font.ofnt');
Board := TBoard.Create;
OpenGLControl1Resize(Nil);
End;
Form1.Invalidate;
End;
Procedure TForm1.OpenGLControl1Paint(Sender: TObject);
Begin
If Not Initialized Then Exit;
// Render Szene
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
go2d;
OpenGlFont.Size := 40;
OpenGlFont.Color := clWhite;
maxp := max(maxp, board.Points);
OpenGlFont.Textout(10, 10, format('Points : %d max Points : %d', [board.Points, maxp]));
glTranslatef(0, 100, 0);
board.Render;
exit2d;
OpenGLControl1.SwapBuffers;
// Game Over ?
If board.IsFull And Not (blocked) Then Begin
timer2.enabled := false;
blocked := true;
finMessage := true;
End;
End;
Procedure TForm1.OpenGLControl1Resize(Sender: TObject);
Begin
If Initialized Then Begin
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, OpenGLControl1.Width, OpenGLControl1.Height);
gluPerspective(45.0, OpenGLControl1.Width / OpenGLControl1.Height, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
board.Resize(OpenGLControl1.Width, OpenGLControl1.Height - 100);
End;
End;
Procedure TForm1.FormCreate(Sender: TObject);
Begin
finMessage := false;
// Init dglOpenGL.pas , Teil 1
If Not InitOpenGl Then Begin
showmessage('Error, could not init dglOpenGL.pas');
Halt;
End;
(*
60 - FPS entsprechen
0.01666666 ms
Ist Interval auf 16 hängt das gesamte system, bei 17 nicht.
Generell sollte die Interval Zahl also dynamisch zum Rechenaufwand, mindestens aber immer 17 sein.
*)
Timer1.Interval := 17;
OpenGLControl1.Align := alClient;
Randomize;
(*
* Historie : 0.01 = Initial Version
* 0.02 = Improvements in End-Game detection, added "ai-player", editable falltime
* 0.03 = Einfügen "u" option
*)
caption := '2048 ver.: 0.02 remake by Corpsman, www.Corpsman.de';
Constraints.MinHeight := 280;
Constraints.MinWidth := 180;
ini := TIniFile.Create(IncludeTrailingPathDelimiter(ExtractFilePath(ParamStrUTF8(0))) + 'Settings.ini');
BoardSize := ini.ReadInteger('General', 'Boardsize', BoardSize + 1) - 1;
FallTime := ini.ReadInteger('General', 'FallTime', FallTime);
maxp := StrToInt64(ini.ReadString(format('Board%dx%d', [BoardSize + 1, BoardSize + 1]), 'MaxPoints', '0'));
OpenDialog1.InitialDir := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStrUTF8(0)));
SaveDialog1.InitialDir := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStrUTF8(0)));
PieceColors[0].BackColor := ini.ReadInteger('Color', 'b', PieceColors[0].BackColor);
//PieceColors[0].FontColor := ini.ReadInteger('Color', 'f', PieceColors[0].FontColor); // Macht keinen Sinn
PieceColors[1].BackColor := ini.ReadInteger('Color', '2b', PieceColors[1].BackColor);
PieceColors[1].FontColor := ini.ReadInteger('Color', '2f', PieceColors[1].FontColor);
PieceColors[2].BackColor := ini.ReadInteger('Color', '4b', PieceColors[2].BackColor);
PieceColors[2].FontColor := ini.ReadInteger('Color', '4f', PieceColors[2].FontColor);
PieceColors[3].BackColor := ini.ReadInteger('Color', '8b', PieceColors[3].BackColor);
PieceColors[3].FontColor := ini.ReadInteger('Color', '8f', PieceColors[3].FontColor);
PieceColors[4].BackColor := ini.ReadInteger('Color', '16b', PieceColors[4].BackColor);
PieceColors[4].FontColor := ini.ReadInteger('Color', '16f', PieceColors[4].FontColor);
PieceColors[5].BackColor := ini.ReadInteger('Color', '32b', PieceColors[5].BackColor);
PieceColors[5].FontColor := ini.ReadInteger('Color', '32f', PieceColors[5].FontColor);
PieceColors[6].BackColor := ini.ReadInteger('Color', '64b', PieceColors[6].BackColor);
PieceColors[6].FontColor := ini.ReadInteger('Color', '64f', PieceColors[6].FontColor);
PieceColors[7].BackColor := ini.ReadInteger('Color', '128b', PieceColors[7].BackColor);
PieceColors[7].FontColor := ini.ReadInteger('Color', '128f', PieceColors[7].FontColor);
PieceColors[8].BackColor := ini.ReadInteger('Color', '256b', PieceColors[8].BackColor);
PieceColors[8].FontColor := ini.ReadInteger('Color', '256f', PieceColors[8].FontColor);
PieceColors[9].BackColor := ini.ReadInteger('Color', '512b', PieceColors[9].BackColor);
PieceColors[9].FontColor := ini.ReadInteger('Color', '512f', PieceColors[9].FontColor);
PieceColors[10].BackColor := ini.ReadInteger('Color', '1024b', PieceColors[10].BackColor);
PieceColors[10].FontColor := ini.ReadInteger('Color', '1024f', PieceColors[10].FontColor);
PieceColors[11].BackColor := ini.ReadInteger('Color', '2048b', PieceColors[11].BackColor);
PieceColors[11].FontColor := ini.ReadInteger('Color', '2048f', PieceColors[11].FontColor);
PieceColors[12].BackColor := ini.ReadInteger('Color', '4096b', PieceColors[12].BackColor);
PieceColors[12].FontColor := ini.ReadInteger('Color', '4096f', PieceColors[12].FontColor);
KeyDebounce := ini.ReadBool('General', 'KeyDebounce', KeyDebounce);
OpenGLControl1.Invalidate;
End;
Procedure TForm1.FormCloseQuery(Sender: TObject; Var CanClose: boolean);
Begin
Initialized := false;
Board.Free;
ini.WriteString(format('Board%dx%d', [BoardSize + 1, BoardSize + 1]), 'MaxPoints', inttostr(maxp));
ini.free;
If lib <> 0 Then Begin
AiMove := Nil;
UnloadLibrary(lib);
lib := 0;
End;
End;
Procedure TForm1.ApplicationProperties1Idle(Sender: TObject; Var Done: Boolean);
Begin
If finMessage Then Begin
finMessage := false;
If ID_YES = Application.MessageBox('Game over, new game?', 'Question', MB_YESNO Or MB_ICONQUESTION) Then Begin
board.restart;
blocked := false;
End
Else Begin
// close;
End;
End;
End;
Procedure TForm1.MenuItem1Click(Sender: TObject);
Var
f: TFileStream;
Begin
// Load Board
If OpenDialog1.Execute Then Begin
// Alte Board Max Sichern
ini.WriteString(format('Board%dx%d', [BoardSize + 1, BoardSize + 1]), 'MaxPoints', inttostr(maxp));
// Board Laden
f := TFileStream.Create(OpenDialog1.FileName, fmopenread);
Board.LoadFromStream(f);
// Neue Board Max Laden
maxp := StrToInt64(ini.ReadString(format('Board%dx%d', [BoardSize + 1, BoardSize + 1]), 'MaxPoints', '0'));
f.free;
End;
End;
Procedure TForm1.MenuItem2Click(Sender: TObject);
Var
f: TFileStream;
Begin
// Save Board
If SaveDialog1.Execute Then Begin
f := TFileStream.Create(SaveDialog1.FileName, fmcreate Or fmopenwrite);
Board.SaveToStream(f);
f.free;
End;
End;
Procedure TForm1.OpenGLControl1KeyDown(Sender: TObject; Var Key: Word;
Shift: TShiftState);
Var
s: String;
Begin
If key = VK_ESCAPE Then close;
If key = vk_F12 Then close; // Boss Key
If key = vk_A Then Begin
{$IFDEF UseAILib}
If lib = 0 Then Begin
s := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStrUTF8(0))) + {$IFDEF windows} 'ai.dll'{$ELSE} 'libai.so'{$ENDIF};
lib := LoadLibrary(s);
If lib = 0 Then Begin
showmessage('Error unable to load : ' + LineEnding + LineEnding + s);
exit;
End;
AiMove := TAiMove(GetProcAddress(lib, 'AiMove'));
If Not assigned(aimove) Then Begin
showmessage('Error could not load "AiMove" in ' + LineEnding + LineEnding + s);
UnloadLibrary(lib);
exit;
End;
info := TInfo(GetProcAddress(lib, 'Info'));
If Not assigned(info) Then Begin
showmessage('Error could not load "Info" in ' + LineEnding + LineEnding + s);
UnloadLibrary(lib);
exit;
End;
End;
timer2.Enabled := Not timer2.Enabled;
{$ENDIF}
End;
If key = VK_u Then Begin
{$IFDEF UseAILib}
If lib <> 0 Then Begin
UnloadLibrary(lib);
lib := 0;
info := Nil;
aimove := Nil;
showmessage('Unload ai, sucessfully done.');
End;
{$ENDIF}
End;
If key = vk_i Then Begin
{$IFDEF UseAILib}
If lib = 0 Then Begin
s := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStrUTF8(0))) + {$IFDEF Windows} 'ai.dll'{$ELSE} 'libai.so'{$ENDIF};
lib := LoadLibrary(s);
If lib = 0 Then Begin
showmessage('Error unable to load : ' + LineEnding + LineEnding + s);
exit;
End;
AiMove := TAiMove(GetProcAddress(lib, 'AiMove'));
If Not assigned(aimove) Then Begin
showmessage('Error could not load "AiMove" in ' + LineEnding + LineEnding + s);
UnloadLibrary(lib);
exit;
End;
info := TInfo(GetProcAddress(lib, 'Info'));
If Not assigned(info) Then Begin
showmessage('Error could not load "Info" in ' + LineEnding + LineEnding + s);
UnloadLibrary(lib);
exit;
End;
End;
showmessage(info());
{$ENDIF}
End;
If key = vk_n Then Begin
board.restart;
blocked := false;
End;
If KeyDebounce And KeyPressedDown Then exit;
If Key = vk_down Then Begin
Board.StartFalling(dDown);
End;
If Key = vk_Up Then Begin
Board.StartFalling(dup);
End;
If Key = vk_left Then Begin
Board.StartFalling(dleft);
End;
If Key = vk_right Then Begin
Board.StartFalling(dright);
End;
KeyPressedDown := True;
End;
Procedure TForm1.OpenGLControl1KeyUp(Sender: TObject; Var Key: Word;
Shift: TShiftState);
Begin
KeyPressedDown := false;
End;
Procedure TForm1.Timer1Timer(Sender: TObject);
{$IFDEF DebuggMode}
Var
i: Cardinal;
p: Pchar;
{$ENDIF}
Begin
If Initialized Then Begin
OpenGLControl1.Invalidate;
{$IFDEF DebuggMode}
i := glGetError();
If i <> 0 Then Begin
Timer1.Enabled := false;
p := gluErrorString(i);
showmessage('OpenGL Error (' + inttostr(i) + ') occured.' + #13#13 +
'OpenGL Message : "' + p + '"'#13#13 +
'Applikation will be terminated.');
close;
End;
{$ENDIF}
End;
End;
Var
ai_Retries: integer = 0;
Procedure TForm1.Timer2Timer(Sender: TObject);
Var
b: Boolean;
key: Word;
f: TField;
i: Integer;
j: Integer;
Begin
If Not assigned(AiMove) Then Begin
timer2.enabled := false;
showmessage('Error, no ai callback found.');
exit;
End;
b := Not Board.IsFalling;
If b Then Begin
KeyPressedDown := false;
setlength(f, BoardSize + 1, BoardSize + 1);
For i := 0 To BoardSize Do
For j := 0 To BoardSize Do
f[i, j] := Board.index(i, j);
key := AiMove(f, BoardSize + 1);
If key <> 0 Then
OpenGLControl1KeyDown(Nil, Key, []);
If Not Board.IsFalling Then Begin
inc(ai_Retries);
// Wenn 60 mal nichts ging (ca. 3s Zeit), dann wird die Ki, zwangs abgebrochen
If (ai_Retries > 60) Then Begin
timer2.Enabled := false;
showmessage('Ai gave 60 times an invalid command, ai will now be deactivated.');
End;
End
Else Begin
ai_Retries := 0;
End;
If key = 0 Then Begin
timer2.Enabled := false;
showmessage('Ai gave up.');
End;
End;
End;
End.