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 36 by yamat0jp, Sat Aug 29 19:48:45 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 Easy;
164      var
165        m, n: integer;
166      begin
167        for m := 0 to bmp_count - 1 do
168          for n := 0 to bmp_count - 1 do
169            if CanSetStone(Stone, m, n, false) = true then
170            begin
171              inc(Score);
172              inc(Score, AddScore(m, n, worth));
173            end;
174      end;
175      procedure Hard;
176      var
177        m, n: integer;
178      begin
179        if loop > 1 then
180          Exit;
181        inc(loop);
182        for m := 0 to bmp_count - 1 do
183          for n := 0 to bmp_count - 1 do
184          begin
185            if CanSetStone(Stone, m, n, true) = true then
186            begin
187              if (loop mod 2) > 0 then
188              begin
189                inc(Score, AddScore(m, n, worth));
190                if FTurnIndex + loop < 50 then
191                  dec(Score, AddScore(m, n, waste));
192              end
193              else
194              begin
195                dec(Score, AddScore(m, n, worth));
196                if FTurnIndex + loop < 50 then
197                  inc(Score, AddScore(m, n, waste));
198              end;
199              case Stone of
200                stBlack:
201                  Stone := stWhite;
202                stWhite:
203                  Stone := stBlack;
204              end;
205              Hard;
206              if loop > 1 then
207              begin
208                Easy;
209                FStrings := FBuffer[FTurnIndex + loop];
210              end else
211                FBuffer[FTurnIndex + loop] := FStrings;
212            end;
213          end;
214        dec(loop);
215      end;
216    
217  begin  begin
218    if CanSetStone(Stone, X, Y, true) = true then    if CanSetStone(Stone, X, Y, true) = true then
219    begin    begin
220      result := 0;      Score := 0;
221        result := true;
222      if FTurnIndex < 50 then      if FTurnIndex < 50 then
223        inc(result, AddScore(X, Y, [Point(1, 0), Point(6, 0), Point(0, 1),        inc(Score, AddScore(X, Y, waste));
224          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)]));  
225      case Stone of      case Stone of
226        stBlack:        stBlack:
227          Stone := stWhite;          Stone := stWhite;
228        stWhite:        stWhite:
229          Stone := stBlack;          Stone := stBlack;
230      end;      end;
231      for i := 0 to Count - 1 do      if (Form1.MenuItem14.IsChecked = true) and (FTurnIndex + 2 <= 60) then
232        for j := 0 to Count - 1 do      begin
233          if CanSetStone(Stone, i, j, false) = true then        loop := 0;
234          begin        Hard;
235            inc(result);      end
236            if FTurnIndex < 50 then      else
237              inc(result, AddScore(i, j, [Point(0, 0), Point(7, 0), Point(0, 7),        Easy;
               Point(7, 7)]));  
         end;  
238    end    end
239    else    else
240      result := -1;      result := false;
241    FStrings := FBuffer[FTurnIndex];    FStrings := FBuffer[FTurnIndex];
242  end;  end;
243    
# Line 183  function TStoneGrid.CanSetStone(Stone: T Line 246  function TStoneGrid.CanSetStone(Stone: T
246  var  var
247    i: integer;    i: integer;
248    p: Boolean;    p: Boolean;
249    q: ^TEffectData;    q: TEffectData;
250    procedure Method(m, n: integer);    procedure Method(m, n: integer);
251    var    var
252      s: TStoneType;      s: TStoneType;
253      j, k: integer;      j: integer;
254        k: integer;
255    begin    begin
256      if p = false then      if p = false then
257        Exit;        Exit;
# Line 207  var Line 271  var
271            result := true;            result := true;
272            if Reverse = true then            if Reverse = true then
273            begin            begin
274                Form1.PaintBox1.Repaint;
275              for j := 1 to i - 1 do              for j := 1 to i - 1 do
276              begin              begin
               Form1.PaintBox1.Repaint;  
277                if Visible = true then                if Visible = true then
278                begin                begin
279                  FEffectStone := Stone;                  FEffectStone := Stone;
280                  New(q);                  q.Left := X + m * j;
281                  q^.Left := X + m * j;                  q.Top := Y + n * j;
282                  q^.Top := Y + n * j;                  q.X := 0;
283                  q^.X := 0;                  q.Y := 0;
                 q^.Y := 0;  
284                  FList.Add(q);                  FList.Add(q);
285                  SetStrings(q^.Left, q^.Top, stEffect);                  SetStrings(q.Left, q.Top, stEffect);
286                  for k := 1 to 100 do                  for k := 1 to 10 do
287                  begin                  begin
288                    Sleep(1);                    Sleep(15);
289                    Application.ProcessMessages;                    Application.ProcessMessages;
290                  end;                  end;
291                end                end
# Line 264  procedure TStoneGrid.Clear; Line 327  procedure TStoneGrid.Clear;
327  var  var
328    i, j: integer;    i, j: integer;
329  begin  begin
   for i := 0 to FList.Count - 1 do  
     Dispose(FList[i]);  
330    FList.Clear;    FList.Clear;
331    for i := 0 to Count - 1 do    for i := 0 to bmp_count - 1 do
332      for j := 0 to Count - 1 do      for j := 0 to bmp_count - 1 do
333        Strings[i, j] := stNone;        Strings[i, j] := stNone;
334    Strings[3, 3] := stBlack;    Strings[3, 3] := stBlack;
335    Strings[4, 4] := stBlack;    Strings[4, 4] := stBlack;
# Line 282  end; Line 343  end;
343  constructor TStoneGrid.Create;  constructor TStoneGrid.Create;
344  begin  begin
345    inherited;    inherited;
346    FList := TList.Create;    FList := TList<TEffectData>.Create;
347  end;  end;
348    
349  destructor TStoneGrid.Destroy;  destructor TStoneGrid.Destroy;
 var  
   i: integer;  
350  begin  begin
   for i := 0 to FList.Count - 1 do  
     Dispose(FList[i]);  
351    FList.Free;    FList.Free;
352    inherited;    inherited;
353  end;  end;
# Line 311  end; Line 368  end;
368    
369  function TStoneGrid.GetStrings(X, Y: integer): TStoneType;  function TStoneGrid.GetStrings(X, Y: integer): TStoneType;
370  begin  begin
371    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
372      result := FStrings[X, Y]      result := FStrings[X, Y]
373    else    else
374      result := stError;      result := stError;
# Line 325  end; Line 382  end;
382    
383  function TStoneGrid.ListExecute: Boolean;  function TStoneGrid.ListExecute: Boolean;
384  var  var
   p: ^TEffectData;  
385    i: integer;    i: integer;
386      s: TEffectData;
387  begin  begin
388    if FList.Count = 0 then    if FList.Count = 0 then
389      result := false      result := false
390    else    else
391    begin    begin
392      for i := 0 to FList.Count - 1 do      i := 0;
393        while i < FList.Count do
394      begin      begin
395        p := FList[i];        s := FList[i];
396        if p^.X < FIndex_X - 1 then        if s.X < FIndex_X - 1 then
397          p^.X := p^.X + 1          s.X := s.X + 1
398        else if p^.Y < FIndex_Y - 1 then        else if s.Y < FIndex_Y - 1 then
399        begin        begin
400          p^.X := 0;          s.X := 0;
401          p^.Y := p^.Y + 1;          s.Y := s.Y + 1;
402        end        end
403        else        else
404        begin        begin
405          SetStrings(p^.Left, p^.Top, FEffectStone);          SetStrings(s.Left, s.Top, FEffectStone);
406          Dispose(p);          FList.Delete(i);
407          FList[i] := nil;          inc(i);
408            continue;
409        end;        end;
410          FList[i] := s;
411          inc(i);
412      end;      end;
     for i := FList.Count - 1 downto 0 do  
       if FList[i] = nil then  
         FList.Delete(i);  
413      if FList.Count = 0 then      if FList.Count = 0 then
414      begin      begin
415        inc(FTurnIndex);        inc(FTurnIndex);
416        inc(FTurnNumber);        inc(FTurnNumber);
417        FBuffer[FTurnIndex] := FStrings;        FBuffer[FTurnIndex] := FStrings;
418          Form1.PaintBox1.Repaint;
419          Form1.ChangePlayer;
420        if FGameOver = false then        if FGameOver = false then
421        begin          FActive := true
         Form1.ChangePlayer;  
         FActive:=true;  
       end;  
