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 21 by yamat0jp, Sat Jul 18 22:36:44 2015 UTC revision 32 by yamat0jp, Sun Aug 16 07:03:42 2015 UTC
# Line 17  type Line 17  type
17    TEffectData = record    TEffectData = record
18      X, Y: integer;      X, Y: integer;
19      Left, Top: integer;      Left, Top: integer;
     Stone: TStoneType;  
20    end;    end;
21    
22    TGridData = array [0 .. Count - 1] of array [0 .. Count - 1] of TStoneType;    TGridData = array [0 .. Count - 1] of array [0 .. Count - 1] of TStoneType;
23    
24    TPlayer = class    TPlayer = class(TObject)
25    private    private
26      FAuto: Boolean;      FAuto: Boolean;
27      FStone: TStoneType;      FStone: TStoneType;
# Line 31  type Line 30  type
30      property Stone: TStoneType read FStone write FStone;      property Stone: TStoneType read FStone write FStone;
31    end;    end;
32    
33    TStoneGrid = class    TStoneGrid = class(TObject)
34    private    private
35      FStrings: TGridData;      FStrings: TGridData;
36      FBuffer: array [0 .. Count * Count - 4] of TGridData;      FBuffer: array [0 .. Count * Count - 4] of TGridData;
37      FTurnNumber: integer;      FTurnNumber: integer;
38      FTurnIndex: integer;      FTurnIndex: integer;
39      FActive: Boolean;      FActive: Boolean;
40      List: TList;      FList: TList;
41      FEffectStone: TStoneType;      FEffectStone: TStoneType;
     FBool: Boolean;  
     FTerminated: Boolean;  
42      FIndex_X: integer;      FIndex_X: integer;
43      FIndex_Y: integer;      FIndex_Y: integer;
44        FGameOver: Boolean;
45      function GetStrings(X, Y: integer): TStoneType;      function GetStrings(X, Y: integer): TStoneType;
46      procedure SetStrings(X, Y: integer; const Value: TStoneType);      procedure SetStrings(X, Y: integer; const Value: TStoneType);
47      procedure SetTurnNumber(const Value: integer);      procedure SetTurnNumber(const Value: integer);
48        function GetActive: Boolean;
49        procedure SetActive(const Value: Boolean);
50    public    public
51      constructor Create;      constructor Create;
52      destructor Destroy; override;      destructor Destroy; override;
53      procedure Clear;      procedure Clear;
54      function CalScore(Stone: TStoneType; X, Y: integer): integer;      function CalScore(Stone: TStoneType; X, Y: integer; out Score: integer): Boolean;
55      function CanSetStone(Stone: TStoneType; X, Y: integer; Reverse: Boolean;      function CanSetStone(Stone: TStoneType; X, Y: integer; Reverse: Boolean;
56        const Visible: Boolean = false): Boolean;        const Visible: Boolean = false): Boolean;
57      function NextStone(Stone: TStoneType): TPoint;      function NextStone(Stone: TStoneType; var Pos: TPoint): Boolean;
58      procedure Start;      procedure Start;
59      procedure Restart;      procedure Restart;
60      procedure Pause;      procedure Pause;
61      function ListExecute: Boolean;      function ListExecute: Boolean;
62        procedure GameOver;
63      procedure Paint(Canvas: TCanvas);      procedure Paint(Canvas: TCanvas);
64      procedure ImageCount(X, Y: integer);      procedure ImageCount(X, Y: integer);
65        function AddScore(X, Y: integer; const NG: array of TPoint): integer;
66      property Strings[X, Y: integer]: TStoneType read GetStrings      property Strings[X, Y: integer]: TStoneType read GetStrings
67        write SetStrings; default;        write SetStrings; default;
68      property TurnNumber: integer read FTurnNumber write SetTurnNumber;      property TurnNumber: integer read FTurnNumber write SetTurnNumber;
69      property Active: Boolean read FActive;      property Active: Boolean read GetActive write SetActive;
70    end;    end;
71    
72    TForm1 = class(TForm)    TForm1 = class(TForm)
# Line 127  implementation Line 129  implementation
129    
130  {$R *.fmx}  {$R *.fmx}
131  {$R *.Windows.fmx MSWINDOWS}  {$R *.Windows.fmx MSWINDOWS}
132    {$R *.XLgXhdpiTb.fmx ANDROID}
133  { TStoneGrid }  { TStoneGrid }
134    
135  function TStoneGrid.CalScore(Stone: TStoneType; X, Y: integer): integer;  function TStoneGrid.AddScore(X, Y: integer; const NG: array of TPoint): integer;
136    var
137      s: TPoint;
138    begin
139      result := 0;
140      for s in NG do
141        if (X = s.X) and (Y = s.Y) then
142        begin
143          result := 10;
144          break;
145        end;
146    end;
147    
148    function TStoneGrid.CalScore(Stone: TStoneType; X, Y: integer; out Score: integer): Boolean;
149  var  var
150    i, j: integer;    i, j: integer;
151    const
152      wast: array [0 .. 11] of TPoint = ((X: 1; Y: 0), (X: 6; Y: 0), (X: 0;
153        Y: 1), (X: 1; Y: 1), (X: 6; Y: 1), (X: 7; Y: 1), (X: 0; Y: 6), (X: 1; Y: 6),
154        (X: 6; Y: 6), (X: 7; Y: 6), (X: 1; Y: 7), (X: 6; Y: 7));
155      worth: array [0 .. 3] of TPoint = ((X: 0; Y: 0), (X: 7; Y: 0), (X: 0;
156        Y: 7), (X: 7; Y: 7));
157  begin  begin
158    if CanSetStone(Stone, X, Y, true) = true then    if CanSetStone(Stone, X, Y, true) = true then
159    begin    begin
160        Score := 0;
161        result:=true;
162        if FTurnIndex < 50 then
163          inc(Score, AddScore(X, Y, wast));
164        dec(Score, AddScore(X, Y, worth));
165      case Stone of      case Stone of
166      stBlack:        stBlack:
167        Stone := stWhite;          Stone := stWhite;
168      stWhite:        stWhite:
169        Stone := stBlack;          Stone := stBlack;
     stEffect:  
       Stone := FEffectStone;  
170      end;      end;
     result := 0;  
171      for i := 0 to Count - 1 do      for i := 0 to Count - 1 do
172        for j := 0 to Count - 1 do        for j := 0 to Count - 1 do
173          if CanSetStone(Stone, i, j, false) = true then          if CanSetStone(Stone, i, j, false) = true then
174            inc(result);          begin
175      FStrings := FBuffer[FTurnIndex];            inc(Score);
176              inc(Score, AddScore(i, j, worth));
177            end;
178    end    end
179    else    else
180    begin      result := false;
181      FStrings := FBuffer[FTurnIndex];    FStrings := FBuffer[FTurnIndex];
     result := -1;  
   end;  
182  end;  end;
183    
184  function TStoneGrid.CanSetStone(Stone: TStoneType; X, Y: integer;  function TStoneGrid.CanSetStone(Stone: TStoneType; X, Y: integer;
# Line 166  var Line 190  var
190    procedure Method(m, n: integer);    procedure Method(m, n: integer);
191    var    var
192      s: TStoneType;      s: TStoneType;
193      j, k: integer;      j: integer;
194        k: Integer;
195    begin    begin
196      if p = false then      if p = false then
197        Exit;        Exit;
# Line 175  var Line 200  var
200      begin      begin
201        s := GetStrings(X + m * i, Y + n * i);        s := GetStrings(X + m * i, Y + n * i);
202        if s = stEffect then        if s = stEffect then
203          s:=FEffectStone;          s := FEffectStone;
204        if (s = stNone) or (s = stError) then        if (s = stNone) or (s = stError) then
205          break          break
206        else if s = Stone then        else if s = Stone then
# Line 186  var Line 211  var
211            result := true;            result := true;
212            if Reverse = true then            if Reverse = true then
213            begin            begin
214                Form1.PaintBox1.Repaint;
215              for j := 1 to i - 1 do              for j := 1 to i - 1 do
216              begin              begin
               Form1.PaintBox1.Repaint;  
217                if Visible = true then                if Visible = true then
218                begin                begin
219                  FEffectStone:=Stone;                  FEffectStone := Stone;
220                  New(q);                  New(q);
221                  q^.Left := X + m * j;                  q^.Left := X + m * j;
222                  q^.Top := Y + n * j;                  q^.Top := Y + n * j;
                 q^.Stone := Stone;  
223                  q^.X := 0;                  q^.X := 0;
224                  q^.Y := 0;                  q^.Y := 0;
225                  List.Add(q);                  FList.Add(q);
226                  SetStrings(q^.Left, q^.Top, stEffect);                  SetStrings(q^.Left, q^.Top, stEffect);
227                  for k := 1 to 100 do                  for k := 1 to 10 do
228                  begin                  begin
229                    Sleep(1);                    Sleep(15);
230                    Application.ProcessMessages;                    Application.ProcessMessages;
231                  end;                  end;
232                end                end
# Line 226  var Line 250  var
250    
251  begin  begin
252    result := false;    result := false;
   if Visible = true then  
   begin  
     FBool := FActive;  
     FActive := false;  
   end;  
253    p := true;    p := true;
254    if GetStrings(X, Y) = stNone then    if GetStrings(X, Y) = stNone then
255    begin    begin
# Line 249  procedure TStoneGrid.Clear; Line 268  procedure TStoneGrid.Clear;
268  var  var
269    i, j: integer;    i, j: integer;
270  begin  begin
271      for i := 0 to FList.Count - 1 do
272        Dispose(FList[i]);
273      FList.Clear;
274    for i := 0 to Count - 1 do    for i := 0 to Count - 1 do
275      for j := 0 to Count - 1 do      for j := 0 to Count - 1 do
276        Strings[i, j] := stNone;        Strings[i, j] := stNone;
# Line 264  end; Line 286  end;
286  constructor TStoneGrid.Create;  constructor TStoneGrid.Create;
287  begin  begin
288    inherited;    inherited;
289    List := TList.Create;    FList := TList.Create;
290  end;  end;
291    
292  destructor TStoneGrid.Destroy;  destructor TStoneGrid.Destroy;
293  var  var
294    i: integer;    i: integer;
295  begin  begin
296    for i := 0 to List.Count - 1 do    for i := 0 to FList.Count - 1 do
297      Dispose(List[i]);      Dispose(FList[i]);
298    List.Free;    FList.Free;
299    inherited;    inherited;
300  end;  end;
301    
302    procedure TStoneGrid.GameOver;
303    begin
304      FGameOver := true;
305      FActive := false;
306    end;
307    
308    function TStoneGrid.GetActive: Boolean;
309    begin
310      if (FActive = true) and (FList.Count = 0) then
311        result := true
312      else
313        result := false;
314    end;
315    
316  function TStoneGrid.GetStrings(X, Y: integer): TStoneType;  function TStoneGrid.GetStrings(X, Y: integer): TStoneType;
317  begin  begin
318    if (X >= 0) and (X < Count) and (Y >= 0) and (Y < Count) then    if (X >= 0) and (X < Count) and (Y >= 0) and (Y < Count) then
# Line 296  var Line 332  var
332    p: ^TEffectData;    p: ^TEffectData;
333    i: integer;    i: integer;
334  begin  begin
335    if List.Count = 0 then    if FList.Count = 0 then
336      result := false      result := false
337    else    else
338    begin    begin
339      for i := 0 to List.Count - 1 do      for i := 0 to FList.Count - 1 do
340      begin      begin
341        p := List.List[i];        p := FList[i];
342        if p^.X < FIndex_X - 1 then        if p^.X < FIndex_X - 1 then
343          p^.X := p^.X + 1          p^.X := p^.X + 1
344        else if p^.Y < FIndex_Y - 1 then        else if p^.Y < FIndex_Y - 1 then
# Line 312  begin Line 348  begin
348        end        end
349        else        else
350        begin        begin
351          SetStrings(p^.Left, p^.Top, p^.Stone);          SetStrings(p^.Left, p^.Top, FEffectStone);
352          Dispose(p);          Dispose(p);
353          List[i] := nil;          FList[i] := nil;
354        end;        end;
355      end;      end;
356      for i := List.Count - 1 downto 0 do      for i := FList.Count - 1 downto 0 do
357        if List[i] = nil then        if FList[i] = nil then
358          List.Delete(i);          FList.Delete(i);
359      if List.Count = 0 then      if FList.Count = 0 then
360      begin      begin
       if FTerminated = true then  
         FActive:=false  
       else  
         FActive := FBool;  
361        inc(FTurnIndex);        inc(FTurnIndex);
362        inc(FTurnNumber);        inc(FTurnNumber);
363        FBuffer[FTurnIndex] := FStrings;        FBuffer[FTurnIndex] := FStrings;
364          Form1.PaintBox1.Repaint;
365          Form1.ChangePlayer;
366          if FGameOver = false then
367            FActive := true
368      end;      end;
369      result := true;      result := true;
370    end;    end;
371  end;  end;
372    
373  function TStoneGrid.NextStone(Stone: TStoneType): TPoint;  function TStoneGrid.NextStone(Stone: TStoneType; var Pos: TPoint): Boolean;
374  var  var
375    i, j, m, n: integer;    i, j, m, n: integer;
376  begin  begin
377    n := -1;    result:=false;
378      n:=0;
379    for i := 0 to Count - 1 do    for i := 0 to Count - 1 do
380      for j := 0 to Count - 1 do      for j := 0 to Count - 1 do
381      begin        if (CalScore(Stone, i, j, m) = true) and ((result = false)or(m < n)) then
       m := CalScore(Stone, i, j);  
       if (n = -1) or ((m > -1) and (n > m)) then  
382        begin        begin
383          n := m;          if result = false then
384          result := Point(i, j);            result:=true;
385            n:=m;
386            Pos := Point(i, j);
387        end;        end;
     end;  
   if n = -1 then  
     result := Point(-1, -1);  
388  end;  end;
389    
390  procedure TStoneGrid.Paint(Canvas: TCanvas);  procedure TStoneGrid.Paint(Canvas: TCanvas);
# Line 363  begin Line 397  begin
397    m := Form1.Image3.Bitmap.Width;    m := Form1.Image3.Bitmap.Width;
398    n := Form1.Image3.Bitmap.Height;    n := Form1.Image3.Bitmap.Height;
399    k := Form1.Size;    k := Form1.Size;
400    for i := 0 to List.Count - 1 do    for i := 0 to FList.Count - 1 do
401    begin    begin
402      p := List[i];      p := FList[i];
403      if p^.Stone = stBlack then      if FEffectStone = stBlack then
404        s := Form1.Image1.Bitmap        s := Form1.Image1.Bitmap
405      else      else
406        s := Form1.Image2.Bitmap;        s := Form1.Image2.Bitmap;
# Line 378  end; Line 412  end;
412    
413  procedure TStoneGrid.Pause;  procedure TStoneGrid.Pause;
414  begin  begin
415    FTerminated:=true;    FActive := false;
416  end;  end;
417    
418  procedure TStoneGrid.Restart;  procedure TStoneGrid.Restart;
419  begin  begin
420    if FTerminated = true then    FActive := true;
421    begin    FGameOver := false;
422      FActive := true;    FTurnIndex := FTurnNumber;
423      FTurnIndex := FTurnNumber;  end;
424      FTerminated:=false;  
425    end;  procedure TStoneGrid.SetActive(const Value: Boolean);
426    begin
427      if (FGameOver = false) or (Value = false) then
428        FActive := Value;
429  end;  end;
430    
431  procedure TStoneGrid.SetStrings(X, Y: integer; const Value: TStoneType);  procedure TStoneGrid.SetStrings(X, Y: integer; const Value: TStoneType);
# Line 411  end; Line 448  end;
448  procedure TStoneGrid.Start;  procedure TStoneGrid.Start;
449  begin  begin
450    Clear;    Clear;
   FTerminated:=false;  
451    FActive := true;    FActive := true;
452      FGameOver := false;
453  end;  end;
454    
455  { TForm1 }  { TForm1 }
# Line 437  var Line 474  var
474    function Execute: Boolean;    function Execute: Boolean;
475    var    var
476      i, j: integer;      i, j: integer;
     m: integer;  
     n: integer;  
477    begin    begin
478      for i := 0 to Count - 1 do      for i := 0 to Count - 1 do
479        for j := 0 to Count - 1 do        for j := 0 to Count - 1 do
# Line 457  begin Line 492  begin
492      Main;      Main;
493      if Execute = false then      if Execute = false then
494      begin      begin
       StoneGrid.Pause;  
495        m := 0;        m := 0;
496        n := 0;        n := 0;
497        for i := 0 to Count - 1 do        for i := 0 to Count - 1 do
# Line 475  begin Line 509  begin
509          s := 'Player2 Win:' + #13#10          s := 'Player2 Win:' + #13#10
510        else        else
511          s := 'Draw:' + #13#10;          s := 'Draw:' + #13#10;
512        Showmessage(s + '(Player1) ' + IntToStr(m) + #13#10 + '(Player2) ' +        StoneGrid.GameOver;
513          IntToStr(n));        Showmessage(s + '(Player1) ' + m.ToString+ #13#10 + '(Player2) ' +
514            n.ToString);
515      end      end
516      else      else
517        Caption := s;        Caption := s;
# Line 489  procedure TForm1.CompStone; Line 524  procedure TForm1.CompStone;
524  var  var
525    s: TPoint;    s: TPoint;
526  begin  begin
527    s := StoneGrid.NextStone(Index.Stone);    StoneGrid.Active := false;
528    StoneGrid.CanSetStone(Index.Stone, s.X, s.Y, true, true);    if StoneGrid.NextStone(Index.Stone, s) = true then
529    PaintBox1.Repaint;    begin
530    ChangePlayer;      StoneGrid.CanSetStone(Index.Stone, s.X, s.Y, true, true);
531        PaintBox1.Repaint;
532      end
533      else
534        ChangePlayer;
535  end;  end;
536    
537  procedure TForm1.GameStart;  procedure TForm1.GameStart;
# Line 530  end; Line 569  end;
569    
570  procedure TForm1.MenuItem2Click(Sender: TObject);  procedure TForm1.MenuItem2Click(Sender: TObject);
571  begin  begin
572      Timer1.Enabled := false;
573      Timer2.Enabled := false;
574    GameStart;    GameStart;
575      Timer1.Enabled := true;
576      Timer2.Enabled := true;
577  end;  end;
578    
579  procedure TForm1.MenuItem4Click(Sender: TObject);  procedure TForm1.MenuItem4Click(Sender: TObject);
# Line 592  end; Line 635  end;
635    
636  procedure TForm1.FormCreate(Sender: TObject);  procedure TForm1.FormCreate(Sender: TObject);
637  begin  begin
638      ClientWidth := 50 * Count;
639      ClientHeight := 50 * Count;
640    StoneGrid := TStoneGrid.Create;    StoneGrid := TStoneGrid.Create;
641    StoneGrid.ImageCount(Form1.Image1.Bitmap.Width div Form1.Image3.Bitmap.Width,    StoneGrid.ImageCount(Form1.Image1.Bitmap.Width div Form1.Image3.Bitmap.Width,
642      Form1.Image1.Bitmap.Height div Form1.Image3.Bitmap.Height);      Form1.Image1.Bitmap.Height div Form1.Image3.Bitmap.Height);
# Line 631  end; Line 676  end;
676    
677  procedure TForm1.Timer2Timer(Sender: TObject);  procedure TForm1.Timer2Timer(Sender: TObject);
678  begin  begin
679    if (StoneGrid.Active = false)and(StoneGrid.ListExecute = true) then    if (StoneGrid.Active = false) and (StoneGrid.ListExecute = true) then
680      PaintBox1.Repaint;      PaintBox1.Repaint;
681  end;  end;
682    
# Line 646  begin Line 691  begin
691    if Index.Auto = false then    if Index.Auto = false then
692    begin    begin
693      MenuItem10Click(Sender);      MenuItem10Click(Sender);
694        StoneGrid.Active := false;
695      if StoneGrid.CanSetStone(Index.Stone, Floor(Point.X / Size),      if StoneGrid.CanSetStone(Index.Stone, Floor(Point.X / Size),
696        Floor(Point.Y / Size), true, true) = true then        Floor(Point.Y / Size), true, true) = true then
     begin  
697        PaintBox1.Repaint;        PaintBox1.Repaint;
698        ChangePlayer;      StoneGrid.Active := true;
     end;  
699    end;    end;
700  end;  end;
701    

Legend:
Removed from v.21  
changed lines
  Added in v.32

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