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

📄 chessboard.pas

📁 Delphi7.0平台开发
💻 PAS
字号:
unit ChessBoard;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ImgList, ExtCtrls, jpeg;

type
  TForm_ChessBoard = class(TForm)
    Image_ChessBoard: TImage;
    ChessList: TImageList;
    procedure Image_ChessBoardMouseUp(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure PutChess(X,Y:Integer;Flag:Integer);
    function Winning(X,Y:Integer):Boolean;
  end;

var
  Form_ChessBoard: TForm_ChessBoard;
  Over:Boolean = TRUE;
  ChessData:array[1..15,1..15] of Byte;

implementation

uses Main;

{$R *.dfm}

procedure TForm_ChessBoard.Image_ChessBoardMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  i,j:Integer;
  label QuitFunc;
begin
  if ((not Over) and (CurrentFlag = LocalFlag)) then
  begin
    for i:=1 to 15 do
      for j:=1 to 15 do
      begin
        if ((abs(X-i*28)<6) and (abs(Y-j*28)<6) and (ChessData[i][j]=$FF)) then
        begin
          PutChess(i,j,LocalFlag);
          ChessData[i][j]:=LocalFlag;
          TxLen :=4;
          TxBuff[0] := $02;
          TxBuff[1] :=i;
          TxBuff[2] :=j;
          CurrentFlag := RemoteFlag;
          if Winning(i,j) then
          begin
            TxBuff[3]:=LocalFlag; //表示游戏结束
            Over:=TRUE;
            Form_Main.lblCurrent.Caption :='';
            Application.MessageBox('您赢得了这场比赛','恭喜',MB_ICONINFORMATION+MB_OK);
          end
          else
          begin
            TxBuff[3]:=$AA;   //表示游戏未结束
            Form_Main.lblCurrent.Caption :='对方';
          end;
          SendData(LocalSocket,TxBuff,TxLen);
          goto QuitFunc;
        end;
      end;
  end;

QuitFunc:

end;

procedure TForm_ChessBoard.FormCreate(Sender: TObject);
var
  i,j:Integer;
begin
  for i:=1 to 15 do
    for j:=1 to 15 do
    begin
      ChessData[i][j]:=$FF;
    end;

end;

procedure TForm_ChessBoard.PutChess(X,Y: Integer;Flag:Integer);
var
  img:TBitmap;
begin
  img := TBitmap.Create;
  ChessList.GetBitmap(Flag,img);
  with TImage.Create(Self) do
  begin
    Top := Y*28-6;
    Left := X*28-6;
    Height := 22;
    Width  := 22;
    Parent := Self;
    Picture.Assign(img);
    TransParent :=TRUE;
  end;
end;

function TForm_ChessBoard.Winning(X, Y: Integer):Boolean;   //判断是否获胜
var
  i:Integer;
  Heng,Shu,Xie:Integer;
begin
  Heng:=1;
  Shu:=1;
  Xie:=1;

  for i:=1 to 4 do
  begin
    //横向判断
    if ((X-i)>=1) then
    begin
      if ChessData[X,Y]=ChessData[X-i,Y] then Inc(Heng);
    end;
    if ((X+i)<=15) then
    begin
      if ChessData[X,Y]=ChessData[X+i,Y] then Inc(Heng);
    end;

    //纵向判断
    if ((Y-i)>=1) then
    begin
      if ChessData[X,Y]=ChessData[X,Y-i] then Inc(Shu);
    end;
    if ((Y+i)<=15) then
    begin
      if ChessData[X,Y]=ChessData[X,Y+i] then Inc(Shu);
    end;

      //斜向判断
    if (((X-i)>=1) and ((Y-i)>=1)) then
    begin
      if ChessData[X,Y]=ChessData[X-i,Y-i] then Inc(Xie);
    end;
    if (((X+i)<=15) and ((Y+i)<=15)) then
    begin
      if ChessData[X,Y]=ChessData[X+i,Y+i] then Inc(Xie);
    end;
  end;

  if ((Heng>=5) or (Shu>=5) or (Xie>=5)) then
    Result:=TRUE
  else
    Result:=FALSE;
    
end;

end.

⌨️ 快捷键说明

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