422      end;      end;
423      result := true;      result := true;
424    end;    end;
# Line 371  function TStoneGrid.NextStone(Stone: TSt Line 428  function TStoneGrid.NextStone(Stone: TSt
428  var  var
429    i, j, m, n: integer;    i, j, m, n: integer;
430  begin  begin
431    n := -1;    result := false;
432    for i := 0 to Count - 1 do    n := 0;
433      for j := 0 to Count - 1 do    for i := 0 to bmp_count - 1 do
434      begin      for j := 0 to bmp_count - 1 do
435        m := CalScore(Stone, i, j);        if (CalScore(Stone, i, j, m) = true) and ((result = false) or (m < n))
436        if (n = -1) or ((0 < m) and (m < n)) then        then
437        begin        begin
438            if result = false then
439              result := true;
440          n := m;          n := m;
441          Pos := Point(i, j);          Pos := Point(i, j);
442        end;        end;
     end;  
   result := not(n = -1);  
443  end;  end;
444    
445  procedure TStoneGrid.Paint(Canvas: TCanvas);  procedure TStoneGrid.Paint(Canvas: TCanvas);
446  var  var
447    i: integer;    k: integer;
   k, m, n: integer;  
448    s: TBitmap;    s: TBitmap;
449    p: ^TEffectData;    p: TEffectData;
450  begin  begin
   m := Form1.Image3.Bitmap.Width;  
   n := Form1.Image3.Bitmap.Height;  
