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 30 by yamat0jp, Sun Aug 16 04:15:52 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;
# Line 54  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 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    
134  { TStoneGrid }  { TStoneGrid }
135    
136    function TStoneGrid.AddScore(X, Y: integer; const NG: array of TPoint): integer;
137    var
138      s: TPoint;
139    begin
140      result := 0;
141      for s in NG do
142        if (X = s.X) and (Y = s.Y) then
143        begin
144          result := 10;
145          break;
146        end;
147    end;
148    
149  function TStoneGrid.CalScore(Stone: TStoneType; X, Y: integer): integer;  function TStoneGrid.CalScore(Stone: TStoneType; X, Y: integer): integer;
150  var  var
151    i, j: integer;    i, j: integer;
152  begin  begin
153    if CanSetStone(Stone, X, Y, true) = true then    if CanSetStone(Stone, X, Y, true) = true then
154    begin    begin
155        result := 0;
156        if FTurnIndex < 50 then
157          inc(result, AddScore(X, Y, [Point(1, 0), Point(6, 0), Point(0, 1),
158            Point(1, 1), Point(6, 1), Point(7, 1), Point(0, 6), Point(1, 6),
159            Point(6, 6), Point(7, 6), Point(1, 7), Point(6, 7)]));
160      case Stone of      case Stone of
161      stBlack:        stBlack:
162        Stone := stWhite;          Stone := stWhite;
163      stWhite:        stWhite:
164        Stone := stBlack;          Stone := stBlack;
     stEffect:  
       Stone := FEffectStone;  
165      end;      end;
     result := 0;  
166      for i := 0 to Count - 1 do      for i := 0 to Count - 1 do
167        for j := 0 to Count - 1 do        for j := 0 to Count - 1 do
168          if CanSetStone(Stone, i, j, false) = true then          if CanSetStone(Stone, i, j, false) = true then
169            begin
170            inc(result);            inc(result);
171      FStrings := FBuffer[FTurnIndex];            if FTurnIndex < 50 then
172                inc(result, AddScore(i, j, [Point(0, 0), Point(7, 0), Point(0, 7),
173                  Point(7, 7)]));
174            end;
175    end    end
176    else    else
   begin  
     FStrings := FBuffer[FTurnIndex];  
