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 25 by yamat0jp, Mon Jul 20 00:50:13 2015 UTC revision 29 by yamat0jp, Sat Aug 15 12:32:41 2015 UTC
# 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;
# Line 53  type Line 54  type
54      function CalScore(Stone: TStoneType; X, Y: integer): integer;      function CalScore(Stone: TStoneType; X, Y: integer): integer;
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 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 130  implementation Line 131  implementation
131  {$R *.Windows.fmx MSWINDOWS}  {$R *.Windows.fmx MSWINDOWS}
132  { TStoneGrid }  { TStoneGrid }
133    
134    function TStoneGrid.AddScore(X, Y: integer; const NG: array of TPoint): integer;
135    var
136      s: TPoint;
137    begin
138      result := 0;
139      for s in NG do
140        if (X = s.X) and (Y = s.Y) then
141        begin
142          result := 10;
143          break;
144        end;
145    end;
146    
147  function TStoneGrid.CalScore(Stone: TStoneType; X, Y: integer): integer;  function TStoneGrid.CalScore(Stone: TStoneType; X, Y: integer): integer;
148  var  var
149    i, j: integer;    i, j: integer;
150  begin  begin
   result:=0;  
151    if CanSetStone(Stone, X, Y, true) = true then    if CanSetStone(Stone, X, Y, true) = true then
152    begin    begin
153      if Stone = stEffect then      result := 0;
154        Stone:=FEffectStone;      inc(result, AddScore(X, Y, [Point(1, 0), Point(6, 0), Point(0, 1),
155          Point(1, 1), Point(6, 1), Point(7, 1), Point(0, 6), Point(1, 6),
156          Point(6, 6), Point(7, 6), Point(1, 7), Point(6, 7)]));
157      case Stone of      case Stone of
158        stBlack:        stBlack:
159          Stone := stWhite;          Stone := stWhite;
160        stWhite:        stWhite:
161          Stone := stBlack;          Stone := stBlack;
       else  
         Exit;  
162      end;      end;
163      for i := 0 to Count - 1 do      for i := 0 to Count - 1 do
164        for j := 0 to Count - 1 do        for j := 0 to Count - 1 do
165          if CanSetStone(Stone, i, j, false) = true then          if CanSetStone(Stone, i, j, false) = true then
166            begin
167            inc(result);            inc(result);
168    end;            inc(result, AddScore(i, j, [Point(0, 0), Point(7, 0), Point(0, 7),
169                Point(7, 7)]));
170            end;
171      end
172      else
173        result := -1;
174    FStrings := FBuffer[FTurnIndex];    FStrings := FBuffer[FTurnIndex];
175  end;  end;
176    
# Line 223  var Line 242  var
242    
243  begin  begin
244    result := false;    result := false;
   if Visible = true then  
   begin  
     FBool := FActive;  
     FActive := false;  
   end;  
245    p := true;    p := true;
246    if GetStrings(X, Y) = stNone then    if GetStrings(X, Y) = stNone then
247    begin    begin
# Line 277  begin Line 291  begin
291    inherited;    inherited;
292  end;  end;
293    
294    procedure TStoneGrid.GameOver;
295    begin
296      FGameOver := true;
297      FActive := false;
298    end;
299    
300    function TStoneGrid.GetActive: Boolean;
301    begin
302      if (FActive = true) and (FList.Count = 0) then
303        result := true
304      else
305        result := false;
306    end;
307    
308  function TStoneGrid.GetStrings(X, Y: integer): TStoneType;  function TStoneGrid.GetStrings(X, Y: integer): TStoneType;
309  begin  begin
310    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 350  begin
350          FList.Delete(i);          FList.Delete(i);
351      if FList.Count = 0 then      if FList.Count = 0 then
352      begin      begin
       if FTerminated = true then  
         FActive := false  
       else  
         FActive := FBool;  
353        inc(FTurnIndex);        inc(FTurnIndex);
354        inc(FTurnNumber);        inc(FTurnNumber);
355        FBuffer[FTurnIndex] := FStrings;        FBuffer[FTurnIndex] := FStrings;
356          if FGameOver = false then
357          begin
358            FActive:=true;
359            Form1.ChangePlayer;
360          end;
361      end;      end;
362      result := true;      result := true;
363    end;    end;
364  end;  end;
365    
366  function TStoneGrid.NextStone(Stone: TStoneType): TPoint;  function TStoneGrid.NextStone(Stone: TStoneType; var Pos: TPoint): Boolean;
367  var  var
368    i, j, m, n: integer;    i, j, m, n: integer;
369  begin  begin
370    n := 0;    n := -1;
371    for i := 0 to Count - 1 do    for i := 0 to Count - 1 do
372      for j := 0 to Count - 1 do      for j := 0 to Count - 1 do
373      begin      begin
374        m := CalScore(Stone, i, j);        m := CalScore(Stone, i, j);
375        if ((n = 0)and(m > 0)) or ((0 < m) and (m < n)) then        if (n = -1) or ((0 < m) and (m < n)) then
376        begin        begin
377          n := m;          n := m;
378          result := Point(i, j);          Pos := Point(i, j);
379        end;        end;
380      end;      end;
381    if n = 0 then    result := not(n = -1);
     result := Point(-1, -1);  
382  end;  end;
383    
384  procedure TStoneGrid.Paint(Canvas: TCanvas);  procedure TStoneGrid.Paint(Canvas: TCanvas);
# Line 379  end; Line 407  end;
407  procedure TStoneGrid.Pause;  procedure TStoneGrid.Pause;
408  begin  begin
409    FActive := false;    FActive := false;
   FTerminated := true;  
