⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.~pas

📁 用c#写的五子棋游戏
💻 ~PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, ExtCtrls, Menus, ComCtrls, ToolWin, Buttons, StdCtrls;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    DrawGrid1: TDrawGrid;
    ToolBar1: TToolBar;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton4: TSpeedButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure DrawGrid1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Draw(IsBlack: Boolean; Row,Col: Integer);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
  private
    { Private declarations }
  public
    IsBlack : Boolean;
    { Public declarations }
  end;



Type
    TFive = class(TObject)                    //此类管理与数组有关的操作
  private
    Grid : array[1..15,1..15] of Integer;     // 0代表没有子.  1.black
    { Private declarations }                  // 2.white
  public
    function IsWin(IsBlack : Boolean):Integer;    //RETURN 0=没有赢 1=blackWin 2=whiteWin
    procedure GridInit();
    function IsBlank(a,b : Integer):Boolean;      //判断一个格子里面是否有子
    procedure GridChange(Row,Col,Color: Integer);
    { Public declarations }
  end;                                             






var
  Form1: TForm1;
  Five1 : TFive;

implementation

uses Unit2;

{$R *.dfm}

procedure TFive.GridChange(Row,Col,Color: Integer);
begin
  Self.Grid[Row,Col] := Color;
end;

procedure TFive.GridInit();
var
  i,j : Integer;
begin
  Form1.DrawGrid1.Canvas.Pen.Color := clWhite;
  Form1.DrawGrid1.Canvas.brush.Color := clWhite;

  for i := 1 to 15 do
    for j := 1 to 15 do
    begin
      Grid[i,j] := 0;
      Form1.DrawGrid1.Canvas.Ellipse((j-1)*24,(i-1)*24,j*24,i*24);
    end;
  Form1.DrawGrid1.Invalidate;
end;

function TFive.IsBlank(a, b: Integer): Boolean;
begin
   if Grid[a,b] = 0 then
     Result := True
   else
     Result := False;
end;

function TFive.IsWin(IsBlack: Boolean): Integer;
label Exit;
var
   Color : Integer;
   m,n : Integer;
begin
   if IsBlack then
     Color := 1          //IsBlack=ture  证明黑棋刚刚走过.只用找黑棋是否连成五子
   else
     Color := 2;         //IsBlack=false 证明白棋刚刚走过.............

for m := 1 to 15 do             //m为行
  for n := 1 to 15 do           //n为列..
  begin                         //此循环对整个数组进行遍历

   {是否有行连成}
    if (n<12)
    and(Grid[m,n]=Color)
    and(Grid[m,n+1]=Color)
    and(Grid[m,n+2]=Color)
    and(Grid[m,n+3]=Color)
    and(Grid[m,n+4]=Color)
    then
    begin
      Result := Color;
      goto Exit;
    end;



    {是否有列连成}
    if (m<12)
    and(Grid[m,n]=Color)
    and(Grid[m+1,n]=Color)
    and(Grid[m+2,n]=Color)
    and(Grid[m+3,n]=Color)
    and(Grid[m+4,n]=Color)
    then begin
      Result := Color;
      goto Exit;
    end;



    {是否有主对角线连成}
    if (m<12)
    and(n<12)
    and(Grid[m,n]=Color)
    and(Grid[m+1,n+1]=Color)
    and(Grid[m+2,n+2]=Color)
    and(Grid[m+3,n+3]=Color)
    and(Grid[m+4,n+4]=Color)
    then
    begin
      Result := Color;
       goto Exit;
    end;



    {是否有副对角线连成}
    if (m>4)
    and(n<12)
    and(Grid[m,n]=Color)
    and(Grid[m-1,n+1]=Color)
    and(Grid[m-2,n+2]=Color)
    and(Grid[m-3,n+3]=Color)
    and(Grid[m-4,n+4]=Color)
    then
    begin
      Result := Color;
       goto Exit;
    end;
  end;


Result := 0;
Exit:
end;




procedure TForm1.FormCreate(Sender: TObject);
begin
  Five1 := TFive.Create;
  Five1.GridInit();
  IsBlack := true;

end;

procedure TForm1.DrawGrid1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  Col,Row:Integer;
begin
//主要的处理在此过程中
  Self.DrawGrid1.MouseToCell(x,y,Col,Row);
  Col := Col+1;
  Row := Row+1;

  if Five1.Isblank(Row,Col) then
  begin
    Form1.Draw(IsBlack,Row,Col);

    if Five1.IsWin(IsBlack) = 1 then
    begin
    Five1.GridInit();
    ShowMessage('恭喜,黑方赢了!');
    
    end;

    if Five1.IsWin(IsBlack) = 2 then
    begin
    Five1.GridInit;
    ShowMessage('恭喜,白方赢了!');
    
    end;

    IsBlack := not IsBlack;

  end;

end;

procedure TForm1.Draw(IsBlack: Boolean; Row,Col: Integer);
begin
  if IsBlack then
  begin
    Five1.GridChange(Row,Col,1);
    DrawGrid1.Canvas.Pen.Color := clBlack;
    DrawGrid1.Canvas.Brush.Color := clBlack;
    DrawGrid1.Canvas.Ellipse((Col-1)*24,(Row-1)*24,(Col)*24,(Row)*24);
  end
  else begin
    Five1.GridChange(Row,Col,2);
    DrawGrid1.Canvas.Pen.Color := clBlack;
    DrawGrid1.Canvas.Brush.Color := clwhite;
    DrawGrid1.Canvas.Ellipse((Col-1)*24,(Row-1)*24,(Col)*24,(Row)*24);
  end;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  Five1.GridInit;
end;

procedure TForm1.SpeedButton4Click(Sender: TObject);
begin
  Close;
end;

procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
   Form2.Show;
end;

end.



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -