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 28 by yamat0jp, Fri Aug 14 07:21:57 2015 UTC revision 33 by yamat0jp, Wed Aug 19 14:41:09 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    TPlayer = class(TObject)
26    private    private
27      FAuto: Boolean;      FAuto: Boolean;
28      FStone: TStoneType;      FStone: TStoneType;
# Line 30  type Line 31  type
31      property Stone: TStoneType read FStone write FStone;      property Stone: TStoneType read FStone write FStone;
32    end;    end;
33    
34    TStoneGrid = class    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 129  implementation Line 131  implementation
131    
132  {$R *.fmx}  {$R *.fmx}
133  {$R *.Windows.fmx MSWINDOWS}  {$R *.Windows.fmx MSWINDOWS}
134    {$R *.XLgXhdpiTb.fmx ANDROID}
135  { TStoneGrid }  { TStoneGrid }
136    
137  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 144  begin Line 147  begin
147      end;      end;
148  end;  end;
149    
150  function TStoneGrid.CalScore(Stone: TStoneType; X, Y: integer): integer;  function TStoneGrid.CalScore(Stone: TStoneType; X, Y: integer;
151      out Score: integer): Boolean;
152  var  var
153    i, j: integer;    i, j: integer;
154    const
155      wast: array [1 .. 12] of TPoint = ((X: 1; Y: 0), (X: 6; Y: 0), (X: 0; Y: 1),
156        (X: 1; Y: 1), (X: 6; Y: 1), (X: 7; Y: 1), (X: 0; Y: 6), (X: 1; Y: 6), (X: 6;
157        Y: 6), (X: 7; Y: 6), (X: 1; Y: 7), (X: 6; Y: 7));
158      worth: array [1 .. 4] of TPoint = ((X: 0; Y: 0), (X: 7; Y: 0), (X: 0; Y: 7),
159        (X: 7; Y: 7));
160  begin  begin
161    if CanSetStone(Stone, X, Y, true) = true then    if CanSetStone(Stone, X, Y, true) = true then
162    begin    begin
163      result := 0;      Score := 0;
164      inc(result, AddScore(X, Y, [Point(1, 0), Point(6, 0), Point(0, 1),      result := true;
165        Point(1, 1), Point(6, 1), Point(7, 1), Point(0, 6), Point(1, 6),      if FTurnIndex < 50 then
166        Point(6, 6), Point(7, 6), Point(1, 7), Point(6, 7)]));        inc(Score, AddScore(X, Y, wast));
167        dec(Score, AddScore(X, Y, worth));
168      case Stone of      case Stone of
169        stBlack:        stBlack:
170          Stone := stWhite;          Stone := stWhite;
171        stWhite:        stWhite:
172          Stone := stBlack;          Stone := stBlack;
173      end;      end;
174      for i := 0 to Count - 1 do      for i := 0 to bmp_count - 1 do
175        for j := 0 to Count - 1 do        for j := 0 to bmp_count - 1 do
176          if CanSetStone(Stone, i, j, false) = true then          if CanSetStone(Stone, i, j, false) = true then
177          begin          begin
178            inc(result);            inc(Score);
179            inc(result, AddScore(i, j, [Point(0, 0), Point(7, 0), Point(0, 7),            inc(Score, AddScore(i, j, worth));
             Point(7, 7)]));  
180          end;          end;
181    end    end
182    else    else
183      result := -1;      result := false;
184    FStrings := FBuffer[FTurnIndex];    FStrings := FBuffer[FTurnIndex];
185  end;  end;
186    
# Line 179  function TStoneGrid.CanSetStone(Stone: T Line 189  function TStoneGrid.CanSetStone(Stone: T
189  var  var
190    i: integer;    i: integer;
191    p: Boolean;    p: Boolean;
192    q: ^TEffectData;    q: TEffectData;
193    procedure Method(m, n: integer);    procedure Method(m, n: integer);
194    var    var
195      s: TStoneType;      s: TStoneType;
196      j, k: integer;      j: integer;
197        k: integer;
198    begin    begin
199      if p = false then      if p = false then
200        Exit;        Exit;
# Line 203  var Line 214  var
214            result := true;            result := true;
215            if Reverse = true then            if Reverse = true then
216            begin            begin
217                Form1.PaintBox1.Repaint;
218              for j := 1 to i - 1 do              for j := 1 to i - 1 do
219              begin              begin
               Form1.PaintBox1.Repaint;  
220                if Visible = true then                if Visible = true then
221                begin                begin
222                  FEffectStone := Stone;                  FEffectStone := Stone;
223                  New(q);                  q.Left := X + m * j;
224                  q^.Left := X + m * j;                  q.Top := Y + n * j;
225                  q^.Top := Y + n * j;                  q.X := 0;
226                  q^.X := 0;                  q.Y := 0;
                 q^.Y := 0;  
227                  FList.Add(q);                  FList.Add(q);
228                  SetStrings(q^.Left, q^.Top, stEffect);                  SetStrings(q.Left, q.Top, stEffect);
229                  for k := 1 to 100 do                  for k := 1 to 10 do
230                  begin                  begin
231                    Sleep(1);                    Sleep(15);
232                    Application.ProcessMessages;                    Application.ProcessMessages;
233                  end;                  end;
234                end                end
# Line 260  procedure TStoneGrid.Clear; Line 270  procedure TStoneGrid.Clear;
270  var  var
271    i, j: integer;    i, j: integer;
272  begin  begin
   for i := 0 to FList.Count - 1 do  
     Dispose(FList[i]);  
273    FList.Clear;    FList.Clear;
274    for i := 0 to Count - 1 do    for i := 0 to bmp_count - 1 do
275      for j := 0 to Count - 1 do      for j := 0 to bmp_count - 1 do
276        Strings[i, j] := stNone;        Strings[i, j] := stNone;
277    Strings[3, 3] := stBlack;    Strings[3, 3] := stBlack;
278    Strings[4, 4] := stBlack;    Strings[4, 4] := stBlack;
# Line 278  end; Line 286  end;
286  constructor TStoneGrid.Create;  constructor TStoneGrid.Create;
287  begin  begin
288    inherited;    inherited;
289    FList := TList.Create;    FList := TList<TEffectData>.Create;
290  end;  end;
291    
292  destructor TStoneGrid.Destroy;  destructor TStoneGrid.Destroy;
 var  
   i: integer;  
293  begin  begin
   for i := 0 to FList.Count - 1 do  
     Dispose(FList[i]);  
294    FList.Free;    FList.Free;
295    inherited;    inherited;
296  end;  end;
# Line 307  end; Line 311  end;
311    
312  function TStoneGrid.GetStrings(X, Y: integer): TStoneType;  function TStoneGrid.GetStrings(X, Y: integer): TStoneType;
313  begin  begin
314    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
315      result := FStrings[X, Y]      result := FStrings[X, Y]
316    else    else
317      result := stError;      result := stError;
# Line 321  end; Line 325  end;
325    
326  function TStoneGrid.ListExecute: Boolean;  function TStoneGrid.ListExecute: Boolean;
327  var  var
   p: ^TEffectData;  
328    i: integer;    i: integer;
329      s: TEffectData;
330  begin  begin
331    if FList.Count = 0 then    if FList.Count = 0 then
332      result := false      result := false
333    else    else
334    begin    begin
335      for i := 0 to FList.Count - 1 do      i:=0;
336        while i < FList.Count do
337      begin      begin
338        p := FList[i];        s := FList[i];
339        if p^.X < FIndex_X - 1 then        if s.X < FIndex_X - 1 then
340          p^.X := p^.X + 1          s.X := s.X + 1
341        else if p^.Y < FIndex_Y - 1 then        else if s.Y < FIndex_Y - 1 then
342        begin        begin
343          p^.X := 0;          s.X := 0;
344          p^.Y := p^.Y + 1;          s.Y := s.Y + 1;
345        end        end
346        else        else
347        begin        begin
348          SetStrings(p^.Left, p^.Top, FEffectStone);          SetStrings(s.Left, s.Top, FEffectStone);
349          Dispose(p);          FList.Delete(i);
350          FList[i] := nil;          inc(i);
351            continue;
352        end;        end;
353          FList[i]:=s;
354          inc(i);
355      end;      end;
     for i := FList.Count - 1 downto 0 do  
       if FList[i] = nil then  
         FList.Delete(i);  
356      if FList.Count = 0 then      if FList.Count = 0 then
357      begin      begin
358        inc(FTurnIndex);        inc(FTurnIndex);
359        inc(FTurnNumber);        inc(FTurnNumber);
360        FBuffer[FTurnIndex] := FStrings;        FBuffer[FTurnIndex] := FStrings;
361          Form1.PaintBox1.Repaint;
362          Form1.ChangePlayer;
363          if FGameOver = false then
364            FActive := true
365      end;      end;
366      result := true;      result := true;
367    end;    end;
# Line 362  function TStoneGrid.NextStone(Stone: TSt Line 371  function TStoneGrid.NextStone(Stone: TSt
371  var  var
372    i, j, m, n: integer;    i, j, m, n: integer;
373  begin  begin
374    n := -1;    result := false;
375    for i := 0 to Count - 1 do    n := 0;
376      for j := 0 to Count - 1 do    for i := 0 to bmp_count - 1 do
377      begin      for j := 0 to bmp_count - 1 do
378        m := CalScore(Stone, i, j);        if (CalScore(Stone, i, j, m) = true) and ((result = false) or (m < n))
379        if (n = -1) or ((0 < m) and (m < n)) then        then
380        begin        begin
381            if result = false then
382              result := true;
383          n := m;          n := m;
384          Pos := Point(i, j);          Pos := Point(i, j);
385        end;        end;
     end;  
   result := not(n = -1);  
386  end;  end;
387    
388  procedure TStoneGrid.Paint(Canvas: TCanvas);  procedure TStoneGrid.Paint(Canvas: TCanvas);
389  var  var
   i: integer;  
390    k, m, n: integer;    k, m, n: integer;
391    s: TBitmap;    s: TBitmap;
392    p: ^TEffectData;    p: TEffectData;
393  begin  begin
394    m := Form1.Image3.Bitmap.Width;    m := Form1.Image3.Bitmap.Width;
395    n := Form1.Image3.Bitmap.Height;    n := Form1.Image3.Bitmap.Height;
396    k := Form1.Size;    k := Form1.Size;
397    for i := 0 to FList.Count - 1 do    if FEffectStone = stBlack then
398        s := Form1.Image1.Bitmap
399      else
400        s := Form1.Image2.Bitmap;
401      for p in FList do
402    begin    begin
403      p := FList[i];      Canvas.DrawBitmap(s, RectF(p.X * m, p.Y * n, (p.X + 1) * m, (p.Y + 1) * n),
404      if FEffectStone = stBlack then        RectF(p.Left * k, p.Top * k, (p.Left + 1) * k, (p.Top + 1) * k), 1);
       s := Form1.Image1.Bitmap  
     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);  
405    end;    end;
406  end;  end;
407    
# Line 413  end; Line 419  end;
419    
420  procedure TStoneGrid.SetActive(const Value: Boolean);  procedure TStoneGrid.SetActive(const Value: Boolean);
421  begin  begin
422    if FGameOver = false then    if (FGameOver = false) or (Value = false) then
423      FActive := Value;      FActive := Value;
424  end;  end;
425    
426  procedure TStoneGrid.SetStrings(X, Y: integer; const Value: TStoneType);  procedure TStoneGrid.SetStrings(X, Y: integer; const Value: TStoneType);
427  begin  begin
428    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
429      FStrings[X, Y] := Value;      FStrings[X, Y] := Value;
430  end;  end;
431    
# Line 436  end; Line 442  end;
442    
443  procedure TStoneGrid.Start;  procedure TStoneGrid.Start;
444  begin  begin
   FActive := true;  
445    Clear;    Clear;
446      FActive := true;
447    FGameOver := false;    FGameOver := false;
448  end;  end;
449    
# Line 464  var Line 470  var
470    var    var
471      i, j: integer;      i, j: integer;
472    begin    begin
473      for i := 0 to Count - 1 do      for i := 0 to bmp_count - 1 do
474        for j := 0 to Count - 1 do        for j := 0 to bmp_count - 1 do
475          if StoneGrid.CanSetStone(Index.Stone, i, j, false) = true then          if StoneGrid.CanSetStone(Index.Stone, i, j, false) = true then
476          begin          begin
477            result := true;            result := true;
# Line 481  begin Line 487  begin
487      Main;      Main;
488      if Execute = false then      if Execute = false then
489      begin      begin
       StoneGrid.Pause;  
490        m := 0;        m := 0;
491        n := 0;        n := 0;
492        for i := 0 to Count - 1 do        for i := 0 to bmp_count - 1 do
493          for j := 0 to Count - 1 do          for j := 0 to bmp_count - 1 do
494            case StoneGrid[i, j] of            case StoneGrid[i, j] of
495              stBlack:              stBlack:
496                inc(m);                inc(m);
# Line 500  begin Line 505  begin
505        else        else
506          s := 'Draw:' + #13#10;          s := 'Draw:' + #13#10;
507        StoneGrid.GameOver;        StoneGrid.GameOver;
508        Showmessage(s + '(Player1) ' + IntToStr(m) + #13#10 + '(Player2) ' +        Showmessage(s + '(Player1) ' + m.ToString + #13#10 + '(Player2) ' +
509          IntToStr(n));          n.ToString);
510      end      end
511      else      else
512        Caption := s;        Caption := s;
# Line 515  var Line 520  var
520    s: TPoint;    s: TPoint;
521  begin  begin
522    StoneGrid.Active := false;    StoneGrid.Active := false;
523    StoneGrid.NextStone(Index.Stone, s);    if StoneGrid.NextStone(Index.Stone, s) = true then
524    StoneGrid.CanSetStone(Index.Stone, s.X, s.Y, true, true);    begin
525    PaintBox1.Repaint;      StoneGrid.CanSetStone(Index.Stone, s.X, s.Y, true, true);
526    ChangePlayer;      PaintBox1.Repaint;
527    StoneGrid.Active := true;    end
528      else
529        ChangePlayer;
530  end;  end;
531    
532  procedure TForm1.GameStart;  procedure TForm1.GameStart;
# Line 586  var Line 593  var
593  begin  begin
594    if StoneGrid.Active = false then    if StoneGrid.Active = false then
595      StoneGrid.Paint(Canvas);      StoneGrid.Paint(Canvas);
596    for i := 0 to Count - 1 do    for i := 0 to bmp_count - 1 do
597    begin    begin
598      for j := 0 to Count - 1 do      for j := 0 to bmp_count - 1 do
599      begin      begin
600        case StoneGrid.Strings[i, j] of        case StoneGrid.Strings[i, j] of
601          stWhite:          stWhite:
# Line 606  begin Line 613  begin
613            Image5.Bitmap.Height), RectF(i * Size, j * Size, (i + 1) * Size,            Image5.Bitmap.Height), RectF(i * Size, j * Size, (i + 1) * Size,
614            (j + 1) * Size), 1);            (j + 1) * Size), 1);
615        end;        end;
616        Canvas.DrawLine(PointF(0, j * Size), PointF(Count * Size, j * Size), 1);        Canvas.DrawLine(PointF(0, j * Size), PointF(bmp_count * Size,
617            j * Size), 1);
618      end;      end;
619      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);
620    end;    end;
621    Canvas.DrawLine(PointF(Count * Size, 0),    Canvas.DrawLine(PointF(bmp_count * Size, 0), PointF(bmp_count * Size,
622      PointF(Count * Size, Count * Size), 1);      bmp_count * Size), 1);
623    Canvas.DrawLine(PointF(0, Count * Size),    Canvas.DrawLine(PointF(0, bmp_count * Size), PointF(bmp_count * Size,
624      PointF(Count * Size, Count * Size), 1);      bmp_count * Size), 1);
625  end;  end;
626    
627  procedure TForm1.PaintBox1Resize(Sender: TObject);  procedure TForm1.PaintBox1Resize(Sender: TObject);
628  begin  begin
629    Size := Min(ClientWidth, ClientHeight) div Count;    Size := Min(ClientWidth, ClientHeight) div bmp_count;
630  end;  end;
631    
632  procedure TForm1.FormCreate(Sender: TObject);  procedure TForm1.FormCreate(Sender: TObject);
633  begin  begin
634      ClientWidth := 50 * bmp_count;
635      ClientHeight := 50 * bmp_count;
636    StoneGrid := TStoneGrid.Create;    StoneGrid := TStoneGrid.Create;
637    StoneGrid.ImageCount(Form1.Image1.Bitmap.Width div Form1.Image3.Bitmap.Width,    StoneGrid.ImageCount(6, 5);
     Form1.Image1.Bitmap.Height div Form1.Image3.Bitmap.Height);  
638    Player1 := TPlayer.Create;    Player1 := TPlayer.Create;
639    Player2 := TPlayer.Create;    Player2 := TPlayer.Create;
640    Player1.Stone := stBlack;    Player1.Stone := stBlack;
# Line 668  end; Line 677  end;
677    
678  procedure TForm1.FormResize(Sender: TObject);  procedure TForm1.FormResize(Sender: TObject);
679  begin  begin
680    Size := Min(ClientWidth, ClientHeight) div Count;    Size := Min(ClientWidth, ClientHeight) div bmp_count;
681    PaintTo(Canvas);    PaintTo(Canvas);
682  end;  end;
683    
# Line 680  begin Line 689  begin
689      StoneGrid.Active := false;      StoneGrid.Active := false;
690      if StoneGrid.CanSetStone(Index.Stone, Floor(Point.X / Size),      if StoneGrid.CanSetStone(Index.Stone, Floor(Point.X / Size),
691        Floor(Point.Y / Size), true, true) = true then        Floor(Point.Y / Size), true, true) = true then
     begin  
692        PaintBox1.Repaint;        PaintBox1.Repaint;
       ChangePlayer;  
     end;  
693      StoneGrid.Active := true;      StoneGrid.Active := true;
694    end;    end;
695  end;  end;

Legend:
Removed from v.28  
changed lines
  Added in v.33

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