410  end;  end;
411    
412  procedure TStoneGrid.Restart;  procedure TStoneGrid.Restart;
413  begin  begin
414    FActive := true;    FActive:=true;
415      FGameOver := false;
416    FTurnIndex := FTurnNumber;    FTurnIndex := FTurnNumber;
417    FTerminated := false;  end;
418    
419    procedure TStoneGrid.SetActive(const Value: Boolean);
420    begin
421      if (FGameOver = false)or(Value = false) then
422        FActive := Value;
423  end;  end;
424    
425  procedure TStoneGrid.SetStrings(X, Y: integer; const Value: TStoneType);  procedure TStoneGrid.SetStrings(X, Y: integer; const Value: TStoneType);
# Line 409  end; Line 442  end;
442  procedure TStoneGrid.Start;  procedure TStoneGrid.Start;
443  begin  begin
444    Clear;    Clear;
   FTerminated := false;  
445    FActive := true;    FActive := true;
446      FGameOver := false;
447  end;  end;
448    
449  { TForm1 }  { TForm1 }
# Line 453  begin Line 486  begin
486      Main;      Main;
487      if Execute = false then      if Execute = false then
488      begin      begin
       StoneGrid.Pause;  
489        m := 0;        m := 0;
490        n := 0;        n := 0;
491        for i := 0 to Count - 1 do        for i := 0 to Count - 1 do
# Line 471  begin Line 503  begin
503          s := 'Player2 Win:' + #13#10          s := 'Player2 Win:' + #13#10
504        else        else
505          s := 'Draw:' + #13#10;          s := 'Draw:' + #13#10;
506          StoneGrid.GameOver;
507        Showmessage(s + '(Player1) ' + IntToStr(m) + #13#10 + '(Player2) ' +        Showmessage(s + '(Player1) ' + IntToStr(m) + #13#10 + '(Player2) ' +
508          IntToStr(n));          IntToStr(n));
509      end      end
# Line 485  procedure TForm1.CompStone; Line 518  procedure TForm1.CompStone;
518  var  var
519    s: TPoint;    s: TPoint;
520  begin  begin
521    s := StoneGrid.NextStone(Index.Stone);    StoneGrid.Active := false;
522      StoneGrid.NextStone(Index.Stone, s);
523    StoneGrid.CanSetStone(Index.Stone, s.X, s.Y, true, true);    StoneGrid.CanSetStone(Index.Stone, s.X, s.Y, true, true);
524    PaintBox1.Repaint;    PaintBox1.Repaint;
   ChangePlayer;  
525  end;  end;
526    
527  procedure TForm1.GameStart;  procedure TForm1.GameStart;
528  begin  begin
   Timer1.Enabled:=false;  
529    Index := Player1;    Index := Player1;
530    StoneGrid.Start;    StoneGrid.Start;
531    PaintBox1.Repaint;    PaintBox1.Repaint;
532    Caption := '黒から始めます';    Caption := '黒から始めます';
   Timer1.Enabled:=true;  
533  end;  end;
534    
535  procedure TForm1.MenuItem10Click(Sender: TObject);  procedure TForm1.MenuItem10Click(Sender: TObject);
# Line 528  end; Line 559  end;
559    
560  procedure TForm1.MenuItem2Click(Sender: TObject);  procedure TForm1.MenuItem2Click(Sender: TObject);
561  begin  begin
562      Timer1.Enabled := false;
563      Timer2.Enabled := false;
564    GameStart;    GameStart;
565      Timer1.Enabled := true;
566      Timer2.Enabled := true;
567  end;  end;
568    
569  procedure TForm1.MenuItem4Click(Sender: TObject);  procedure TForm1.MenuItem4Click(Sender: TObject);
# Line 588  begin Line 623  begin
623    Size := Min(ClientWidth, ClientHeight) div Count;    Size := Min(ClientWidth, ClientHeight) div Count;
624  end;  end;
625    
 procedure TForm1.FormActivate(Sender: TObject);  
 begin  
   Timer1.Enabled := true;  
 end;  
   
626  procedure TForm1.FormCreate(Sender: TObject);  procedure TForm1.FormCreate(Sender: TObject);
627  begin  begin
628    StoneGrid := TStoneGrid.Create;    StoneGrid := TStoneGrid.Create;
# Line 613  begin Line 643  begin
643    GameStart;    GameStart;
644  end;  end;
645    
 procedure TForm1.FormDeactivate(Sender: TObject);  
 begin  
   Timer1.Enabled := false;  
 end;  
   
646  procedure TForm1.FormDestroy(Sender: TObject);  procedure TForm1.FormDestroy(Sender: TObject);
647  begin  begin
648    StoneGrid.Free;    StoneGrid.Free;
# Line 654  begin Line 679  begin
679    if Index.Auto = false then    if Index.Auto = false then
680    begin    begin
681      MenuItem10Click(Sender);      MenuItem10Click(Sender);
682        StoneGrid.Active := false;
683      if StoneGrid.CanSetStone(Index.Stone, Floor(Point.X / Size),      if StoneGrid.CanSetStone(Index.Stone, Floor(Point.X / Size),
684        Floor(Point.Y / Size), true, true) = true then        Floor(Point.Y / Size), true, true) = true then
     begin  
685        PaintBox1.Repaint;        PaintBox1.Repaint;
686        ChangePlayer;      StoneGrid.Active := true;
     end;  
687    end;    end;
688  end;  end;
689    

Legend:
Removed from v.25  
changed lines
  Added in v.29

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