📄 unit1.pas
字号:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls, Buttons, ExtCtrls;
type
TForm1 = class(TForm)
DrawGrid1: TDrawGrid;
GroupBox1: TGroupBox;
Label1: TLabel;
Label2: TLabel;
Shape1: TShape;
Shape2: TShape;
Timer1: TTimer;
GroupBox2: TGroupBox;
GroupBox3: TGroupBox;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Label10: TLabel;
btReStart: TButton;
btClose: TButton;
procedure DrawGrid1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure btReStartClick(Sender: TObject);
procedure btCloseClick(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
IsPlayer1:boolean=true; //是否轮到Player1下棋
playing:boolean=false; //比赛是否正在进行
chess:array [0..18, 0..18]of integer; //棋盘
currentTime:TDateTime; //开始思考时间
player1Time,player2Time:double; //累积时间
implementation
{$R *.dfm}
//初始化比赛
procedure Initiate;
var
i, j:integer;
begin
//棋盘初始化
for i:=0 to 18 do
for j:=0 to 18 do
chess[i][j]:=0;
//显示信息初始化
Form1.Label7.Caption:='0:00:00';
Form1.Label8.Caption:='0:00:00';
player1Time:=0;
player2Time:=0;
currentTime:=time;
end;
//电子裁判程序
function Win(x,y:integer):boolean;
var
i,j,who,CountFive:integer;
begin
result:=false;
who:=chess[x,y]; //取得目标棋子的颜色
//横向判断
if (chess[x,y+1]=who) or (chess[x,y-1]=who) then
begin
CountFive:=1;
j:=y+1;
while (chess[x,j]=who) and (j<=18) do
begin
Inc(CountFive);
Inc(j);
end;
j:=y-1;
while (chess[x,j]=who) and (j>=0) do
begin
Inc(CountFive);
Dec(j);
end;
if CountFive=5 then
result:=true;
end;
//纵向判断
if (chess[x+1,y]=who) or (chess[x-1,y]=who) then
begin
CountFive:=1;
i:=x+1;
while (chess[i,y]=who) and (i<=18) do
begin
Inc(CountFive);
Inc(i);
end;
i:=x-1;
while (chess[i,y]=who) and (i>=0) do
begin
Inc(CountFive);
Dec(i);
end;
if CountFive=5 then
result:=true;
end;
//正45度角方向判断
if (chess[x+1,y+1]=who) or (chess[x-1,y-1]=who) then
begin
CountFive:=1;
i:=x+1;
j:=y+1;
while (chess[i,j]=who) and (i<=18) and (j<=18) do
begin
Inc(CountFive);
Inc(i);
Inc(j);
end;
i:=x-1;
j:=y-1;
while (chess[i,j]=who) and (i>=0) and (j>=0) do
begin
Inc(CountFive);
Dec(i);
Dec(j);
end;
if CountFive=5 then
result:=true;
end;
//负45度角方向判断
if (chess[x+1,y-1]=who) or (chess[x-1,y+1]=who) then
begin
CountFive:=1;
i:=x+1;
j:=y-1;
while (chess[i,j]=who) and (i<=18) and (j>=0) do
begin
Inc(CountFive);
Inc(i);
Dec(j);
end;
i:=x-1;
j:=y+1;
while (chess[i,j]=who) and (i>=0) and (j<=18) do
begin
Inc(CountFive);
Dec(i);
Inc(j);
end;
if CountFive=5 then
result:=true;
end;
end;
//鼠标键弹起的时候才落子
procedure TForm1.DrawGrid1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
ACol, ARow:integer;
begin
//取得目标栅格
DrawGrid1.MouseToCell(X,Y,ACol,ARow);
//判断是否在比赛中,如果不在比赛中不予绘制棋子
if playing then
//判断是谁在落子
if IsPlayer1 then
begin
//判断是否能够落子
if chess[ACol, ARow]=0 then
begin
//绘制黑棋
DrawGrid1.Canvas.Pen.Color:=clBlack;
DrawGrid1.Canvas.Brush.Color:=clBlack;
DrawGrid1.Canvas.Ellipse(ACol*21+1, ARow*21+1, (ACol+1)*21-1, (ARow+1)*21-1);
//记录黑棋
chess[ACol, ARow]:=1;
//切换选手
IsPlayer1:=false;
//判断胜负
if Win(ACol,ARow) then
begin
//更新和显示统计提示信息
Label9.Caption:=inttostr(strtoint(Label9.Caption)+1);
Timer1.Enabled:=false;
playing:=false;
btReStart.Enabled:=true;
showmessage('Player 1 Win!');
end;
end
else showmessage('此处不能落子!');
end
else
begin
//判断是否能够落子
if chess[ACol, ARow]=0 then
begin
//绘制白棋
DrawGrid1.Canvas.Pen.Color:=clBlack;
DrawGrid1.Canvas.Brush.Color:=clWhite;
DrawGrid1.Canvas.Ellipse(ACol*21+1, ARow*21+1, (ACol+1)*21-1, (ARow+1)*21-1);
//记录白棋
chess[ACol, ARow]:=2;
//切换选手
IsPlayer1:=true;
//判断胜负
if Win(ACol,ARow) then
begin
//更新和显示统计提示信息
Label10.Caption:=inttostr(strtoint(Label10.Caption)+1);
Timer1.Enabled:=false;
playing:=false;
btReStart.Enabled:=true;
showmessage('Player 2 Win!');
end;
end
else showmessage('此处不能落子!');
end;
end;
procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
//判断该栅格中是否有黑棋
if chess[ACol, ARow]=1 then
begin
DrawGrid1.Canvas.Pen.Color:=clBlack;
DrawGrid1.Canvas.Brush.Color:=clBlack;
DrawGrid1.Canvas.Ellipse(ACol*21+1, ARow*21+1, (ACol+1)*21-1, (ARow+1)*21-1);
end
else
//判断该栅格中是否有白棋
if chess[ACol, ARow]=2 then
begin
DrawGrid1.Canvas.Pen.Color:=clBlack;
DrawGrid1.Canvas.Brush.Color:=clWhite;
DrawGrid1.Canvas.Ellipse(ACol*21, ARow*21, (ACol+1)*21, (ARow+1)*21);
end;
end;
procedure TForm1.btReStartClick(Sender: TObject);
begin
//重新开始初始化设置
Initiate;
DrawGrid1.Refresh;
playing:=true;
timer1.Enabled:=true;
btReStart.Enabled:=false;
end;
procedure TForm1.btCloseClick(Sender: TObject);
begin
close;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
tempTime:TDateTime;
begin
tempTime:=time;
if IsPlayer1 then
begin
//使得Label1闪烁
if Label1.Font.Color=clBlack then
Label1.Font.Color:=clRed
else
Label1.Font.Color:=clBlack;
Label2.Font.Color:=clBlack;
//累积Player 1 的思考时间
player1Time:=player1Time+(tempTime-currentTime);
Label7.Caption:=TimeToStr(player1Time);
end
else
begin
Label1.Font.Color:=clBlack;
//使得Label2闪烁
if Label2.Font.Color=clBlack then
Label2.Font.Color:=clRed
else
Label2.Font.Color:=clBlack;
//累积Player 2 的思考时间
player2Time:=player2Time+(tempTime-currentTime);
Label8.Caption:=TimeToStr(player2Time);
end;
currentTime:=tempTime;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -