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 31 by yamat0jp, Sun Aug 16 06:21:21 2015 UTC revision 37 by yamat0jp, Sun Aug 30 08:32:57 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; out Score: integer): Boolean;      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 145  begin Line 148  begin
148      end;      end;
149  end;  end;
150    
151  function TStoneGrid.CalScore(Stone: TStoneType; X, Y: integer; out Score: integer): Boolean;  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  const
157    wast: array [0 .. 11] of TPoint = ((X: 1; Y: 0), (X: 6; Y: 0), (X: 0;    waste: array [1 .. 12] of TPoint = ((X: 1; Y: 0), (X: 6; Y: 0), (X: 0; Y: 1),
158      Y: 1), (X: 1; Y: 1), (X: 6; Y: 1), (X: 7; Y: 1), (X: 0; Y: 6), (X: 1; Y: 6),      (X: 1; Y: 1), (X: 6; Y: 1), (X: 7; Y: 1), (X: 0; Y: 6), (X: 1; Y: 6), (X: 6;
159      (X: 6; Y: 6), (X: 7; Y: 6), (X: 1; Y: 7), (X: 6; Y: 7));      Y: 6), (X: 7; Y: 6), (X: 1; Y: 7), (X: 6; Y: 7));
160    worth: array [0 .. 3] of TPoint = ((X: 0; Y: 0), (X: 7; Y: 0), (X: 0;    worth: array [1 .. 4] of TPoint = ((X: 0; Y: 0), (X: 7; Y: 0), (X: 0; Y: 7),
161      Y: 7), (X: 7; Y: 7));      (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                inc(Score)
189              else
190                dec(Score);
191              case Stone of
192                stBlack:
193                  Stone := stWhite;
194                stWhite:
195                  Stone := stBlack;
196              end;
197              Hard;
198              if loop > 1 then
199              begin
200                Easy;
201                FStrings := FBuffer[FTurnIndex + loop];
202              end
203              else
204                FBuffer[FTurnIndex + loop] := FStrings;
205            end;
206          end;
207        dec(loop);
208      end;
209    
210  begin  begin
211    if CanSetStone(Stone, X, Y, true) = true then    if CanSetStone(Stone, X, Y, true) = true then
212    begin    begin
213      Score := 0;      Score := 0;
214      result:=true;      result := true;
215      if FTurnIndex < 50 then      if FTurnIndex < 50 then
216        inc(Score, AddScore(X, Y, wast));        inc(Score, AddScore(X, Y, waste));
217      dec(Score, AddScore(X, Y, worth));      dec(Score, AddScore(X, Y, worth));
218      case Stone of      case Stone of
219        stBlack:        stBlack:
# Line 168  begin Line 221  begin
221        stWhite:        stWhite:
222          Stone := stBlack;          Stone := stBlack;
223      end;      end;
224      for i := 0 to Count - 1 do      if (Form1.MenuItem14.IsChecked = true) and (FTurnIndex + 2 <= 60) then
225        for j := 0 to Count - 1 do      begin
226          if CanSetStone(Stone, i, j, false) = true then        loop := 0;
227          begin        Hard;
228            inc(Score);      end
229            inc(Score, AddScore(i, j, worth));      else
230          end;        Easy;
231    end    end
232    else    else
233      result := false;      result := false;
# Line 186  function TStoneGrid.CanSetStone(Stone: T Line 239  function TStoneGrid.CanSetStone(Stone: T
239  var  var
240    i: integer;    i: integer;
241    p: Boolean;    p: Boolean;
242    q: ^TEffectData;    q: TEffectData;
243    procedure Method(m, n: integer);    procedure Method(m, n: integer);
244    var    var
245      s: TStoneType;      s: TStoneType;
246      j, k: integer;      j: integer;
247        k: integer;
248    begin    begin
249      if p = false then      if p = false then
250        Exit;        Exit;
# Line 210  var Line 264  var
264            result := true;            result := true;
265            if Reverse = true then            if Reverse = true then
266            begin            begin
267                Form1.PaintBox1.Repaint;
268              for j := 1 to i - 1 do              for j := 1 to i - 1 do
269              begin              begin
               Form1.PaintBox1.Repaint;  
270                if Visible = true then                if Visible = true then
271                begin                begin
272                  FEffectStone := Stone;                  FEffectStone := Stone;
273                  New(q);                  q.Left := X + m * j;
274                  q^.Left := X + m * j;                  q.Top := Y + n * j;
275                  q^.Top := Y + n * j;                  q.X := 0;
276                  q^.X := 0;                  q.Y := 0;
                 q^.Y := 0;  
277                  FList.Add(q);                  FList.Add(q);
278                  SetStrings(q^.Left, q^.Top, stEffect);                  SetStrings(q.Left, q.Top, stEffect);
279                  for k := 1 to 100 do                  for k := 1 to 10 do
280                  begin                  begin
281                    Sleep(1);                    Sleep(15);
282                    Application.ProcessMessages;                    Application.ProcessMessages;
283                  end;                  end;
284                end                end
# Line 267  procedure TStoneGrid.Clear; Line 320  procedure TStoneGrid.Clear;
320  var  var
321    i, j: integer;    i, j: integer;
322  begin  begin
   for i := 0 to FList.Count - 1 do  
     Dispose(FList[i]);  
323    FList.Clear;    FList.Clear;
324    for i := 0 to Count - 1 do    for i := 0 to bmp_count - 1 do
325      for j := 0 to Count - 1 do      for j := 0 to bmp_count - 1 do
326        Strings[i, j] := stNone;        Strings[i, j] := stNone;
327    Strings[3, 3] := stBlack;    Strings[3, 3] := stBlack;
328    Strings[4, 4] := stBlack;    Strings[4, 4] := stBlack;
# Line 285  end; Line 336  end;
336  constructor TStoneGrid.Create;  constructor TStoneGrid.Create;
337  begin  begin
338    inherited;    inherited;
339    FList := TList.Create;    FList := TList<TEffectData>.Create;
340  end;  end;
341    
342  destructor TStoneGrid.Destroy;  destructor TStoneGrid.Destroy;
 var  
   i: integer;  
343  begin  begin
   for i := 0 to FList.Count - 1 do  
     Dispose(FList[i]);  
344    FList.Free;    FList.Free;
345    inherited;    inherited;
346  end;  end;
# Line 314  end; Line 361  end;
361    
362  function TStoneGrid.GetStrings(X, Y: integer): TStoneType;  function TStoneGrid.GetStrings(X, Y: integer): TStoneType;
363  begin  begin
364    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
365      result := FStrings[X, Y]      result := FStrings[X, Y]
366    else    else
367      result := stError;      result := stError;
# Line 328  end; Line 375  end;
375    
376  function TStoneGrid.ListExecute: Boolean;  function TStoneGrid.ListExecute: Boolean;
377  var  var
   p: ^TEffectData;  
378    i: integer;    i: integer;
379      s: TEffectData;
380  begin  begin
381    if FList.Count = 0 then    if FList.Count = 0 then
382      result := false      result := false
383    else    else
384    begin    begin
385      for i := 0 to FList.Count - 1 do      i := 0;
386        while i < FList.Count do
387      begin      begin
388        p := FList[i];        s := FList[i];
389        if p^.X < FIndex_X - 1 then        if s.X < FIndex_X - 1 then
390          p^.X := p^.X + 1          s.X := s.X + 1
391        else if p^.Y < FIndex_Y - 1 then        else if s.Y < FIndex_Y - 1 then
392        begin        begin
393          p^.X := 0;          s.X := 0;
394          p^.Y := p^.Y + 1;          s.Y := s.Y + 1;
395        end        end
396        else        else
397        begin        begin
398          SetStrings(p^.Left, p^.Top, FEffectStone);          SetStrings(s.Left, s.Top, FEffectStone);
399          Dispose(p);          FList.Delete(i);
400          FList[i] := nil;          inc(i);
401            continue;
402        end;        end;
403          FList[i] := s;
404          inc(i);
405      end;      end;
     for i := FList.Count - 1 downto 0 do  
       if FList[i] = nil then  
         FList.Delete(i);  
406      if FList.Count = 0 then      if FList.Count = 0 then
407      begin      begin
408        inc(FTurnIndex);        inc(FTurnIndex);
# Line 373  function TStoneGrid.NextStone(Stone: TSt Line 421  function TStoneGrid.NextStone(Stone: TSt
421  var  var
422    i, j, m, n: integer;    i, j, m, n: integer;
423  begin  begin
424    result:=false;    result := false;
425    n:=0;    n := 0;
426    for i := 0 to Count - 1 do    for i := 0 to bmp_count - 1 do
427      for j := 0 to Count - 1 do      for j := 0 to bmp_count - 1 do
428        if (CalScore(Stone, i, j, m) = true) and ((result = false)or(m < n)) then        if (CalScore(Stone, i, j, m) = true) and ((result = false) or (m < n))
429          then
430        begin        begin
431          if result = false then          if result = false then
432            result:=true;            result := true;
433          n:=m;          n := m;
434          Pos := Point(i, j);          Pos := Point(i, j);
435        end;        end;
436  end;  end;
437    
438  procedure TStoneGrid.Paint(Canvas: TCanvas);  procedure TStoneGrid.Paint(Canvas: TCanvas);
439  var  var
440    i: integer;    k: integer;
   k, m, n: integer;  
441    s: TBitmap;    s: TBitmap;
442    p: ^TEffectData;    p: TEffectData;
443  begin  begin
   m := Form1.Image3.Bitmap.Width;  
   n := Form1.Image3.Bitmap.Height;  
444    k := Form1.Size;    k := Form1.Size;
445    for i := 0 to FList.Count - 1 do    if FEffectStone = stBlack then
446        s := Form1.Image1.Bitmap
447      else
448        s := Form1.Image2.Bitmap;
449      for p in FList do
450    begin    begin
451      p := FList[i];      Canvas.DrawBitmap(s, RectF(p.X * 50, p.Y * 50, (p.X + 1) * 50,
452      if FEffectStone = stBlack then        (p.Y + 1) * 50), RectF(p.Left * k, p.Top * k, (p.Left + 1) * k,
453        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);  
454    end;    end;
455  end;  end;
456    
# Line 429  end; Line 474  end;
474    
475  procedure TStoneGrid.SetStrings(X, Y: integer; const Value: TStoneType);  procedure TStoneGrid.SetStrings(X, Y: integer; const Value: TStoneType);
476  begin  begin
477    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
478      FStrings[X, Y] := Value;      FStrings[X, Y] := Value;
479  end;  end;
480    
# Line 474  var Line 519  var
519    var    var
520      i, j: integer;      i, j: integer;
521    begin    begin
522      for i := 0 to Count - 1 do      for i := 0 to bmp_count - 1 do
523        for j := 0 to Count - 1 do        for j := 0 to bmp_count - 1 do
524          if StoneGrid.CanSetStone(Index.Stone, i, j, false) = true then          if StoneGrid.CanSetStone(Index.Stone, i, j, false) = true then
525          begin          begin
526            result := true;            result := true;
# Line 493  begin Line 538  begin
538      begin      begin
539        m := 0;        m := 0;
540        n := 0;        n := 0;
541        for i := 0 to Count - 1 do        for i := 0 to bmp_count - 1 do
542          for j := 0 to Count - 1 do          for j := 0 to bmp_count - 1 do
543            case StoneGrid[i, j] of            case StoneGrid[i, j] of
544              stBlack:              stBlack:
545                inc(m);                inc(m);
# Line 509  begin Line 554  begin
554        else        else
555          s := 'Draw:' + #13#10;          s := 'Draw:' + #13#10;
556        StoneGrid.GameOver;        StoneGrid.GameOver;
557        Showmessage(s + '(Player1) ' + m.ToString+ #13#10 + '(Player2) ' +        Showmessage(s + '(Player1) ' + m.ToString + #13#10 + '(Player2) ' +
558          n.ToString);          n.ToString);
559      end      end
560      else      else
# Line 597  var Line 642  var
642  begin  begin
643    if StoneGrid.Active = false then    if StoneGrid.Active = false then
644      StoneGrid.Paint(Canvas);      StoneGrid.Paint(Canvas);
645    for i := 0 to Count - 1 do    for i := 0 to bmp_count - 1 do
646    begin    begin
647      for j := 0 to Count - 1 do      for j := 0 to bmp_count - 1 do
648      begin      begin
649        case StoneGrid.Strings[i, j] of        case StoneGrid.Strings[i, j] of
650          stWhite:          stWhite:
651            Canvas.DrawBitmap(Image4.Bitmap, RectF(0, 0, Image4.Bitmap.Width,            Canvas.DrawBitmap(Image3.Bitmap, RectF(100, 0, 150, 50),
652              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);  
653          stBlack:          stBlack:
654            Canvas.DrawBitmap(Image3.Bitmap, RectF(0, 0, Image3.Bitmap.Width,            Canvas.DrawBitmap(Image3.Bitmap, RectF(50, 0, 100, 50),
655              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);  
656          stEffect:          stEffect:
657            continue;            continue;
658        else        else
659          Canvas.DrawBitmap(Image5.Bitmap, RectF(0, 0, Image5.Bitmap.Width,          Canvas.DrawBitmap(Image3.Bitmap, RectF(0, 0, 50, 50),
660            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);  
661        end;        end;
662        Canvas.DrawLine(PointF(0, j * Size), PointF(Count * Size, j * Size), 1);        Canvas.DrawLine(PointF(0, j * Size), PointF(bmp_count * Size,
663            j * Size), 1);
664      end;      end;
665      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);
666    end;    end;
667    Canvas.DrawLine(PointF(Count * Size, 0),    Canvas.DrawLine(PointF(bmp_count * Size, 0), PointF(bmp_count * Size,
668      PointF(Count * Size, Count * Size), 1);      bmp_count * Size), 1);
669    Canvas.DrawLine(PointF(0, Count * Size),    Canvas.DrawLine(PointF(0, bmp_count * Size), PointF(bmp_count * Size,
670      PointF(Count * Size, Count * Size), 1);      bmp_count * Size), 1);
671  end;  end;
672    
673  procedure TForm1.PaintBox1Resize(Sender: TObject);  procedure TForm1.PaintBox1Resize(Sender: TObject);
674  begin  begin
675    Size := Min(ClientWidth, ClientHeight) div Count;    Size := Min(ClientWidth, ClientHeight) div bmp_count;
676  end;  end;
677    
678  procedure TForm1.FormCreate(Sender: TObject);  procedure TForm1.FormCreate(Sender: TObject);
679  begin  begin
680    ClientWidth := 50 * Count;    ClientWidth := 400;
681    ClientHeight := 50 * Count;    ClientHeight := 400;
682    StoneGrid := TStoneGrid.Create;    StoneGrid := TStoneGrid.Create;
683    StoneGrid.ImageCount(Form1.Image1.Bitmap.Width div Form1.Image3.Bitmap.Width,    StoneGrid.ImageCount(6, 5);
     Form1.Image1.Bitmap.Height div Form1.Image3.Bitmap.Height);  
684    Player1 := TPlayer.Create;    Player1 := TPlayer.Create;
685    Player2 := TPlayer.Create;    Player2 := TPlayer.Create;
686    Player1.Stone := stBlack;    Player1.Stone := stBlack;
# Line 681  end; Line 723  end;
723    
724  procedure TForm1.FormResize(Sender: TObject);  procedure TForm1.FormResize(Sender: TObject);
725  begin  begin
726    Size := Min(ClientWidth, ClientHeight) div Count;    Size := Min(ClientWidth, ClientHeight) div bmp_count;
727    PaintTo(Canvas);    PaintTo(Canvas);
728  end;  end;
729    

Legend:
Removed from v.31  
changed lines
  Added in v.37

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