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 20 by yamat0jp, Sat Jul 18 21:05:05 2015 UTC revision 31 by yamat0jp, Sun Aug 16 06:21:21 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;  
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 126  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 174  var Line 199  var
199      begin      begin
200        s := GetStrings(X + m * i, Y + n * i);        s := GetStrings(X + m * i, Y + n * i);
201        if s = stEffect then        if s = stEffect then
202          s:=FEffectStone;          s := FEffectStone;
203        if (s = stNone) or (s = stError) then        if (s = stNone) or (s = stError) then
204          break          break
205        else if s = Stone then        else if s = Stone then
# Line 190  var Line 215  var
215                Form1.PaintBox1.Repaint;                Form1.PaintBox1.Repaint;
216                if Visible = true then                if Visible = true then
217                begin                begin
218                  FEffectStone:=Stone;                  FEffectStone := Stone;
219                  New(q);                  New(q);
220                  q^.Left := X + m * j;                  q^.Left := X + m * j;
221                  q^.Top := Y + n * j;                  q^.Top := Y + n * j;
                 q^.Stone := Stone;  
222                  q^.X := 0;                  q^.X := 0;
223                  q^.Y := 0;                  q^.Y := 0;
224                  List.Add(q);                  FList.Add(q);
225                  SetStrings(q^.Left, q^.Top, stEffect);                  SetStrings(q^.Left, q^.Top, stEffect);
226                  for k := 1 to 100 do                  for k := 1 to 100 do
227                  begin                  begin
# Line 225  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 248  procedure TStoneGrid.Clear; Line 267  procedure TStoneGrid.Clear;
267  var  var
268    i, j: integer;    i, j: integer;
269  begin  begin
270      for i := 0 to FList.Count - 1 do
271        Dispose(FList[i]);
272      FList.Clear;
273    for i := 0 to Count - 1 do    for i := 0 to Count - 1 do
274      for j := 0 to Count - 1 do      for j := 0 to Count - 1 do
275        Strings[i, j] := stNone;        Strings[i, j] := stNone;
# Line 263  end; Line 285  end;
285  constructor TStoneGrid.Create;  constructor TStoneGrid.Create;
286  begin  begin
287    inherited;    inherited;
288    List := TList.Create;    FList := TList.Create;
289  end;  end;
290    
291  destructor TStoneGrid.Destroy;  destructor TStoneGrid.Destroy;
292  var  var
293    i: integer;    i: integer;
294  begin  begin
295    for i := 0 to List.Count - 1 do    for i := 0 to FList.Count - 1 do
296      Dispose(List[i]);      Dispose(FList[i]);
297    List.Free;    FList.Free;
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 295  var Line 331  var
331    p: ^TEffectData;    p: ^TEffectData;
332    i: integer;    i: integer;
333  begin  begin
334    if List.Count = 0 then    if FList.Count = 0 then
335      result := false      result := false
336    else    else
337    begin    begin
338      for i := 0 to List.Count - 1 do      for i := 0 to FList.Count - 1 do
339      begin      begin
340        p := List.List[i];        p := FList[i];
341        if p^.X < FIndex_X - 1 then        if p^.X < FIndex_X - 1 then
342          p^.X := p^.X + 1          p^.X := p^.X + 1
343        else if p^.Y < FIndex_Y - 1 then        else if p^.Y < FIndex_Y - 1 then
# Line 311  begin Line 347  begin
347        end        end
348        else        else
349        begin        begin
350          SetStrings(p^.Left, p^.Top, p^.Stone);          SetStrings(p^.Left, p^.Top, FEffectStone);
351          Dispose(p);          Dispose(p);
352          List[i] := nil;          FList[i] := nil;
353        end;        end;
354      end;      end;
355      for i := List.Count - 1 downto 0 do      for i := FList.Count - 1 downto 0 do
356        if List[i] = nil then        if FList[i] = nil then
357          List.Delete(i);          FList.Delete(i);
358      if List.Count = 0 then      if FList.Count = 0 then
359      begin      begin
       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;
370  end;  end;
371    
372  function TStoneGrid.NextStone(Stone: TStoneType): TPoint;  function TStoneGrid.NextStone(Stone: TStoneType; var Pos: TPoint): Boolean;
373  var  var
374    i, j, m, n: integer;    i, j, m, n: integer;
375  begin  begin
376    n := -1;    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 = -1) or ((m > -1) and (n > m)) then  
381        begin        begin
382          n := m;          if result = false then
383          result := Point(i, j);            result:=true;
384            n:=m;
385            Pos := Point(i, j);
386        end;        end;
     end;  
   if n = -1 then  
     result := Point(-1, -1);  
