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 26 by yamat0jp, Mon Jul 20 07:09:07 2015 UTC revision 31 by yamat0jp, Sun Aug 16 06:21:21 2015 UTC
# Line 21  type Line 21  type
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 30  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;
# Line 39  type Line 39  type
39      FActive: Boolean;      FActive: Boolean;
40      FList: 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; var Pos: TPoint): Boolean;      function NextStone(Stone: TStoneType; var Pos: TPoint): Boolean;
# Line 58  type Line 59  type
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 104  type Line 107  type
107      procedure MenuItem10Click(Sender: TObject);      procedure MenuItem10Click(Sender: TObject);
108      procedure MenuItem11Click(Sender: TObject);      procedure MenuItem11Click(Sender: TObject);
109      procedure Timer2Timer(Sender: TObject);      procedure Timer2Timer(Sender: TObject);
     procedure FormDeactivate(Sender: TObject);  
     procedure FormActivate(Sender: TObject);  
110    private    private
111      { Private 宣言 }      { Private 宣言 }
112      StoneGrid: TStoneGrid;      StoneGrid: TStoneGrid;
# Line 128  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  var
137    i, j: integer;    s: TPoint;
138  begin  begin
139    result := 0;    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
150      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
158    if CanSetStone(Stone, X, Y, true) = true then    if CanSetStone(Stone, X, Y, true) = true then
159    begin    begin
160      if Stone = stEffect then      Score := 0;
161        Stone := FEffectStone;      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;
     else  
       Exit;  
170      end;      end;
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    end;            inc(Score);
176              inc(Score, AddScore(i, j, worth));
177            end;
178      end
179      else
180        result := false;
181    FStrings := FBuffer[FTurnIndex];    FStrings := FBuffer[FTurnIndex];
182  end;  end;
183    
# Line 223  var Line 249  var
249    
250  begin  begin
251    result := false;    result := false;
   if Visible = true then  
   begin  
     FBool := FActive;  
     FActive := false;  
   end;  
252    p := true;    p := true;
253    if GetStrings(X, Y) = stNone then    if GetStrings(X, Y) = stNone then
254    begin    begin
# Line 277  begin Line 298  begin
298    inherited;    inherited;
299  end;  end;
300    
301    procedure TStoneGrid.GameOver;
302    begin
303      FGameOver := true;
304      FActive := false;
305    end;
306    
307    function TStoneGrid.GetActive: Boolean;
308    begin
309      if (FActive = true) and (FList.Count = 0) then
310        result := true
311      else
312        result := false;
313    end;
314    
315  function TStoneGrid.GetStrings(X, Y: integer): TStoneType;  function TStoneGrid.GetStrings(X, Y: integer): TStoneType;
316  begin  begin
317    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 322  begin Line 357  begin
357          FList.Delete(i);          FList.Delete(i);
358      if FList.Count = 0 then      if FList.Count = 0 then
359      begin      begin
       if FTerminated = true then  
         FActive := false  
       else  
         FActive := FBool;  
360        inc(FTurnIndex);        inc(FTurnIndex);
361        inc(FTurnNumber);        inc(FTurnNumber);
362        FBuffer[FTurnIndex] := FStrings;        FBuffer[FTurnIndex] := FStrings;
363          Form1.PaintBox1.Repaint;
364          Form1.ChangePlayer;
365          if FGameOver = false then
366            FActive := true
367      end;      end;
368      result := true;      result := true;
369    end;    end;
# Line 338  function TStoneGrid.NextStone(Stone: TSt Line 373  function TStoneGrid.NextStone(Stone: TSt
373  var  var
374    i, j, m, n: integer;    i, j, m, n: integer;
375  begin  begin
376    n := 0;    result:=false;
377      n:=0;
378    for i := 0 to Count - 1 do    for i := 0 to Count - 1 do
379      for j := 0 to Count - 1 do      for j := 0 to Count - 1 do
380      begin        if (CalScore(Stone, i, j, m) = true) and ((result = false)or(m < n)) then
       m := CalScore(Stone, i, j);  
       if ((n = 0) and (m > 0)) or ((0 < m) and (m < n)) then  
381        begin        begin
382          n := m;          if result = false then
383              result:=true;
384            n:=m;
385          Pos := Point(i, j);          Pos := Point(i, j);
386        end;        end;
     end;  
   result := not(n = 0);  
387  end;  end;
388    
389  procedure TStoneGrid.Paint(Canvas: TCanvas);  procedure TStoneGrid.Paint(Canvas: TCanvas);
# Line 378  end; Line 412  end;
412  procedure TStoneGrid.Pause;  procedure TStoneGrid.Pause;
413  begin  begin
414    FActive := false;    FActive := false;
   FTerminated := true;  
415  end;  end;
416    
417  procedure TStoneGrid.Restart;  procedure TStoneGrid.Restart;
418  begin  begin
419    FActive := true;    FActive := true;
420      FGameOver := false;
421    FTurnIndex := FTurnNumber;    FTurnIndex := FTurnNumber;
422    FTerminated := false;  end;
423    
424    procedure TStoneGrid.SetActive(const Value: Boolean);
425    begin
426      if (FGameOver = false) or (Value = false) then
427        FActive := Value;
428  end;  end;
429    
430  procedure TStoneGrid.SetStrings(X, Y: integer; const Value: TStoneType);  procedure TStoneGrid.SetStrings(X, Y: integer; const Value: TStoneType);
# Line 408  end; Line 447  end;
447  procedure TStoneGrid.Start;  procedure TStoneGrid.Start;
448  begin  begin
449    Clear;    Clear;
   FTerminated := false;  
