Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /Unit1.pas

Parent Directory Parent Directory | Revision Log Revision Log


Revision 14 - (hide annotations) (download) (as text)
Thu Jul 16 09:39:13 2015 UTC (8 years, 7 months ago) by yamat0jp
File MIME type: text/x-pascal
File size: 14523 byte(s)
テスト

3Dを取り入れた
1 yamat0jp 5 unit Unit1;
2 yamat0jp 1
3     interface
4    
5     uses
6 yamat0jp 5 System.SysUtils, System.Types, System.UITypes, System.Classes,
7     System.Variants,
8     FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Menus,
9     System.Math, FMX.Objects, FMX.StdCtrls;
10 yamat0jp 1
11     const
12     Count = 8;
13    
14     type
15 yamat0jp 14 TStoneType = (stNone, stWhite, stBlack, stError, stEffect);
16 yamat0jp 1
17 yamat0jp 14 TEffectData = record
18     X, Y: integer;
19     Left, Top: integer;
20     Stone: TStoneType;
21     end;
22    
23 yamat0jp 3 TGridData = array [0 .. Count - 1] of array [0 .. Count - 1] of TStoneType;
24 yamat0jp 1
25     TPlayer = class
26     private
27     FAuto: Boolean;
28 yamat0jp 4 FStone: TStoneType;
29 yamat0jp 1 public
30     property Auto: Boolean read FAuto write FAuto;
31 yamat0jp 4 property Stone: TStoneType read FStone write FStone;
32 yamat0jp 1 end;
33    
34     TStoneGrid = class
35     private
36     FStrings: TGridData;
37 yamat0jp 7 FBuffer: array [0 .. Count * Count - 4] of TGridData;
38 yamat0jp 1 FTurnNumber: integer;
39     FTurnIndex: integer;
40 yamat0jp 9 FActive: Boolean;
41 yamat0jp 14 List: TList;
42     FIndex_X: integer;
43     FIndex_Y: integer;
44 yamat0jp 1 function GetStrings(X, Y: integer): TStoneType;
45     procedure SetStrings(X, Y: integer; const Value: TStoneType);
46     procedure SetTurnNumber(const Value: integer);
47     public
48 yamat0jp 14 constructor Create;
49     destructor Destroy; override;
50 yamat0jp 1 procedure Clear;
51 yamat0jp 10 function CalScore(Stone: TStoneType; X, Y: integer): integer;
52     function CanSetStone(Stone: TStoneType; X, Y: integer; Reverse: Boolean;
53 yamat0jp 7 const Visible: Boolean = false): Boolean;
54 yamat0jp 10 function NextStone(Stone: TStoneType): TPoint;
55 yamat0jp 9 procedure Start;
56     procedure Restart;
57     procedure Pause;
58 yamat0jp 14 function ListExecute: Boolean;
59     procedure Paint(Canvas: TCanvas);
60     procedure ImageCount(X, Y: integer);
61 yamat0jp 3 property Strings[X, Y: integer]: TStoneType read GetStrings
62     write SetStrings; default;
63 yamat0jp 1 property TurnNumber: integer read FTurnNumber write SetTurnNumber;
64 yamat0jp 10 property Active: Boolean read FActive;
65 yamat0jp 1 end;
66    
67     TForm1 = class(TForm)
68     Timer1: TTimer;
69     MainMenu1: TMainMenu;
70 yamat0jp 5 MenuItem1: TMenuItem;
71     MenuItem2: TMenuItem;
72     MenuItem3: TMenuItem;
73     MenuItem4: TMenuItem;
74     MenuItem5: TMenuItem;
75     MenuItem6: TMenuItem;
76     MenuItem7: TMenuItem;
77     PaintBox1: TPaintBox;
78 yamat0jp 7 MenuItem8: TMenuItem;
79     MenuItem9: TMenuItem;
80     MenuItem10: TMenuItem;
81     MenuItem11: TMenuItem;
82     MenuItem12: TMenuItem;
83 yamat0jp 14 Timer2: TTimer;
84     Image1: TImage;
85     Image2: TImage;
86     Image3: TImage;
87     Image4: TImage;
88 yamat0jp 1 procedure FormCreate(Sender: TObject);
89     procedure FormDestroy(Sender: TObject);
90     procedure Timer1Timer(Sender: TObject);
91     procedure FormResize(Sender: TObject);
92 yamat0jp 5 procedure MenuItem4Click(Sender: TObject);
93     procedure MenuItem2Click(Sender: TObject);
94 yamat0jp 7 procedure PaintBox1Tap(Sender: TObject; const Point: TPointF);
95     procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
96 yamat0jp 5 Shift: TShiftState; X, Y: Single);
97     procedure PaintBox1Paint(Sender: TObject; Canvas: TCanvas);
98     procedure MenuItem6Click(Sender: TObject);
99 yamat0jp 7 procedure PaintBox1Resize(Sender: TObject);
100     procedure MenuItem8Click(Sender: TObject);
101     procedure MenuItem10Click(Sender: TObject);
102     procedure MenuItem11Click(Sender: TObject);
103 yamat0jp 14 procedure Timer2Timer(Sender: TObject);
104 yamat0jp 1 private
105 yamat0jp 5 { Private ���� }
106 yamat0jp 1 StoneGrid: TStoneGrid;
107     Index: TPlayer;
108     Size: integer;
109     procedure CompStone;
110     procedure GameStart;
111     procedure ChangePlayer;
112     public
113 yamat0jp 5 { Public ���� }
114 yamat0jp 1 end;
115    
116     var
117     Player1: TPlayer;
118     Player2: TPlayer;
119    
120     Form1: TForm1;
121    
122     implementation
123    
124 yamat0jp 5 {$R *.fmx}
125 yamat0jp 7 {$R *.Windows.fmx MSWINDOWS}
126 yamat0jp 1 { TStoneGrid }
127    
128 yamat0jp 10 function TStoneGrid.CalScore(Stone: TStoneType; X, Y: integer): integer;
129 yamat0jp 1 var
130     i, j: integer;
131     begin
132 yamat0jp 10 if CanSetStone(Stone, X, Y, true) = true then
133 yamat0jp 1 begin
134 yamat0jp 10 if Stone = stBlack then
135     Stone := stWhite
136 yamat0jp 3 else
137 yamat0jp 10 Stone := stBlack;
138 yamat0jp 3 result := 0;
139     for i := 0 to Count - 1 do
140     for j := 0 to Count - 1 do
141 yamat0jp 10 if CanSetStone(Stone, i, j, false) = true then
142 yamat0jp 3 inc(result);
143 yamat0jp 9 FStrings := FBuffer[FTurnIndex];
144 yamat0jp 3 end
145     else
146 yamat0jp 1 begin
147 yamat0jp 9 FStrings := FBuffer[FTurnIndex];
148 yamat0jp 3 result := -1;
149 yamat0jp 1 end;
150     end;
151    
152 yamat0jp 10 function TStoneGrid.CanSetStone(Stone: TStoneType; X, Y: integer;
153 yamat0jp 7 Reverse: Boolean; const Visible: Boolean): Boolean;
154 yamat0jp 1 var
155 yamat0jp 14 i: integer;
156 yamat0jp 4 p: Boolean;
157 yamat0jp 14 q: ^TEffectData;
158 yamat0jp 4 procedure Method(m, n: integer);
159 yamat0jp 3 var
160 yamat0jp 4 s: TStoneType;
161 yamat0jp 3 j: integer;
162 yamat0jp 1 begin
163 yamat0jp 4 if p = false then
164     Exit;
165     i := 1;
166 yamat0jp 3 while true do
167 yamat0jp 4 begin
168     s := GetStrings(X + m * i, Y + n * i);
169     if (s = stNone) or (s = stError) then
170     break
171 yamat0jp 10 else if s = Stone then
172 yamat0jp 4 if i > 1 then
173     begin
174     result := true;
175     if Reverse = true then
176 yamat0jp 1 begin
177 yamat0jp 4 for j := 1 to i - 1 do
178 yamat0jp 7 begin
179 yamat0jp 14 if Visible = true then
180     begin
181     New(q);
182     q^.Left := X + m * j;
183     q^.Top := Y + n * j;
184     q^.Stone := Stone;
185     q^.X := 0;
186     q^.Y := 0;
187     List.Add(q);
188     SetStrings(q^.Left, q^.Top, stEffect);
189     Sleep(20);
190     end
191     else
192     SetStrings(X + m * j, Y + n * j, Stone);
193 yamat0jp 7 end;
194 yamat0jp 4 break;
195 yamat0jp 3 end
196     else
197 yamat0jp 1 begin
198 yamat0jp 4 p := false;
199 yamat0jp 3 break;
200 yamat0jp 4 end;
201     end
202     else
203     break
204 yamat0jp 3 else
205 yamat0jp 4 inc(i);
206     end;
207 yamat0jp 3 end;
208    
209     begin
210 yamat0jp 14 FActive := false;
211     result := false;
212     p := true;
213     if GetStrings(X, Y) = stNone then
214 yamat0jp 9 begin
215 yamat0jp 14 Method(-1, -1);
216     Method(-1, 0);
217     Method(-1, 1);
218     Method(0, -1);
219     Method(0, 1);
220     Method(1, -1);
221     Method(1, 0);
222     Method(1, 1);
223     if (Reverse = true) and (result = true) then
224     SetStrings(X, Y, Stone);
225 yamat0jp 9 end;
226 yamat0jp 1 end;
227    
228     procedure TStoneGrid.Clear;
229     var
230     i, j: integer;
231     begin
232 yamat0jp 3 for i := 0 to Count - 1 do
233     for j := 0 to Count - 1 do
234     Strings[i, j] := stNone;
235     Strings[3, 3] := stBlack;
236     Strings[4, 4] := stBlack;
237     Strings[4, 3] := stWhite;
238     Strings[3, 4] := stWhite;
239 yamat0jp 7 FTurnNumber := 0;
240     FTurnIndex := 0;
241 yamat0jp 10 FBuffer[0] := FStrings;
242 yamat0jp 1 end;
243    
244 yamat0jp 14 constructor TStoneGrid.Create;
245     begin
246     inherited;
247     List := TList.Create;
248     end;
249    
250     destructor TStoneGrid.Destroy;
251     var
252     i: integer;
253     begin
254     for i := 0 to List.Count - 1 do
255     Dispose(List[i]);
256     List.Free;
257     inherited;
258     end;
259    
260 yamat0jp 1 function TStoneGrid.GetStrings(X, Y: integer): TStoneType;
261     begin
262 yamat0jp 3 if (X >= 0) and (X < Count) and (Y >= 0) and (Y < Count) then
263 yamat0jp 4 result := FStrings[X, Y]
264 yamat0jp 3 else
265     result := stError;
266 yamat0jp 1 end;
267    
268 yamat0jp 14 procedure TStoneGrid.ImageCount(X, Y: integer);
269     begin
270     FIndex_X := X;
271     FIndex_Y := Y;
272     end;
273    
274     function TStoneGrid.ListExecute: Boolean;
275     var
276     p: ^TEffectData;
277     i: integer;
278     begin
279     if List.Count = 0 then
280     result := false
281     else
282     begin
283     for i := 0 to List.Count - 1 do
284     begin
285     p := List.List[i];
286     if p^.X < FIndex_X - 1 then
287     p^.X := p^.X + 1
288     else if p^.Y < FIndex_Y - 1 then
289     begin
290     p^.X := 0;
291     p^.Y := p^.Y + 1;
292     end
293     else
294     begin
295     SetStrings(p^.Left, p^.Top, p^.Stone);
296     Dispose(p);
297     List[i] := nil;
298     end;
299     end;
300     for i := List.Count - 1 downto 0 do
301     if List[i] = nil then
302     List.Delete(i);
303     if List.Count = 0 then
304     begin
305     FActive := true;
306     inc(FTurnIndex);
307     inc(FTurnNumber);
308     FBuffer[FTurnIndex] := FStrings;
309     end;
310     result := true;
311     end;
312     end;
313    
314 yamat0jp 10 function TStoneGrid.NextStone(Stone: TStoneType): TPoint;
315 yamat0jp 1 var
316     i, j, m, n: integer;
317     begin
318 yamat0jp 3 n := -1;
319     for i := 0 to Count - 1 do
320     for j := 0 to Count - 1 do
321 yamat0jp 1 begin
322 yamat0jp 10 m := CalScore(Stone, i, j);
323 yamat0jp 3 if (n = -1) or ((m > -1) and (n > m)) then
324 yamat0jp 1 begin
325 yamat0jp 3 n := m;
326     result := Point(i, j);
327 yamat0jp 1 end;
328     end;
329     if n = -1 then
330 yamat0jp 3 result := Point(-1, -1);
331 yamat0jp 1 end;
332    
333 yamat0jp 14 procedure TStoneGrid.Paint(Canvas: TCanvas);
334     var
335     i: integer;
336     k, m, n: integer;
337     s: TBitmap;
338     p: ^TEffectData;
339     begin
340     m := Form1.Image3.Bitmap.Width;
341     n := Form1.Image3.Bitmap.Height;
342     k:=Form1.Size;
343     for i := 0 to List.Count - 1 do
344     begin
345     p := List[i];
346     if p^.Stone = stBlack then
347     s := Form1.Image1.Bitmap
348     else
349     s := Form1.Image2.Bitmap;
350     Canvas.DrawBitmap(s, RectF(p^.X * m, p^.Y * n, (p^.X + 1) * m,
351     (p^.Y + 1) * n), RectF(p^.Left * k, p^.Top * k, (p^.Left + 1) * k,
352     (p^.Top + 1) * k), 1);
353     end;
354     end;
355    
356 yamat0jp 9 procedure TStoneGrid.Pause;
357     begin
358 yamat0jp 10 FActive := false;
359 yamat0jp 9 end;
360    
361     procedure TStoneGrid.Restart;
362     begin
363 yamat0jp 10 FActive := true;
364     FTurnIndex := FTurnNumber;
365 yamat0jp 9 end;
366    
367 yamat0jp 1 procedure TStoneGrid.SetStrings(X, Y: integer; const Value: TStoneType);
368     begin
369 yamat0jp 3 if (X >= 0) and (X < Count) and (Y >= 0) and (Y < Count) then
370     FStrings[X, Y] := Value;
371 yamat0jp 1 end;
372    
373     procedure TStoneGrid.SetTurnNumber(const Value: integer);
374     begin
375     if Value > FTurnIndex then
376 yamat0jp 4 FTurnNumber := FTurnIndex
377 yamat0jp 12 else if Value < 0 then
378     FTurnNumber := 0
379 yamat0jp 3 else
380     FTurnNumber := Value;
381 yamat0jp 10 FActive := false;
382 yamat0jp 3 FStrings := FBuffer[FTurnNumber];
383 yamat0jp 1 end;
384    
385 yamat0jp 9 procedure TStoneGrid.Start;
386     begin
387     Clear;
388     FActive := true;
389     end;
390    
391 yamat0jp 1 { TForm1 }
392    
393     procedure TForm1.ChangePlayer;
394     var
395     i, j, m, n: integer;
396     s: string;
397     procedure Main;
398     begin
399     if Index = Player1 then
400 yamat0jp 4 Index := Player2
401 yamat0jp 3 else
402     Index := Player1;
403 yamat0jp 1 end;
404     function Execute: Boolean;
405     var
406     i, j: integer;
407     begin
408 yamat0jp 3 result := false;
409     for i := 0 to Count - 1 do
410     for j := 0 to Count - 1 do
411 yamat0jp 10 if StoneGrid.CanSetStone(Index.Stone, i, j, false) = true then
412 yamat0jp 1 begin
413 yamat0jp 3 result := true;
414 yamat0jp 12 Exit;
415 yamat0jp 1 end;
416     end;
417 yamat0jp 3
418 yamat0jp 1 begin
419 yamat0jp 3 Main;
420 yamat0jp 1 if Execute = false then
421     begin
422 yamat0jp 3 Main;
423 yamat0jp 1 if Execute = false then
424     begin
425 yamat0jp 9 StoneGrid.Pause;
426 yamat0jp 3 Timer1.Enabled := false;
427     m := 0;
428     n := 0;
429     for i := 0 to Count - 1 do
430     for j := 0 to Count - 1 do
431     case StoneGrid[i, j] of
432     stBlack:
433     inc(m);
434     stWhite:
435     inc(n);
436 yamat0jp 1 end;
437     if m > n then
438 yamat0jp 4 s := 'Player1 Win:' + #13#10
439 yamat0jp 3 else if m < n then
440 yamat0jp 4 s := 'Player2 Win:' + #13#10
441 yamat0jp 3 else
442     s := 'Draw:' + #13#10;
443 yamat0jp 7 Showmessage(s + '(Player1) ' + IntToStr(m) + #13#10 + '(Player2) ' +
444     IntToStr(n));
445 yamat0jp 1 end;
446     end;
447     end;
448    
449     procedure TForm1.CompStone;
450     var
451     s: TPoint;
452     begin
453 yamat0jp 10 s := StoneGrid.NextStone(Index.Stone);
454     StoneGrid.CanSetStone(Index.Stone, s.X, s.Y, true, true);
455 yamat0jp 5 PaintBox1.Repaint;
456 yamat0jp 7 ChangePlayer;
457 yamat0jp 1 end;
458    
459     procedure TForm1.GameStart;
460     begin
461 yamat0jp 9 StoneGrid.Start;
462 yamat0jp 5 PaintBox1.Repaint;
463 yamat0jp 3 Index := Player1;
464     Timer1.Enabled := true;
465 yamat0jp 1 end;
466    
467 yamat0jp 7 procedure TForm1.MenuItem10Click(Sender: TObject);
468     begin
469 yamat0jp 9 StoneGrid.Restart;
470 yamat0jp 7 Timer1.Enabled := true;
471     end;
472    
473     procedure TForm1.MenuItem11Click(Sender: TObject);
474     begin
475 yamat0jp 10 Timer1.Enabled := false;
476 yamat0jp 7 with StoneGrid do
477     if Sender = MenuItem11 then
478     TurnNumber := TurnNumber + 1
479     else
480     TurnNumber := TurnNumber - 1;
481 yamat0jp 14 PaintBox1.Repaint;
482 yamat0jp 12 ChangePlayer;
483 yamat0jp 7 end;
484    
485 yamat0jp 5 procedure TForm1.MenuItem2Click(Sender: TObject);
486 yamat0jp 1 begin
487 yamat0jp 3 GameStart;
488 yamat0jp 1 end;
489    
490 yamat0jp 5 procedure TForm1.MenuItem4Click(Sender: TObject);
491 yamat0jp 1 begin
492 yamat0jp 5 Close;
493 yamat0jp 1 end;
494    
495 yamat0jp 5 procedure TForm1.MenuItem6Click(Sender: TObject);
496     begin
497 yamat0jp 7 Player1.Auto := MenuItem6.IsChecked;
498     Player2.Auto := MenuItem7.IsChecked;
499 yamat0jp 10 MenuItem10Click(Sender);
500 yamat0jp 5 end;
501    
502 yamat0jp 7 procedure TForm1.MenuItem8Click(Sender: TObject);
503     begin
504 yamat0jp 9 StoneGrid.Pause;
505     Timer1.Enabled := false;
506 yamat0jp 7 end;
507    
508 yamat0jp 5 procedure TForm1.PaintBox1Paint(Sender: TObject; Canvas: TCanvas);
509 yamat0jp 1 var
510     i, j: integer;
511     begin
512 yamat0jp 5 Canvas.Fill.Color := TAlphaColors.White;
513     Canvas.FillRect(RectF(0, 0, Count * Size, Count * Size), 0, 0, [], 1);
514     for i := 0 to Count do
515 yamat0jp 1 begin
516 yamat0jp 5 Canvas.DrawLine(PointF(i * Size, 0), PointF(i * Size, Size * Count), 1);
517     for j := 0 to Count do
518 yamat0jp 1 begin
519 yamat0jp 5 Canvas.DrawLine(PointF(0, j * Size), PointF(Count * Size, j * Size), 1);
520 yamat0jp 3 case StoneGrid.Strings[i, j] of
521     stWhite:
522 yamat0jp 14 Canvas.DrawBitmap(Image4.Bitmap, RectF(0, 0, Image4.Bitmap.Width,
523     Image4.Bitmap.Height), RectF(i * Size, j * Size, (i + 1) * Size,
524 yamat0jp 7 (j + 1) * Size), 1);
525 yamat0jp 3 stBlack:
526 yamat0jp 14 Canvas.DrawBitmap(Image3.Bitmap, RectF(0, 0, Image3.Bitmap.Width,
527     Image3.Bitmap.Height), RectF(i * Size, j * Size, (i + 1) * Size,
528     (j + 1) * Size), 1);
529     else
530     continue;
531 yamat0jp 1 end;
532     end;
533     end;
534 yamat0jp 14 if StoneGrid.Active = false then
535     StoneGrid.Paint(Canvas);
536 yamat0jp 1 end;
537    
538 yamat0jp 7 procedure TForm1.PaintBox1Resize(Sender: TObject);
539     begin
540     Size := Min(ClientWidth, ClientHeight) div Count;
541     end;
542    
543 yamat0jp 5 procedure TForm1.FormCreate(Sender: TObject);
544 yamat0jp 1 begin
545 yamat0jp 5 StoneGrid := TStoneGrid.Create;
546 yamat0jp 14 StoneGrid.ImageCount(Form1.Image1.Bitmap.Width div Form1.Image3.Bitmap.Width,
547     Form1.Image1.Bitmap.Height div Form1.Image3.Bitmap.Height);
548 yamat0jp 5 Player1 := TPlayer.Create;
549     Player2 := TPlayer.Create;
550     Player1.Stone := stBlack;
551     Player2.Stone := stWhite;
552     Player2.Auto := true;
553     with PaintBox1.Canvas do
554 yamat0jp 1 begin
555 yamat0jp 5 StrokeDash := TStrokeDash.Solid;
556     Stroke.Color := TAlphaColors.Black;
557     StrokeThickness := 3;
558 yamat0jp 1 end;
559 yamat0jp 7 PaintBox1Resize(Sender);
560 yamat0jp 5 GameStart;
561 yamat0jp 1 end;
562    
563 yamat0jp 5 procedure TForm1.FormDestroy(Sender: TObject);
564     begin
565     StoneGrid.Free;
566     Player1.Free;
567     Player2.Free;
568     end;
569    
570 yamat0jp 7 procedure TForm1.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
571 yamat0jp 5 Shift: TShiftState; X, Y: Single);
572     begin
573 yamat0jp 7 PaintBox1Tap(Sender, PointF(X, Y));
574 yamat0jp 5 end;
575    
576 yamat0jp 1 procedure TForm1.Timer1Timer(Sender: TObject);
577     begin
578 yamat0jp 10 if (StoneGrid.Active = true) and (Index.Auto = true) then
579 yamat0jp 1 begin
580 yamat0jp 3 Timer1.Enabled := false;
581     CompStone;
582     Timer1.Enabled := true;
583 yamat0jp 1 end;
584     end;
585    
586 yamat0jp 14 procedure TForm1.Timer2Timer(Sender: TObject);
587     begin
588     if StoneGrid.ListExecute = true then
589     PaintBox1.Repaint;
590     end;
591    
592 yamat0jp 1 procedure TForm1.FormResize(Sender: TObject);
593     begin
594 yamat0jp 3 Size := Min(ClientWidth, ClientHeight) div Count;
595 yamat0jp 5 PaintTo(Canvas);
596 yamat0jp 1 end;
597    
598 yamat0jp 7 procedure TForm1.PaintBox1Tap(Sender: TObject; const Point: TPointF);
599 yamat0jp 1 begin
600 yamat0jp 11 if Index.Auto = false then
601     begin
602 yamat0jp 10 MenuItem10Click(Sender);
603     if StoneGrid.CanSetStone(Index.Stone, Floor(Point.X / Size),
604 yamat0jp 7 Floor(Point.Y / Size), true, true) = true then
605 yamat0jp 5 begin
606     PaintBox1.Repaint;
607 yamat0jp 7 ChangePlayer;
608 yamat0jp 5 end;
609     end;
610 yamat0jp 1 end;
611    
612     end.

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26