177      result := -1;      result := -1;
178    end;    FStrings := FBuffer[FTurnIndex];
179  end;  end;
180    
181  function TStoneGrid.CanSetStone(Stone: TStoneType; X, Y: integer;  function TStoneGrid.CanSetStone(Stone: TStoneType; X, Y: integer;
# Line 175  var Line 196  var
196      begin      begin
197        s := GetStrings(X + m * i, Y + n * i);        s := GetStrings(X + m * i, Y + n * i);
198        if s = stEffect then        if s = stEffect then
199          s:=FEffectStone;          s := FEffectStone;
200        if (s = stNone) or (s = stError) then        if (s = stNone) or (s = stError) then
201          break          break
202        else if s = Stone then        else if s = Stone then
# Line 191  var Line 212  var
212                Form1.PaintBox1.Repaint;                Form1.PaintBox1.Repaint;
213                if Visible = true then                if Visible = true then
214                begin                begin
215                  FEffectStone:=Stone;                  FEffectStone := Stone;
216                  New(q);                  New(q);
217                  q^.Left := X + m * j;                  q^.Left := X + m * j;
218                  q^.Top := Y + n * j;                  q^.Top := Y + n * j;
                 q^.Stone := Stone;  
219                  q^.X := 0;                  q^.X := 0;
220                  q^.Y := 0;                  q^.Y := 0;
221                  List.Add(q);                  FList.Add(q);
222                  SetStrings(q^.Left, q^.Top, stEffect);                  SetStrings(q^.Left, q^.Top, stEffect);
223                  for k := 1 to 100 do                  for k := 1 to 100 do
224                  begin                  begin
# Line 226  var Line 246  var
246    
247  begin  begin
248    result := false;    result := false;
   if Visible = true then  
   begin  
     FBool := FActive;  
     FActive := false;  
   end;  
249    p := true;    p := true;
250    if GetStrings(X, Y) = stNone then    if GetStrings(X, Y) = stNone then
251    begin    begin
# Line 249  procedure TStoneGrid.Clear; Line 264  procedure TStoneGrid.Clear;
264  var  var
265    i, j: integer;    i, j: integer;
266  begin  begin
267      for i := 0 to FList.Count - 1 do
268        Dispose(FList[i]);
269      FList.Clear;
270    for i := 0 to Count - 1 do    for i := 0 to Count - 1 do
271      for j := 0 to Count - 1 do      for j := 0 to Count - 1 do
272        Strings[i, j] := stNone;        Strings[i, j] := stNone;
# Line 264  end; Line 282  end;
282  constructor TStoneGrid.Create;  constructor TStoneGrid.Create;
283  begin  begin
284    inherited;    inherited;
285    List := TList.Create;    FList := TList.Create;
286  end;  end;
287    
288  destructor TStoneGrid.Destroy;  destructor TStoneGrid.Destroy;
289  var  var
290    i: integer;    i: integer;
291  begin  begin
292    for i := 0 to List.Count - 1 do    for i := 0 to FList.Count - 1 do
293      Dispose(List[i]);      Dispose(FList[i]);
294    List.Free;    FList.Free;
295    inherited;    inherited;
296  end;  end;
297    
298    procedure TStoneGrid.GameOver;
299    begin
300      FGameOver := true;
301      FActive := false;
302    end;
303    
304    function TStoneGrid.GetActive: Boolean;
305    begin
306      if (FActive = true) and (FList.Count = 0) then
307        result := true
308      else
309        result := false;
310    end;
311    
312  function TStoneGrid.GetStrings(X, Y: integer): TStoneType;  function TStoneGrid.GetStrings(X, Y: integer): TStoneType;
313  begin  begin
314    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 328  var
328    p: ^TEffectData;    p: ^TEffectData;
329    i: integer;    i: integer;
330  begin  begin
331    if List.Count = 0 then    if FList.Count = 0 then
332      result := false      result := false
333    else    else
334    begin    begin
335      for i := 0 to List.Count - 1 do      for i := 0 to FList.Count - 1 do
336      begin      begin
337        p := List.List[i];        p := FList[i];
338        if p^.X < FIndex_X - 1 then        if p^.X < FIndex_X - 1 then
339          p^.X := p^.X + 1          p^.X := p^.X + 1
340        else if p^.Y < FIndex_Y - 1 then        else if p^.Y < FIndex_Y - 1 then
# Line 312  begin Line 344  begin
344        end        end
345        else        else
346        begin        begin
347          SetStrings(p^.Left, p^.Top, p^.Stone);          SetStrings(p^.Left, p^.Top, FEffectStone);
348          Dispose(p);          Dispose(p);
349          List[i] := nil;          FList[i] := nil;
350        end;        end;
351      end;      end;
352      for i := List.Count - 1 downto 0 do      for i := FList.Count - 1 downto 0 do
353        if List[i] = nil then        if FList[i] = nil then
354          List.Delete(i);          FList.Delete(i);
355      if List.Count = 0 then      if FList.Count = 0 then
356      begin      begin
       if FTerminated = true then  
         FActive:=false  
       else  
         FActive := FBool;  
357        inc(FTurnIndex);        inc(FTurnIndex);
358        inc(FTurnNumber);        inc(FTurnNumber);
359        FBuffer[FTurnIndex] := FStrings;        FBuffer[FTurnIndex] := FStrings;
360          if FGameOver = false then
361          begin
362            Form1.ChangePlayer;
363            FActive:=true;
364          end;
365      end;      end;
366      result := true;      result := true;
367    end;    end;
368  end;  end;
369    
370  function TStoneGrid.NextStone(Stone: TStoneType): TPoint;  function TStoneGrid.NextStone(Stone: TStoneType; var Pos: TPoint): Boolean;
371  var  var
372    i, j, m, n: integer;    i, j, m, n: integer;
373  begin  begin
# Line 343  begin Line 376  begin
376      for j := 0 to Count - 1 do      for j := 0 to Count - 1 do
377      begin      begin
378        m := CalScore(Stone, i, j);        m := CalScore(Stone, i, j);
379        if (n = -1) or ((m > -1) and (n > m)) then        if (n = -1) or ((0 < m) and (m < n)) then
380        begin        begin
381          n := m;          n := m;
382          result := Point(i, j);          Pos := Point(i, j);
383        end;        end;
384      end;      end;
385    if n = -1 then    result := not(n = -1);
     result := Point(-1, -1);  
386  end;  end;
387    
388  procedure TStoneGrid.Paint(Canvas: TCanvas);  procedure TStoneGrid.Paint(Canvas: TCanvas);
# Line 363  begin Line 395  begin
395    m := Form1.Image3.Bitmap.Width;    m := Form1.Image3.Bitmap.Width;
396    n := Form1.Image3.Bitmap.Height;    n := Form1.Image3.Bitmap.Height;
397    k := Form1.Size;    k := Form1.Size;
398    for i := 0 to List.Count - 1 do    for i := 0 to FList.Count - 1 do
399    begin    begin
400      p := List[i];      p := FList[i];
401      if p^.Stone = stBlack then      if FEffectStone = stBlack then
402        s := Form1.Image1.Bitmap        s := Form1.Image1.Bitmap
403      else      else
404        s := Form1.Image2.Bitmap;        s := Form1.Image2.Bitmap;
# Line 378  end; Line 410  end;
410    
411  procedure TStoneGrid.Pause;  procedure TStoneGrid.Pause;
412  begin  begin
413    FTerminated:=true;    FActive := false;
414  end;  end;
415    
416  procedure TStoneGrid.Restart;  procedure TStoneGrid.Restart;
417  begin  begin
418    if FTerminated = true then    FActive:=true;
419    begin    FGameOver := false;
420      FActive := true;    FTurnIndex := FTurnNumber;
421      FTurnIndex := FTurnNumber;  end;
422      FTerminated:=false;  
423    end;  procedure TStoneGrid.SetActive(const Value: Boolean);
424    begin
425      if (FGameOver = false)or(Value = false) then
426        FActive := Value;
427  end;  end;
428    
429  procedure TStoneGrid.SetStrings(X, Y: integer; const Value: TStoneType);  procedure TStoneGrid.SetStrings(X, Y: integer; const Value: TStoneType);
# Line 411  end; Line 446  end;
446  procedure TStoneGrid.Start;  procedure TStoneGrid.Start;
447  begin  begin
448    Clear;    Clear;
   FTerminated:=false;  
449    FActive := true;    FActive := true;
450      FGameOver := false;
451  end;  end;
452    
453  { TForm1 }  { TForm1 }
# Line 437  var Line 472  var
472    function Execute: Boolean;    function Execute: Boolean;
473    var    var
474      i, j: integer;      i, j: integer;
     m: integer;  
     n: integer;  
475    begin    begin
476      for i := 0 to Count - 1 do      for i := 0 to Count - 1 do
477        for j := 0 to Count - 1 do        for j := 0 to Count - 1 do
# Line 457  begin Line 490  begin
490      Main;      Main;
491      if Execute = false then      if Execute = false then
492      begin      begin
       StoneGrid.Pause;  
493        m := 0;        m := 0;
494        n := 0;        n := 0;
495        for i := 0 to Count - 1 do        for i := 0 to Count - 1 do
# Line 475  begin Line 507  begin
507          s := 'Player2 Win:' + #13#10          s := 'Player2 Win:' + #13#10
508        else        else
509          s := 'Draw:' + #13#10;          s := 'Draw:' + #13#10;
510          StoneGrid.GameOver;
511        Showmessage(s + '(Player1) ' + IntToStr(m) + #13#10 + '(Player2) ' +        Showmessage(s + '(Player1) ' + IntToStr(m) + #13#10 + '(Player2) ' +
512          IntToStr(n));          IntToStr(n));
513      end      end
# Line 489  procedure TForm1.CompStone; Line 522  procedure TForm1.CompStone;
522  var  var
523    s: TPoint;    s: TPoint;
524  begin  begin
525    s := StoneGrid.NextStone(Index.Stone);    StoneGrid.Active := false;
526      StoneGrid.NextStone(Index.Stone, s);
527    StoneGrid.CanSetStone(Index.Stone, s.X, s.Y, true, true);    StoneGrid.CanSetStone(Index.Stone, s.X, s.Y, true, true);
528    PaintBox1.Repaint;    PaintBox1.Repaint;
   ChangePlayer;  
529  end;  end;
530    
531  procedure TForm1.GameStart;  procedure TForm1.GameStart;
# Line 530  end; Line 563  end;
563    
564  procedure TForm1.MenuItem2Click(Sender: TObject);  procedure TForm1.MenuItem2Click(Sender: TObject);
565  begin  begin
566      Timer1.Enabled := false;
567      Timer2.Enabled := false;
568    GameStart;    GameStart;
569      Timer1.Enabled := true;
570      Timer2.Enabled := true;
571  end;  end;
572    
573  procedure TForm1.MenuItem4Click(Sender: TObject);  procedure TForm1.MenuItem4Click(Sender: TObject);
# Line 592  end; Line 629  end;
629    
630  procedure TForm1.FormCreate(Sender: TObject);  procedure TForm1.FormCreate(Sender: TObject);
631  begin  begin
632      ClientWidth:=20*Count;
633      ClientHeight:=20*Count;
634    StoneGrid := TStoneGrid.Create;    StoneGrid := TStoneGrid.Create;
635    StoneGrid.ImageCount(Form1.Image1.Bitmap.Width div Form1.Image3.Bitmap.Width,    StoneGrid.ImageCount(Form1.Image1.Bitmap.Width div Form1.Image3.Bitmap.Width,
636      Form1.Image1.Bitmap.Height div Form1.Image3.Bitmap.Height);      Form1.Image1.Bitmap.Height div Form1.Image3.Bitmap.Height);
# Line 631  end; Line 670  end;
670    
671  procedure TForm1.Timer2Timer(Sender: TObject);  procedure TForm1.Timer2Timer(Sender: TObject);
672  begin  begin
673    if (StoneGrid.Active = false)and(StoneGrid.ListExecute = true) then    if (StoneGrid.Active = false) and (StoneGrid.ListExecute = true) then
674      PaintBox1.Repaint;      PaintBox1.Repaint;
675  end;  end;
676    
# Line 646  begin Line 685  begin
685    if Index.Auto = false then    if Index.Auto = false then
686    begin    begin
687      MenuItem10Click(Sender);      MenuItem10Click(Sender);
688        StoneGrid.Active := false;
689      if StoneGrid.CanSetStone(Index.Stone, Floor(Point.X / Size),      if StoneGrid.CanSetStone(Index.Stone, Floor(Point.X / Size),
690        Floor(Point.Y / Size), true, true) = true then        Floor(Point.Y / Size), true, true) = true then
     begin  
691        PaintBox1.Repaint;        PaintBox1.Repaint;
692        ChangePlayer;      StoneGrid.Active := true;
     end;  
693    end;    end;
694  end;  end;
695    

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

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