450    FActive := true;    FActive := true;
451      FGameOver := false;
452  end;  end;
453    
454  { TForm1 }  { TForm1 }
# Line 452  begin Line 491  begin
491      Main;      Main;
492      if Execute = false then      if Execute = false then
493      begin      begin
       StoneGrid.Pause;  
494        m := 0;        m := 0;
495        n := 0;        n := 0;
496        for i := 0 to Count - 1 do        for i := 0 to Count - 1 do
# Line 470  begin Line 508  begin
508          s := 'Player2 Win:' + #13#10          s := 'Player2 Win:' + #13#10
509        else        else
510          s := 'Draw:' + #13#10;          s := 'Draw:' + #13#10;
511        Showmessage(s + '(Player1) ' + IntToStr(m) + #13#10 + '(Player2) ' +        StoneGrid.GameOver;
512          IntToStr(n));        Showmessage(s + '(Player1) ' + m.ToString+ #13#10 + '(Player2) ' +
513            n.ToString);
514      end      end
515      else      else
516        Caption := s;        Caption := s;
# Line 484  procedure TForm1.CompStone; Line 523  procedure TForm1.CompStone;
523  var  var
524    s: TPoint;    s: TPoint;
525  begin  begin
526      StoneGrid.Active := false;
527    if StoneGrid.NextStone(Index.Stone, s) = true then    if StoneGrid.NextStone(Index.Stone, s) = true then
528    begin    begin
529      StoneGrid.CanSetStone(Index.Stone, s.X, s.Y, true, true);      StoneGrid.CanSetStone(Index.Stone, s.X, s.Y, true, true);
530      PaintBox1.Repaint;      PaintBox1.Repaint;
531      end
532      else
533      ChangePlayer;      ChangePlayer;
   end;  
534  end;  end;
535    
536  procedure TForm1.GameStart;  procedure TForm1.GameStart;
537  begin  begin
   Timer1.Enabled := false;  
538    Index := Player1;    Index := Player1;
539    StoneGrid.Start;    StoneGrid.Start;
540    PaintBox1.Repaint;    PaintBox1.Repaint;
541    Caption := '黒から始めます';    Caption := '黒から始めます';
   Timer1.Enabled := true;  
542  end;  end;
543    
544  procedure TForm1.MenuItem10Click(Sender: TObject);  procedure TForm1.MenuItem10Click(Sender: TObject);
# Line 529  end; Line 568  end;
568    
569  procedure TForm1.MenuItem2Click(Sender: TObject);  procedure TForm1.MenuItem2Click(Sender: TObject);
570  begin  begin
571      Timer1.Enabled := false;
572      Timer2.Enabled := false;
573    GameStart;    GameStart;
574      Timer1.Enabled := true;
575      Timer2.Enabled := true;
576  end;  end;
577    
578  procedure TForm1.MenuItem4Click(Sender: TObject);  procedure TForm1.MenuItem4Click(Sender: TObject);
# Line 589  begin Line 632  begin
632    Size := Min(ClientWidth, ClientHeight) div Count;    Size := Min(ClientWidth, ClientHeight) div Count;
633  end;  end;
634    
 procedure TForm1.FormActivate(Sender: TObject);  
 begin  
   Timer1.Enabled := true;  
 end;  
   
635  procedure TForm1.FormCreate(Sender: TObject);  procedure TForm1.FormCreate(Sender: TObject);
636  begin  begin
637      ClientWidth := 50 * Count;
638      ClientHeight := 50 * Count;
639    StoneGrid := TStoneGrid.Create;    StoneGrid := TStoneGrid.Create;
640    StoneGrid.ImageCount(Form1.Image1.Bitmap.Width div Form1.Image3.Bitmap.Width,    StoneGrid.ImageCount(Form1.Image1.Bitmap.Width div Form1.Image3.Bitmap.Width,
641      Form1.Image1.Bitmap.Height div Form1.Image3.Bitmap.Height);      Form1.Image1.Bitmap.Height div Form1.Image3.Bitmap.Height);
# Line 614  begin Line 654  begin
654    GameStart;    GameStart;
655  end;  end;
656    
 procedure TForm1.FormDeactivate(Sender: TObject);  
 begin  
   Timer1.Enabled := false;  
 end;  
   
657  procedure TForm1.FormDestroy(Sender: TObject);  procedure TForm1.FormDestroy(Sender: TObject);
658  begin  begin
659    StoneGrid.Free;    StoneGrid.Free;
# Line 655  begin Line 690  begin
690    if Index.Auto = false then    if Index.Auto = false then
691    begin    begin
692      MenuItem10Click(Sender);      MenuItem10Click(Sender);
693        StoneGrid.Active := false;
694      if StoneGrid.CanSetStone(Index.Stone, Floor(Point.X / Size),      if StoneGrid.CanSetStone(Index.Stone, Floor(Point.X / Size),
695        Floor(Point.Y / Size), true, true) = true then        Floor(Point.Y / Size), true, true) = true then
     begin  
696        PaintBox1.Repaint;        PaintBox1.Repaint;
697        ChangePlayer;      StoneGrid.Active := true;
     end;  
698    end;    end;
699  end;  end;
700    

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

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