Develop and Download Open Source Software

Browse Subversion Repository

Contents of /Unit1.pas

Parent Directory Parent Directory | Revision Log Revision Log


Revision 34 - (show annotations) (download) (as text)
Sun Aug 23 05:26:29 2015 UTC (8 years, 7 months ago) by yamat0jp
File MIME type: text/x-pascal
File size: 17615 byte(s)
難易度Hardを付け加えました。従来より2手先読みしますが強くなっているかはわかりません。

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

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