387  end;  end;
388    
389  procedure TStoneGrid.Paint(Canvas: TCanvas);  procedure TStoneGrid.Paint(Canvas: TCanvas);
# Line 359  begin Line 396  begin
396    m := Form1.Image3.Bitmap.Width;    m := Form1.Image3.Bitmap.Width;
397    n := Form1.Image3.Bitmap.Height;    n := Form1.Image3.Bitmap.Height;
398    k := Form1.Size;    k := Form1.Size;
399    for i := 0 to List.Count - 1 do    for i := 0 to FList.Count - 1 do
400    begin    begin
401      p := List[i];      p := FList[i];
402      if p^.Stone = stBlack then      if FEffectStone = stBlack then
403        s := Form1.Image1.Bitmap        s := Form1.Image1.Bitmap
404      else      else
405        s := Form1.Image2.Bitmap;        s := Form1.Image2.Bitmap;
# Line 374  end; Line 411  end;
411    
412  procedure TStoneGrid.Pause;  procedure TStoneGrid.Pause;
413  begin  begin
   FBool := false;  
414    FActive := false;    FActive := false;
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  end;  end;
423    
424    procedure TStoneGrid.SetActive(const Value: Boolean);
425    begin
426      if (FGameOver = false) or (Value = false) then
427        FActive := Value;
428    end;
429    
430  procedure TStoneGrid.SetStrings(X, Y: integer; const Value: TStoneType);  procedure TStoneGrid.SetStrings(X, Y: integer; const Value: TStoneType);
431  begin  begin
432    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 398  begin Line 441  begin
441      FTurnNumber := 0      FTurnNumber := 0
442    else    else
443      FTurnNumber := Value;      FTurnNumber := Value;
   FActive := false;  
444    FStrings := FBuffer[FTurnNumber];    FStrings := FBuffer[FTurnNumber];
445  end;  end;
446    
# Line 406  procedure TStoneGrid.Start; Line 448  procedure TStoneGrid.Start;
448  begin  begin
449    Clear;    Clear;
450    FActive := true;    FActive := true;
451      FGameOver := false;
452  end;  end;
453    
454  { TForm1 }  { TForm1 }
# Line 430  var Line 473  var
473    function Execute: Boolean;    function Execute: Boolean;
474    var    var
475      i, j: integer;      i, j: integer;
     m: integer;  
     n: integer;  
476    begin    begin
477      for i := 0 to Count - 1 do      for i := 0 to Count - 1 do
478        for j := 0 to Count - 1 do        for j := 0 to Count - 1 do
# Line 450  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 468  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 482  procedure TForm1.CompStone; Line 523  procedure TForm1.CompStone;
523  var  var
524    s: TPoint;    s: TPoint;
525  begin  begin
526    s := StoneGrid.NextStone(Index.Stone);    StoneGrid.Active := false;
527    StoneGrid.CanSetStone(Index.Stone, s.X, s.Y, true, true);    if StoneGrid.NextStone(Index.Stone, s) = true then
528    PaintBox1.Repaint;    begin
529    ChangePlayer;      StoneGrid.CanSetStone(Index.Stone, s.X, s.Y, true, true);
530        PaintBox1.Repaint;
531      end
532      else
533        ChangePlayer;
534  end;  end;
535    
536  procedure TForm1.GameStart;  procedure TForm1.GameStart;
537  begin  begin
538      Index := Player1;
539    StoneGrid.Start;    StoneGrid.Start;
540    PaintBox1.Repaint;    PaintBox1.Repaint;
   Index := Player1;  
541    Caption := '黒から始めます';    Caption := '黒から始めます';
   Timer1.Enabled := true;  
542  end;  end;
543    
544  procedure TForm1.MenuItem10Click(Sender: TObject);  procedure TForm1.MenuItem10Click(Sender: TObject);
# Line 524  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 586  end; Line 634  end;
634    
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 625  end; Line 675  end;
675    
676  procedure TForm1.Timer2Timer(Sender: TObject);  procedure TForm1.Timer2Timer(Sender: TObject);
677  begin  begin
678    if StoneGrid.ListExecute = true then    if (StoneGrid.Active = false) and (StoneGrid.ListExecute = true) then
679      PaintBox1.Repaint;      PaintBox1.Repaint;
680  end;  end;
681    
# Line 640  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.20  
changed lines
  Added in v.31

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