Develop and Download Open Source Software

Browse Subversion Repository

Diff of /Unit1.pas

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 30 by yamat0jp, Sun Aug 16 04:15:52 2015 UTC revision 34 by yamat0jp, Sun Aug 23 05:26:29 2015 UTC
# Line 4  interface Line 4  interface
4    
5  uses  uses
6    System.SysUtils, System.Types, System.UITypes, System.Classes,    System.SysUtils, System.Types, System.UITypes, System.Classes,
7    System.Variants,    System.Variants, Generics.Collections,
8    FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Menus,    FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Menus,
9    System.Math, FMX.Objects, FMX.StdCtrls;    System.Math, FMX.Objects, FMX.StdCtrls;
10    
11  const  const
12    Count = 8;    bmp_count = 8;
13    
14  type  type
15    TStoneType = (stNone, stWhite, stBlack, stError, stEffect);    TStoneType = (stNone, stWhite, stBlack, stError, stEffect);
# Line 19  type Line 19  type
19      Left, Top: integer;      Left, Top: integer;
20    end;    end;
21    
22    TGridData = array [0 .. Count - 1] of array [0 .. Count - 1] of TStoneType;    TGridData = array [0 .. bmp_count - 1] of array [0 .. bmp_count - 1]
23        of TStoneType;
24    
25    TPlayer = class(TObject)    TPlayer = class(TObject)
26    private    private
# Line 33  type Line 34  type
34    TStoneGrid = class(TObject)    TStoneGrid = class(TObject)
35    private    private
36      FStrings: TGridData;      FStrings: TGridData;
37      FBuffer: array [0 .. Count * Count - 4] of TGridData;      FBuffer: array [0 .. bmp_count * bmp_count - 4] of TGridData;
38      FTurnNumber: integer;      FTurnNumber: integer;
39      FTurnIndex: integer;      FTurnIndex: integer;
40      FActive: Boolean;      FActive: Boolean;
41      FList: TList;      FList: TList<TEffectData>;
42      FEffectStone: TStoneType;      FEffectStone: TStoneType;
43      FIndex_X: integer;      FIndex_X: integer;
44      FIndex_Y: integer;      FIndex_Y: integer;
# Line 51  type Line 52  type
52      constructor Create;      constructor Create;
53      destructor Destroy; override;      destructor Destroy; override;
54      procedure Clear;      procedure Clear;
55      function CalScore(Stone: TStoneType; X, Y: integer): integer;      function CalScore(Stone: TStoneType; X, Y: integer;
56          out Score: integer): Boolean;
57      function CanSetStone(Stone: TStoneType; X, Y: integer; Reverse: Boolean;      function CanSetStone(Stone: TStoneType; X, Y: integer; Reverse: Boolean;
58        const Visible: Boolean = false): Boolean;        const Visible: Boolean = false): Boolean;
59      function NextStone(Stone: TStoneType; var Pos: TPoint): Boolean;      function NextStone(Stone: TStoneType; var Pos: TPoint): Boolean;
# Line 89  type Line 91  type
91      Image1: TImage;      Image1: TImage;
92      Image2: TImage;      Image2: TImage;
93      Image3: TImage;      Image3: TImage;
94      Image4: TImage;      MenuItem13: TMenuItem;
95      Image5: TImage;      MenuItem14: TMenuItem;
96        MenuItem15: TMenuItem;
97      procedure FormCreate(Sender: TObject);      procedure FormCreate(Sender: TObject);
98      procedure FormDestroy(Sender: TObject);      procedure FormDestroy(Sender: TObject);
99      procedure Timer1Timer(Sender: TObject);      procedure Timer1Timer(Sender: TObject);
# Line 130  implementation Line 133  implementation
133  {$R *.fmx}  {$R *.fmx}
134  {$R *.Windows.fmx MSWINDOWS}  {$R *.Windows.fmx MSWINDOWS}
135  {$R *.XLgXhdpiTb.fmx ANDROID}  {$R *.XLgXhdpiTb.fmx ANDROID}
   
136  { TStoneGrid }  { TStoneGrid }
137    
138  function TStoneGrid.AddScore(X, Y: integer; const NG: array of TPoint): integer;  function TStoneGrid.AddScore(X, Y: integer; const NG: array of TPoint): integer;
# Line 146  begin Line 148  begin
148      end;      end;
149  end;  end;
150    
151  function TStoneGrid.CalScore(Stone: TStoneType; X, Y: integer): integer;  function TStoneGrid.CalScore(Stone: TStoneType; X, Y: integer;
152      out Score: integer): Boolean;
153  var  var
154    i, j: integer;    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  begin
191    if CanSetStone(Stone, X, Y, true) = true then    if CanSetStone(Stone, X, Y, true) = true then
192    begin    begin
193      result := 0;      Score := 0;
194        result := true;
195      if FTurnIndex < 50 then      if FTurnIndex < 50 then
196        inc(result, AddScore(X, Y, [Point(1, 0), Point(6, 0), Point(0, 1),        inc(Score, AddScore(X, Y, waste));
197          Point(1, 1), Point(6, 1), Point(7, 1), Point(0, 6), Point(1, 6),      dec(Score, AddScore(X, Y, worth));
         Point(6, 6), Point(7, 6), Point(1, 7), Point(6, 7)]));  
198      case Stone of      case Stone of
199        stBlack:        stBlack:
200          Stone := stWhite;          Stone := stWhite;
201        stWhite:        stWhite:
202          Stone := stBlack;          Stone := stBlack;
203      end;      end;
204      for i := 0 to Count - 1 do      if (Form1.MenuItem14.IsChecked = true) and (FTurnIndex + 1 >= 60) then
205        for j := 0 to Count - 1 do      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          if CanSetStone(Stone, i, j, false) = true then
213          begin          begin
214            inc(result);            inc(Score);
215            if FTurnIndex < 50 then            inc(Score, AddScore(i, j, worth));
             inc(result, AddScore(i, j, [Point(0, 0), Point(7, 0), Point(0, 7),  
               Point(7, 7)]));  
216          end;          end;
217    end    end
218    else    else
219      result := -1;      result := false;
220    FStrings := FBuffer[FTurnIndex];    FStrings := FBuffer[FTurnIndex];
221  end;  end;
222    
# Line 183  function TStoneGrid.CanSetStone(Stone: T Line 225  function TStoneGrid.CanSetStone(Stone: T
225  var  var
226    i: integer;    i: integer;
227    p: Boolean;    p: Boolean;
228    q: ^TEffectData;    q: TEffectData;
229    procedure Method(m, n: integer);    procedure Method(m, n: integer);
230    var    var
231      s: TStoneType;      s: TStoneType;
232      j, k: integer;      j: integer;
233        k: integer;
234    begin    begin
235      if p = false then      if p = false then
236        Exit;        Exit;
# Line 207  var Line 250  var
250            result := true;            result := true;
251            if Reverse = true then            if Reverse = true then
252            begin            begin
253                Form1.PaintBox1.Repaint;
254              for j := 1 to i - 1 do              for j := 1 to i - 1 do
255              begin              begin
               Form1.PaintBox1.Repaint;  
256                if Visible = true then                if Visible = true then
257                begin                begin
258                  FEffectStone := Stone;                  FEffectStone := Stone;
259                  New(q);                  q.Left := X + m * j;
260                  q^.Left := X + m * j;                  q.Top := Y + n * j;
261                  q^.Top := Y + n * j;                  q.X := 0;
262                  q^.X := 0;                  q.Y := 0;
                 q^.Y := 0;  
263                  FList.Add(q);                  FList.Add(q);
264                  SetStrings(q^.Left, q^.Top, stEffect);                  SetStrings(q.Left, q.Top, stEffect);
265                  for k := 1 to 100 do                  for k := 1 to 10 do
266                  begin                  begin
267                    Sleep(1);                    Sleep(15);
268                    Application.ProcessMessages;                    Application.ProcessMessages;
269                  end;                  end;
270                end                end
# Line 264  procedure TStoneGrid.Clear; Line 306  procedure TStoneGrid.Clear;
306  var  var
307    i, j: integer;    i, j: integer;
308  begin  begin
   for i := 0 to FList.Count - 1 do  
     Dispose(FList[i]);  
309    FList.Clear;    FList.Clear;
310    for i := 0 to Count - 1 do    for i := 0 to bmp_count - 1 do
311      for j := 0 to Count - 1 do      for j := 0 to bmp_count - 1 do
312        Strings[i, j] := stNone;        Strings[i, j] := stNone;
313    Strings[3, 3] := stBlack;    Strings[3, 3] := stBlack;
314    Strings[4, 4] := stBlack;    Strings[4, 4] := stBlack;
# Line 282  end; Line 322  end;
322  constructor TStoneGrid.Create;  constructor TStoneGrid.Create;
323  begin  begin
324    inherited;    inherited;
325    FList := TList.Create;    FList := TList<TEffectData>.Create;
326  end;  end;
327    
328  destructor TStoneGrid.Destroy;  destructor TStoneGrid.Destroy;
 var  
   i: integer;  