451    k := Form1.Size;    k := Form1.Size;
452    for i := 0 to FList.Count - 1 do    if FEffectStone = stBlack then
453        s := Form1.Image1.Bitmap
454      else
455        s := Form1.Image2.Bitmap;
456      for p in FList do
457    begin    begin
458      p := FList[i];      Canvas.DrawBitmap(s, RectF(p.X * 50, p.Y * 50, (p.X + 1) * 50,
459      if FEffectStone = stBlack then        (p.Y + 1) * 50), RectF(p.Left * k, p.Top * k, (p.Left + 1) * k,
460        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);  
461    end;    end;
462  end;  end;
463    
# Line 415  end; Line 468  end;
468    
469  procedure TStoneGrid.Restart;  procedure TStoneGrid.Restart;
470  begin  begin
471    FActive:=true;    FActive := true;
472    FGameOver := false;    FGameOver := false;
473    FTurnIndex := FTurnNumber;    FTurnIndex := FTurnNumber;
474  end;  end;
475    
476  procedure TStoneGrid.SetActive(const Value: Boolean);  procedure TStoneGrid.SetActive(const Value: Boolean);
477  begin  begin
478    if (FGameOver = false)or(Value = false) then    if (FGameOver = false) or (Value = false) then
479      FActive := Value;      FActive := Value;
480  end;  end;
481    
482  procedure TStoneGrid.SetStrings(X, Y: integer; const Value: TStoneType);  procedure TStoneGrid.SetStrings(X, Y: integer; const Value: TStoneType);
483  begin  begin
484    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
485      FStrings[X, Y] := Value;      FStrings[X, Y] := Value;
486  end;  end;
487    
# Line 473  var Line 526  var
526    var    var
527      i, j: integer;      i, j: integer;
528    begin    begin
529      for i := 0 to Count - 1 do      for i := 0 to bmp_count - 1 do
530        for j := 0 to Count - 1 do        for j := 0 to bmp_count - 1 do
531          if StoneGrid.CanSetStone(Index.Stone, i, j, false) = true then          if StoneGrid.CanSetStone(Index.Stone, i, j, false) = true then
532          begin          begin
533            result := true;            result := true;
# Line 492  begin Line 545  begin
545      begin      begin
546        m := 0;        m := 0;
547        n := 0;        n := 0;
548        for i := 0 to Count - 1 do        for i := 0 to bmp_count - 1 do
549          for j := 0 to Count - 1 do          for j := 0 to bmp_count - 1 do
550            case StoneGrid[i, j] of            case StoneGrid[i, j] of
551              stBlack:              stBlack:
552                inc(m);                inc(m);
# Line 508  begin Line 561  begin
561        else        else
562          s := 'Draw:' + #13#10;          s := 'Draw:' + #13#10;
563        StoneGrid.GameOver;        StoneGrid.GameOver;
564        Showmessage(s + '(Player1) ' + IntToStr(m) + #13#10 + '(Player2) ' +        Showmessage(s + '(Player1) ' + m.ToString + #13#10 + '(Player2) ' +
565          IntToStr(n));          n.ToString);
566      end      end
567      else      else
568        Caption := s;        Caption := s;
# Line 523  var Line 576  var
576    s: TPoint;    s: TPoint;
577  begin  begin
578    StoneGrid.Active := false;    StoneGrid.Active := false;
579    StoneGrid.NextStone(Index.Stone, s);    if StoneGrid.NextStone(Index.Stone, s) = true then
580    StoneGrid.CanSetStone(Index.Stone, s.X, s.Y, true, true);    begin
581    PaintBox1.Repaint;      StoneGrid.CanSetStone(Index.Stone, s.X, s.Y, true, true);
582        PaintBox1.Repaint;
583      end
584      else
585        ChangePlayer;
586  end;  end;
587    
588  procedure TForm1.GameStart;  procedure TForm1.GameStart;
# Line 592  var Line 649  var
649  begin  begin
650    if StoneGrid.Active = false then    if StoneGrid.Active = false then
651      StoneGrid.Paint(Canvas);      StoneGrid.Paint(Canvas);
652    for i := 0 to Count - 1 do    for i := 0 to bmp_count - 1 do
653    begin    begin
654      for j := 0 to Count - 1 do      for j := 0 to bmp_count - 1 do
655      begin      begin
656        case StoneGrid.Strings[i, j] of        case StoneGrid.Strings[i, j] of
657          stWhite:          stWhite:
658            Canvas.DrawBitmap(Image4.Bitmap, RectF(0, 0, Image4.Bitmap.Width,            Canvas.DrawBitmap(Image3.Bitmap, RectF(100, 0, 150, 50),
659              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);  
660          stBlack:          stBlack:
661            Canvas.DrawBitmap(Image3.Bitmap, RectF(0, 0, Image3.Bitmap.Width,            Canvas.DrawBitmap(Image3.Bitmap, RectF(50, 0, 100, 50),
662              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);  
663          stEffect:          stEffect:
664            continue;            continue;
665        else        else
666          Canvas.DrawBitmap(Image5.Bitmap, RectF(0, 0, Image5.Bitmap.Width,          Canvas.DrawBitmap(Image3.Bitmap, RectF(0, 0, 50, 50),
667            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);  
668        end;        end;
669        Canvas.DrawLine(PointF(0, j * Size), PointF(Count * Size, j * Size), 1);        Canvas.DrawLine(PointF(0, j * Size), PointF(bmp_count * Size,
670            j * Size), 1);
671      end;      end;
672      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);
673    end;    end;
674    Canvas.DrawLine(PointF(Count * Size, 0),    Canvas.DrawLine(PointF(bmp_count * Size, 0), PointF(bmp_count * Size,
675      PointF(Count * Size, Count * Size), 1);      bmp_count * Size), 1);
676    Canvas.DrawLine(PointF(0, Count * Size),    Canvas.DrawLine(PointF(0, bmp_count * Size), PointF(bmp_count * Size,
677      PointF(Count * Size, Count * Size), 1);      bmp_count * Size), 1);
678  end;  end;
679    
680  procedure TForm1.PaintBox1Resize(Sender: TObject);  procedure TForm1.PaintBox1Resize(Sender: TObject);
681  begin  begin
682    Size := Min(ClientWidth, ClientHeight) div Count;    Size := Min(ClientWidth, ClientHeight) div bmp_count;
683  end;  end;
684    
685  procedure TForm1.FormCreate(Sender: TObject);  procedure TForm1.FormCreate(Sender: TObject);
686  begin  begin
687    ClientWidth:=20*Count;    ClientWidth := 400;
688    ClientHeight:=20*Count;    ClientHeight := 400;
689    StoneGrid := TStoneGrid.Create;    StoneGrid := TStoneGrid.Create;
690    StoneGrid.ImageCount(Form1.Image1.Bitmap.Width div Form1.Image3.Bitmap.Width,    StoneGrid.ImageCount(6, 5);
     Form1.Image1.Bitmap.Height div Form1.Image3.Bitmap.Height);  
691    Player1 := TPlayer.Create;    Player1 := TPlayer.Create;
692    Player2 := TPlayer.Create;    Player2 := TPlayer.Create;
693    Player1.Stone := stBlack;    Player1.Stone := stBlack;
# Line 676  end; Line 730  end;
730    
731  procedure TForm1.FormResize(Sender: TObject);  procedure TForm1.FormResize(Sender: TObject);
732  begin  begin
733    Size := Min(ClientWidth, ClientHeight) div Count;    Size := Min(ClientWidth, ClientHeight) div bmp_count;
734    PaintTo(Canvas);    PaintTo(Canvas);
735  end;  end;
736    

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

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