unit Unit1; {人机对战五子棋,程序设计:徐豪 www.haodisoft.com}interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1; Computer,Player:array[1..15,1..15] of integer; chessboard:array[0..16,0..16] of integer; ShuYing:Integer=0; xi,xj,MaxCou:integer; implementation{$R *.DFM}Function LianZiShu(i,j,XiaQiFang,FangShi:integer):integer; //FangShi =1计算连续子数 =2 判断输赢 var ZiCou:integer; Function FangXiang(i,j,k1,k2:integer):Integer; var t,Cou:integer; begin Cou:=1; for t:=1 to 5 do if ChessBoard[i+t*k1,j+t*k2]=XiaQiFang then inc(Cou) else break; if (ChessBoard[i+t*k1,j+t*k2]>0) and (Fangshi+XiaQiFang=2) then Dec(Cou); for t:=1 to 5 do if ChessBoard[i-t*k1,j-t*k2]=XiaQiFang then inc(Cou) else break; if (ChessBoard[i-t*k1,j-t*k2]>0) and (Fangshi+XiaQiFang=2) then Dec(Cou); Result:=Cou*Cou*Cou*10; if (Cou=5) and (FangShi=2) then ShuYing:=1; end; begin ZiCou:=FangXiang(i,j,1,0); //水平方向连续子数 - ZiCou:=ZiCou+FangXiang(i,j,0,1); //垂直方向连续子数 | ZiCou:=ZiCou+FangXiang(i,j,1,1); //左斜方向连续子数 \ ZiCou:=ZiCou+FangXiang(i,j,1,-1); //右斜方向连续子数 / Result:=ZiCou; end;procedure JiSuan(XiaQiFang:integer); var i,j,Cou:integer; begin for i:=1 to 15 do for j:=1 to 15 do begin if XiaQiFang=1 then Player[i,j]:=0 else computer[i,j]:=0; if ChessBoard[i,j]=0 then begin Cou:=LianZiShu(i,j,XiaQiFang,1); if XiaQiFang=1 then Player[i,j]:=Cou else Computer[i,j]:=cou; if Cou>=MaxCou then begin MaxCou:=Cou; Xi:=i; Xj:=j; end; end; end; end;procedure XiaZi(qzx,qzy,Xia:integer); //在指定位置下子 begin if ShuYing=1 then exit; if Xia=1 then Form1.Canvas.Pen.Mode:=pmWhite else Form1.Canvas.Pen.Mode:=pmBlack; Form1.Canvas.Ellipse(qzx*30-11,qzy*30-11,qzx*30+11,qzy*30+11); chessboard[qzx,qzy]:=Xia; LianZiShu(qzx,qzy,Xia,2); //判断输赢 if ShuYing=1 then if Xia=1 then ShowMessage('你赢了') else ShowMessage('你输了'); end;procedure DrawChessBoard(); //画棋盘 var x:integer; begin Form1.Canvas.Pen.Color:=clblue; for x:=1 to 15 do begin Form1.Canvas.MoveTo(30,x*30); Form1.Canvas.LineTo(450,x*30); end; for x:=1 to 15 do begin Form1.Canvas.MoveTo(x*30,30); Form1.Canvas.LineTo(x*30,450); end; end;procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var qzx,qzy:integer; begin xi:=0; xj:=0; if (x<30) or (y<30) or (x>450) or (y>450) then exit; qzx:=x div 30; qzy:=y div 30; if x mod 30 >15 then inc(qzx); if y mod 30 >15 then inc(qzy); if chessboard[qzx,qzy]<>0 then exit; XiaZi(qzx,qzy,1); MaxCou:=0; JiSuan(1); //判断下子位置 JiSuan(2); XiaZi(Xi,Xj,2); end;procedure TForm1.Button1Click(Sender: TObject); var i,j:integer; begin Form1.Canvas.Brush.Color:=clBtnFace; Form1.Canvas.FillRect(Form1.Canvas.ClipRect); DrawChessBoard; for i:=0 to 16 do for j:=0 to 16 do ChessBoard[i,j]:=0; end;end.
[email protected]
谢谢!
[email protected]
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
Computer,Player:array[1..15,1..15] of integer;
chessboard:array[0..16,0..16] of integer;
ShuYing:Integer=0;
xi,xj,MaxCou:integer;
implementation{$R *.DFM}Function LianZiShu(i,j,XiaQiFang,FangShi:integer):integer; //FangShi =1计算连续子数 =2 判断输赢
var ZiCou:integer;
Function FangXiang(i,j,k1,k2:integer):Integer;
var t,Cou:integer;
begin
Cou:=1;
for t:=1 to 5 do
if ChessBoard[i+t*k1,j+t*k2]=XiaQiFang then inc(Cou) else break;
if (ChessBoard[i+t*k1,j+t*k2]>0) and (Fangshi+XiaQiFang=2) then Dec(Cou); for t:=1 to 5 do
if ChessBoard[i-t*k1,j-t*k2]=XiaQiFang then inc(Cou) else break;
if (ChessBoard[i-t*k1,j-t*k2]>0) and (Fangshi+XiaQiFang=2) then Dec(Cou);
Result:=Cou*Cou*Cou*10;
if (Cou=5) and (FangShi=2) then ShuYing:=1;
end;
begin
ZiCou:=FangXiang(i,j,1,0); //水平方向连续子数 -
ZiCou:=ZiCou+FangXiang(i,j,0,1); //垂直方向连续子数 |
ZiCou:=ZiCou+FangXiang(i,j,1,1); //左斜方向连续子数 \
ZiCou:=ZiCou+FangXiang(i,j,1,-1); //右斜方向连续子数 /
Result:=ZiCou;
end;procedure JiSuan(XiaQiFang:integer);
var i,j,Cou:integer;
begin
for i:=1 to 15 do
for j:=1 to 15 do
begin
if XiaQiFang=1 then Player[i,j]:=0 else computer[i,j]:=0;
if ChessBoard[i,j]=0 then
begin
Cou:=LianZiShu(i,j,XiaQiFang,1);
if XiaQiFang=1 then Player[i,j]:=Cou else Computer[i,j]:=cou;
if Cou>=MaxCou then begin
MaxCou:=Cou;
Xi:=i; Xj:=j;
end;
end;
end;
end;procedure XiaZi(qzx,qzy,Xia:integer); //在指定位置下子
begin
if ShuYing=1 then exit;
if Xia=1 then
Form1.Canvas.Pen.Mode:=pmWhite
else
Form1.Canvas.Pen.Mode:=pmBlack; Form1.Canvas.Ellipse(qzx*30-11,qzy*30-11,qzx*30+11,qzy*30+11);
chessboard[qzx,qzy]:=Xia;
LianZiShu(qzx,qzy,Xia,2); //判断输赢
if ShuYing=1 then
if Xia=1 then ShowMessage('你赢了') else ShowMessage('你输了');
end;procedure DrawChessBoard(); //画棋盘
var x:integer;
begin
Form1.Canvas.Pen.Color:=clblue;
for x:=1 to 15 do begin
Form1.Canvas.MoveTo(30,x*30);
Form1.Canvas.LineTo(450,x*30);
end;
for x:=1 to 15 do begin
Form1.Canvas.MoveTo(x*30,30);
Form1.Canvas.LineTo(x*30,450);
end;
end;procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var qzx,qzy:integer;
begin
xi:=0; xj:=0;
if (x<30) or (y<30) or (x>450) or (y>450) then exit;
qzx:=x div 30;
qzy:=y div 30;
if x mod 30 >15 then inc(qzx);
if y mod 30 >15 then inc(qzy);
if chessboard[qzx,qzy]<>0 then exit;
XiaZi(qzx,qzy,1);
MaxCou:=0;
JiSuan(1); //判断下子位置
JiSuan(2);
XiaZi(Xi,Xj,2);
end;procedure TForm1.Button1Click(Sender: TObject);
var i,j:integer;
begin
Form1.Canvas.Brush.Color:=clBtnFace;
Form1.Canvas.FillRect(Form1.Canvas.ClipRect);
DrawChessBoard;
for i:=0 to 16 do
for j:=0 to 16 do
ChessBoard[i,j]:=0;
end;end.