329  begin  begin
   for i := 0 to FList.Count - 1 do  
     Dispose(FList[i]);  
330    FList.Free;    FList.Free;
331    inherited;    inherited;
332  end;  end;
# Line 311  end; Line 347  end;
347    
348  function TStoneGrid.GetStrings(X, Y: integer): TStoneType;  function TStoneGrid.GetStrings(X, Y: integer): TStoneType;
349  begin  begin
350    if (X >= 0) and (X < Count) and (Y >= 0) and (Y < Count) then    if (X >= 0) and (X < bmp_count) and (Y >= 0) and (Y < bmp_count) then
351      result := FStrings[X, Y]      result := FStrings[X, Y]
352    else    else
353      result := stError;      result := stError;
# Line 325  end; Line 361  end;
361    
362  function TStoneGrid.ListExecute: Boolean;  function TStoneGrid.ListExecute: Boolean;
363  var  var
   p: ^TEffectData;  
364    i: integer;    i: integer;
365      s: TEffectData;
366  begin  begin
367    if FList.Count = 0 then    if FList.Count = 0 then
368      result := false      result := false
369    else    else
370    begin    begin
371      for i := 0 to FList.Count - 1 do      i := 0;
372        while i < FList.Count do
373      begin      begin
374        p := FList[i];        s := FList[i];
375        if p^.X < FIndex_X - 1 then        if s.X < FIndex_X - 1 then
376          p^.X := p^.X + 1          s.X := s.X + 1
377        else if p^.Y < FIndex_Y - 1 then        else if s.Y < FIndex_Y - 1 then
378        begin        begin
379          p^.X := 0;          s.X := 0;
380          p^.Y := p^.Y + 1;          s.Y := s.Y + 1;
381        end        end
382        else        else
383        begin        begin
384          SetStrings(p^.Left, p^.Top, FEffectStone);          SetStrings(s.Left, s.Top, FEffectStone);
385          Dispose(p);          FList.Delete(i);
386          FList[i] := nil;          inc(i);
387            continue;
388        end;        end;
389          FList[i] := s;
390          inc(i);
391      end;      end;
     for i := FList.Count - 1 downto 0 do  
       if FList[i] = nil then  
         FList.Delete(i);  
392      if FList.Count = 0 then      if FList.Count = 0 then
393      begin      begin
394        inc(FTurnIndex);        inc(FTurnIndex);
395        inc(FTurnNumber);        inc(FTurnNumber);
396        FBuffer[FTurnIndex] := FStrings;        FBuffer[FTurnIndex] := FStrings;
397          Form1.PaintBox1.Repaint;
398          Form1.ChangePlayer;
399        if FGameOver = false then        if FGameOver = false then
400        begin          FActive := true
         Form1.ChangePlayer;  
         FActive:=true;  
       end;  
401      end;      end;
402      result := true;      result := true;
403    end;    end;
# Line 371  function TStoneGrid.NextStone(Stone: TSt Line 407  function TStoneGrid.NextStone(Stone: TSt
407  var  var
408    i, j, m, n: integer;    i, j, m, n: integer;
409  begin  begin
410    n := -1;    result := false;
411    for i := 0 to Count - 1 do    n := 0;
412      for j := 0 to Count - 1 do    for i := 0 to bmp_count - 1 do
413      begin      for j := 0 to bmp_count - 1 do
414        m := CalScore(Stone, i, j);        if (CalScore(Stone, i, j, m) = true) and ((result = false) or (m < n))
415        if (n = -1) or ((0 < m) and (m < n)) then        then
416        begin        begin
417            if result = false then
418              result := true;
419          n := m;          n := m;
420          Pos := Point(i, j);          Pos := Point(i, j);
421        end;        end;
     end;  
   result := not(n = -1);  
422  end;  end;
423    
424  procedure TStoneGrid.Paint(Canvas: TCanvas);  procedure TStoneGrid.Paint(Canvas: TCanvas);
425  var  var
426    i: integer;    k: integer;
   k, m, n: integer;  
427    s: TBitmap;    s: TBitmap;
428    p: ^TEffectData;    p: TEffectData;
429  begin  begin
   m := Form1.Image3.Bitmap.Width;  
   n := Form1.Image3.Bitmap.Height;  
430    k := Form1.Size;    k := Form1.Size;
431    for i := 0 to FList.Count - 1 do    if FEffectStone = stBlack then
432        s := Form1.Image1.Bitmap
433      else
434        s := Form1.Image2.Bitmap;
435      for p in FList do
436    begin    begin
437      p := FList[i];      Canvas.DrawBitmap(s, RectF(p.X * 50, p.Y * 50, (p.X + 1) * 50,
438      if FEffectStone = stBlack then        (p.Y + 1) * 50), RectF(p.Left * k, p.Top * k, (p.Left + 1) * k,
439        s := Form1.Image1.Bitmap        (p.Top + 1) * k), 1);
     else  
       s := Form1.Image2.Bitmap;  
     Canvas.DrawBitmap(s, RectF(p^.X * m, p^.Y * n, (p^.X + 1) * m,  
       (p^.Y + 1) * n), RectF(p^.Left * k, p^.Top * k, (p^.Left + 1) * k,  
       (p^.Top + 1) * k), 1);  
440    end;    end;
441  end;  end;
442    
# Line 415  end; Line 447  end;
447    
448  procedure TStoneGrid.Restart;  procedure TStoneGrid.Restart;
449  begin  begin
450    FActive:=true;    FActive := true;
451    FGameOver := false;    FGameOver := false;
452    FTurnIndex := FTurnNumber;    FTurnIndex := FTurnNumber;
453  end;  end;
454    
455  procedure TStoneGrid.SetActive(const Value: Boolean);  procedure TStoneGrid.SetActive(const Value: Boolean);
456  begin  begin
457    if (FGameOver = false)or(Value = false) then    if (FGameOver = false) or (Value = false) then
458      FActive := Value;      FActive := Value;
459  end;  end;
460    
461  procedure TStoneGrid.SetStrings(X, Y: integer; const Value: TStoneType);  procedure TStoneGrid.SetStrings(X, Y: integer; const Value: TStoneType);
462  begin  begin
463    if (X >= 0) and (X < Count) and (Y >= 0) and (Y < Count) then    if (X >= 0) and (X < bmp_count) and (Y >= 0) and (Y < bmp_count) then
464      FStrings[X, Y] := Value;      FStrings[X, Y] := Value;
465  end;  end;
466    
# Line 473  var Line 505  var
505    var    var
506      i, j: integer;      i, j: integer;
507    begin    begin
508      for i := 0 to Count - 1 do      for i := 0 to bmp_count - 1 do
509        for j := 0 to Count - 1 do        for j := 0 to bmp_count - 1 do
510          if StoneGrid.CanSetStone(Index.Stone, i, j, false) = true then          if StoneGrid.CanSetStone(Index.Stone, i, j, false) = true then
511          begin          begin
512            result := true;            result := true;
# Line 492  begin Line 524  begin
524      begin      begin
525        m := 0;        m := 0;
526        n := 0;        n := 0;
527        for i := 0 to Count - 1 do        for i := 0 to bmp_count - 1 do
528          for j := 0 to Count - 1 do          for j := 0 to bmp_count - 1 do
529            case StoneGrid[i, j] of            case StoneGrid[i, j] of
530              stBlack:              stBlack:
531                inc(m);                inc(m);
# Line 508  begin Line 540  begin
540        else        else
541          s := 'Draw:' + #13#10;          s := 'Draw:' + #13#10;
542        StoneGrid.GameOver;        StoneGrid.GameOver;
543        Showmessage(s + '(Player1) ' + IntToStr(m) + #13#10 + '(Player2) ' +        Showmessage(s + '(Player1) ' + m.ToString + #13#10 + '(Player2) ' +
544          IntToStr(n));          n.ToString);
545      end      end
546      else      else
547        Caption := s;        Caption := s;
# Line 523  var Line 555  var
555    s: TPoint;    s: TPoint;
556  begin  begin
557    StoneGrid.Active := false;    StoneGrid.Active := false;
558    StoneGrid.NextStone(Index.Stone, s);    if StoneGrid.NextStone(Index.Stone, s) = true then
559    StoneGrid.CanSetStone(Index.Stone, s.X, s.Y, true, true);    begin
560    PaintBox1.Repaint;      StoneGrid.CanSetStone(Index.Stone, s.X, s.Y, true, true);
561        PaintBox1.Repaint;
562      end
563      else
564        ChangePlayer;
565  end;  end;
566    
567  procedure TForm1.GameStart;  procedure TForm1.GameStart;
# Line 592  var Line 628  var
628  begin  begin
629    if StoneGrid.Active = false then    if StoneGrid.Active = false then
630      StoneGrid.Paint(Canvas);      StoneGrid.Paint(Canvas);
631    for i := 0 to Count - 1 do    for i := 0 to bmp_count - 1 do
632    begin    begin
633      for j := 0 to Count - 1 do      for j := 0 to bmp_count - 1 do
634      begin      begin
635        case StoneGrid.Strings[i, j] of        case StoneGrid.Strings[i, j] of
636          stWhite:          stWhite:
637            Canvas.DrawBitmap(Image4.Bitmap, RectF(0, 0, Image4.Bitmap.Width,            Canvas.DrawBitmap(Image3.Bitmap, RectF(100, 0, 150, 50),
638              Image4.Bitmap.Height), RectF(i * Size, j * Size, (i + 1) * Size,              RectF(i * Size, j * Size, (i + 1) * Size, (j + 1) * Size), 1);
             (j + 1) * Size), 1);  
639          stBlack:          stBlack:
640            Canvas.DrawBitmap(Image3.Bitmap, RectF(0, 0, Image3.Bitmap.Width,            Canvas.DrawBitmap(Image3.Bitmap, RectF(50, 0, 100, 50),
641              Image3.Bitmap.Height), RectF(i * Size, j * Size, (i + 1) * Size,              RectF(i * Size, j * Size, (i + 1) * Size, (j + 1) * Size), 1);
             (j + 1) * Size), 1);  
642          stEffect:          stEffect:
643            continue;            continue;
644        else        else
645          Canvas.DrawBitmap(Image5.Bitmap, RectF(0, 0, Image5.Bitmap.Width,          Canvas.DrawBitmap(Image3.Bitmap, RectF(0, 0, 50, 50),
646            Image5.Bitmap.Height), RectF(i * Size, j * Size, (i + 1) * Size,            RectF(i * Size, j * Size, (i + 1) * Size, (j + 1) * Size), 1);
           (j + 1) * Size), 1);  
647        end;        end;
648        Canvas.DrawLine(PointF(0, j * Size), PointF(Count * Size, j * Size), 1);        Canvas.DrawLine(PointF(0, j * Size), PointF(bmp_count * Size,
649            j * Size), 1);
650      end;      end;
651      Canvas.DrawLine(PointF(i * Size, 0), PointF(i * Size, Size * Count), 1);      Canvas.DrawLine(PointF(i * Size, 0), PointF(i * Size, Size * bmp_count), 1);
652    end;    end;
653    Canvas.DrawLine(PointF(Count * Size, 0),    Canvas.DrawLine(PointF(bmp_count * Size, 0), PointF(bmp_count * Size,
654      PointF(Count * Size, Count * Size), 1);      bmp_count * Size), 1);
655    Canvas.DrawLine(PointF(0, Count * Size),    Canvas.DrawLine(PointF(0, bmp_count * Size), PointF(bmp_count * Size,
656      PointF(Count * Size, Count * Size), 1);      bmp_count * Size), 1);
657  end;  end;
658    
659  procedure TForm1.PaintBox1Resize(Sender: TObject);  procedure TForm1.PaintBox1Resize(Sender: TObject);
660  begin  begin
661    Size := Min(ClientWidth, ClientHeight) div Count;    Size := Min(ClientWidth, ClientHeight) div bmp_count;
662  end;  end;
663    
664  procedure TForm1.FormCreate(Sender: TObject);  procedure TForm1.FormCreate(Sender: TObject);
665  begin  begin
666    ClientWidth:=20*Count;    ClientWidth := 400;
667    ClientHeight:=20*Count;    ClientHeight := 400;
668    StoneGrid := TStoneGrid.Create;    StoneGrid := TStoneGrid.Create;
669    StoneGrid.ImageCount(Form1.Image1.Bitmap.Width div Form1.Image3.Bitmap.Width,    StoneGrid.ImageCount(6, 5);
     Form1.Image1.Bitmap.Height div Form1.Image3.Bitmap.Height);  
670    Player1 := TPlayer.Create;    Player1 := TPlayer.Create;
671    Player2 := TPlayer.Create;    Player2 := TPlayer.Create;
672    Player1.Stone := stBlack;    Player1.Stone := stBlack;
# Line 676  end; Line 709  end;
709    
710  procedure TForm1.FormResize(Sender: TObject);  procedure TForm1.FormResize(Sender: TObject);
711  begin  begin
712    Size := Min(ClientWidth, ClientHeight) div Count;    Size := Min(ClientWidth, ClientHeight) div bmp_count;
713    PaintTo(Canvas);    PaintTo(Canvas);
714  end;  end;
715    

Legend:
Removed from v.30  
changed lines
  Added in v.34

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