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 16 by yamat0jp, Sat Jul 18 05:48:27 2015 UTC revision 29 by yamat0jp, Sat Aug 15 12:32:41 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;
# Line 38  type Line 37  type
37      FTurnNumber: integer;      FTurnNumber: integer;
38      FTurnIndex: integer;      FTurnIndex: integer;
39      FActive: Boolean;      FActive: Boolean;
40      List: TList;      FList: TList;
41      FBool: Boolean;      FEffectStone: TStoneType;
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 52  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 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
151    if CanSetStone(Stone, X, Y, true) = true then    if CanSetStone(Stone, X, Y, true) = true then
152    begin    begin
     if Stone = stBlack then  
       Stone := stWhite  
     else  
       Stone := stBlack;  
153      result := 0;      result := 0;
154        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
158          stBlack:
159            Stone := stWhite;
160          stWhite:
161            Stone := stBlack;
162        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      FStrings := FBuffer[FTurnIndex];            inc(result, AddScore(i, j, [Point(0, 0), Point(7, 0), Point(0, 7),
169                Point(7, 7)]));
170            end;
171    end    end
172    else    else
   begin  
     FStrings := FBuffer[FTurnIndex];  
173      result := -1;      result := -1;
174    end;    FStrings := FBuffer[FTurnIndex];
175  end;  end;
176    
177  function TStoneGrid.CanSetStone(Stone: TStoneType; X, Y: integer;  function TStoneGrid.CanSetStone(Stone: TStoneType; X, Y: integer;
# Line 168  var Line 191  var
191      while true do      while true do
192      begin      begin
193        s := GetStrings(X + m * i, Y + n * i);        s := GetStrings(X + m * i, Y + n * i);
194          if s = stEffect then
195            s := FEffectStone;
196        if (s = stNone) or (s = stError) then        if (s = stNone) or (s = stError) then
197          break          break
198        else if s = Stone then        else if s = Stone then
# Line 183  var Line 208  var
208                Form1.PaintBox1.Repaint;                Form1.PaintBox1.Repaint;
209                if Visible = true then                if Visible = true then
210                begin                begin
211                    FEffectStone := Stone;
212                  New(q);                  New(q);
213                  q^.Left := X + m * j;                  q^.Left := X + m * j;
214                  q^.Top := Y + n * j;                  q^.Top := Y + n * j;
                 q^.Stone := Stone;  
215                  q^.X := 0;                  q^.X := 0;
216                  q^.Y := 0;                  q^.Y := 0;
217                  List.Add(q);                  FList.Add(q);
218                  SetStrings(q^.Left, q^.Top, stEffect);                  SetStrings(q^.Left, q^.Top, stEffect);
219                  for k := 1 to 10 do                  for k := 1 to 100 do
220                  begin                  begin
221                    Sleep(10);                    Sleep(1);
222                    Application.ProcessMessages;                    Application.ProcessMessages;
223                  end;                  end;
224                end                end
# Line 216  var Line 241  var
241    end;    end;
242    
243  begin  begin
   if Visible = true then  
   begin  
     FBool := FActive;  
     FActive := false;  
   end;  
244    result := false;    result := false;
245    p := true;    p := true;
246    if GetStrings(X, Y) = stNone then    if GetStrings(X, Y) = stNone then
# Line 240  procedure TStoneGrid.Clear; Line 260  procedure TStoneGrid.Clear;
260  var  var
261    i, j: integer;    i, j: integer;
262  begin  begin
263      for i := 0 to FList.Count - 1 do
264        Dispose(FList[i]);
265      FList.Clear;
266    for i := 0 to Count - 1 do    for i := 0 to Count - 1 do
267      for j := 0 to Count - 1 do      for j := 0 to Count - 1 do
268        Strings[i, j] := stNone;        Strings[i, j] := stNone;
# Line 255  end; Line 278  end;
278  constructor TStoneGrid.Create;  constructor TStoneGrid.Create;
279  begin  begin
280    inherited;    inherited;
281    List := TList.Create;    FList := TList.Create;
282  end;  end;
283    
284  destructor TStoneGrid.Destroy;  destructor TStoneGrid.Destroy;
285  var  var
286    i: integer;    i: integer;
287  begin  begin
288    for i := 0 to List.Count - 1 do    for i := 0 to FList.Count - 1 do
289      Dispose(List[i]);      Dispose(FList[i]);
290    List.Free;    FList.Free;
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 287  var Line 324  var
324    p: ^TEffectData;    p: ^TEffectData;
325    i: integer;    i: integer;
326  begin  begin
327    if List.Count = 0 then    if FList.Count = 0 then
328      result := false      result := false
329    else    else
330    begin    begin
331      for i := 0 to List.Count - 1 do      for i := 0 to FList.Count - 1 do
332      begin      begin
333        p := List.List[i];        p := FList[i];
334        if p^.X < FIndex_X - 1 then        if p^.X < FIndex_X - 1 then
335          p^.X := p^.X + 1          p^.X := p^.X + 1
336        else if p^.Y < FIndex_Y - 1 then        else if p^.Y < FIndex_Y - 1 then
# Line 303  begin Line 340  begin
340        end        end
341        else        else
342        begin        begin
343          SetStrings(p^.Left, p^.Top, p^.Stone);          SetStrings(p^.Left, p^.Top, FEffectStone);
344          Dispose(p);          Dispose(p);
345          List[i] := nil;          FList[i] := nil;
346        end;        end;
347      end;      end;
348      for i := List.Count - 1 downto 0 do      for i := FList.Count - 1 downto 0 do
349        if List[i] = nil then        if FList[i] = nil then
350          List.Delete(i);          FList.Delete(i);
351      if List.Count = 0 then      if FList.Count = 0 then
352      begin      begin
       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
# Line 331  begin Line 372  begin
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 = -1) or ((m > -1) and (n > m)) 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 = -1 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 351  begin Line 391  begin
391    m := Form1.Image3.Bitmap.Width;    m := Form1.Image3.Bitmap.Width;
392    n := Form1.Image3.Bitmap.Height;    n := Form1.Image3.Bitmap.Height;
393    k := Form1.Size;    k := Form1.Size;
394    for i := 0 to List.Count - 1 do    for i := 0 to FList.Count - 1 do
395    begin    begin
396      p := List[i];      p := FList[i];
397      if p^.Stone = stBlack then      if FEffectStone = stBlack then
398        s := Form1.Image1.Bitmap        s := Form1.Image1.Bitmap
399      else      else
400        s := Form1.Image2.Bitmap;        s := Form1.Image2.Bitmap;
# Line 366  end; Line 406  end;
406    
407  procedure TStoneGrid.Pause;  procedure TStoneGrid.Pause;
408  begin  begin
   FBool := false;  
409    FActive := false;    FActive := false;
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  end;  end;
418    
419    procedure TStoneGrid.SetActive(const Value: Boolean);
420    begin
421      if (FGameOver = false)or(Value = false) then
422        FActive := Value;
423    end;
424    
425  procedure TStoneGrid.SetStrings(X, Y: integer; const Value: TStoneType);  procedure TStoneGrid.SetStrings(X, Y: integer; const Value: TStoneType);
426  begin  begin
427    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 390  begin Line 436  begin
436      FTurnNumber := 0      FTurnNumber := 0
437    else    else
438      FTurnNumber := Value;      FTurnNumber := Value;
   FActive := false;  
439    FStrings := FBuffer[FTurnNumber];    FStrings := FBuffer[FTurnNumber];
440  end;  end;
441    
# Line 398  procedure TStoneGrid.Start; Line 443  procedure TStoneGrid.Start;
443  begin  begin
444    Clear;    Clear;
445    FActive := true;    FActive := true;
446      FGameOver := false;
447  end;  end;
448    
449  { TForm1 }  { TForm1 }
# Line 409  var Line 455  var
455    procedure Main;    procedure Main;
456    begin    begin
457      if Index = Player1 then      if Index = Player1 then
458        Index := Player2      begin
459          Index := Player2;
460          s := '白の手番です';
461        end
462      else      else
463        begin
464        Index := Player1;        Index := Player1;
465          s := '黒の手番です';
466        end;
467    end;    end;
468    function Execute: Boolean;    function Execute: Boolean;
469    var    var
470      i, j: integer;      i, j: integer;
471    begin    begin
     result := false;  
472      for i := 0 to Count - 1 do      for i := 0 to Count - 1 do
473        for j := 0 to Count - 1 do        for j := 0 to Count - 1 do
474          if StoneGrid.CanSetStone(Index.Stone, i, j, false) = true then          if StoneGrid.CanSetStone(Index.Stone, i, j, false) = true then
# Line 425  var Line 476  var
476            result := true;            result := true;
477            Exit;            Exit;
478          end;          end;
479        result := false;
480    end;    end;
481    
482  begin  begin
# Line 434  begin Line 486  begin
486      Main;      Main;
487      if Execute = false then      if Execute = false then
488      begin      begin
       StoneGrid.Pause;  
       Timer1.Enabled := false;  
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 446  begin Line 496  begin
496              stWhite:              stWhite:
497                inc(n);                inc(n);
498            end;            end;
499          Caption := s;
500        if m > n then        if m > n then
501          s := 'Player1 Win:' + #13#10          s := 'Player1 Win:' + #13#10
502        else if m < n then        else if m < n then
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
510    end;      else
511          Caption := s;
512      end
513      else
514        Caption := s;
515  end;  end;
516    
517  procedure TForm1.CompStone;  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
529      Index := Player1;
530    StoneGrid.Start;    StoneGrid.Start;
531    PaintBox1.Repaint;    PaintBox1.Repaint;
532    Index := Player1;    Caption := '黒から始めます';
   Timer1.Enabled := true;  
533  end;  end;
534    
535  procedure TForm1.MenuItem10Click(Sender: TObject);  procedure TForm1.MenuItem10Click(Sender: TObject);
536  begin  begin
537    StoneGrid.Restart;    StoneGrid.Restart;
   Timer1.Enabled := true;  
538  end;  end;
539    
540  procedure TForm1.MenuItem11Click(Sender: TObject);  procedure TForm1.MenuItem11Click(Sender: TObject);
541    var
542      i: integer;
543  begin  begin
   Timer1.Enabled := false;  
544    with StoneGrid do    with StoneGrid do
545      begin
546        i := TurnNumber;
547      if Sender = MenuItem11 then      if Sender = MenuItem11 then
548        TurnNumber := TurnNumber + 1        TurnNumber := TurnNumber + 1
549      else      else
550        TurnNumber := TurnNumber - 1;        TurnNumber := TurnNumber - 1;
551        if (i = TurnNumber) then
552          Exit
553        else
554          Pause;
555      end;
556    PaintBox1.Repaint;    PaintBox1.Repaint;
557    ChangePlayer;    ChangePlayer;
558  end;  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 508  procedure TForm1.MenuItem6Click(Sender: Line 575  procedure TForm1.MenuItem6Click(Sender:
575  begin  begin
576    Player1.Auto := MenuItem6.IsChecked;    Player1.Auto := MenuItem6.IsChecked;
577    Player2.Auto := MenuItem7.IsChecked;    Player2.Auto := MenuItem7.IsChecked;
   MenuItem10Click(Sender);  
578  end;  end;
579    
580  procedure TForm1.MenuItem8Click(Sender: TObject);  procedure TForm1.MenuItem8Click(Sender: TObject);
581  begin  begin
582    StoneGrid.Pause;    StoneGrid.Pause;
   Timer1.Enabled := false;  
583  end;  end;
584    
585  procedure TForm1.PaintBox1Paint(Sender: TObject; Canvas: TCanvas);  procedure TForm1.PaintBox1Paint(Sender: TObject; Canvas: TCanvas);
586  var  var
587    i, j: integer;    i, j: integer;
588  begin  begin
589    for i := 0 to Count-1 do    if StoneGrid.Active = false then
590        StoneGrid.Paint(Canvas);
591      for i := 0 to Count - 1 do
592    begin    begin
593      for j := 0 to Count-1 do      for j := 0 to Count - 1 do
594      begin      begin
595        case StoneGrid.Strings[i, j] of        case StoneGrid.Strings[i, j] of
596          stWhite:          stWhite:
# Line 545  begin Line 612  begin
612      end;      end;
613      Canvas.DrawLine(PointF(i * Size, 0), PointF(i * Size, Size * Count), 1);      Canvas.DrawLine(PointF(i * Size, 0), PointF(i * Size, Size * Count), 1);
614    end;    end;
615    Canvas.DrawLine(PointF(Count*Size,0),PointF(Count*Size,Count*Size),1);    Canvas.DrawLine(PointF(Count * Size, 0),
616    Canvas.DrawLine(PointF(0,Count*Size),PointF(Count*Size,Count*Size),1);      PointF(Count * Size, Count * Size), 1);
617    if StoneGrid.Active = false then    Canvas.DrawLine(PointF(0, Count * Size),
618      StoneGrid.Paint(Canvas);      PointF(Count * Size, Count * Size), 1);
619  end;  end;
620    
621  procedure TForm1.PaintBox1Resize(Sender: TObject);  procedure TForm1.PaintBox1Resize(Sender: TObject);
# Line 592  end; Line 659  end;
659  procedure TForm1.Timer1Timer(Sender: TObject);  procedure TForm1.Timer1Timer(Sender: TObject);
660  begin  begin
661    if (StoneGrid.Active = true) and (Index.Auto = true) then    if (StoneGrid.Active = true) and (Index.Auto = true) then
   begin  
     Timer1.Enabled := false;  
662      CompStone;      CompStone;
     Timer1.Enabled := true;  
   end;  
663  end;  end;
664    
665  procedure TForm1.Timer2Timer(Sender: TObject);  procedure TForm1.Timer2Timer(Sender: TObject);
666  begin  begin
667    if StoneGrid.ListExecute = true then    if (StoneGrid.Active = false) and (StoneGrid.ListExecute = true) then
668      PaintBox1.Repaint;      PaintBox1.Repaint;
669  end;  end;
670    
# Line 616  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.16  
changed lines
  Added in v.29

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