?? chessboard.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 + -