📄 unit1.pas
字号:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, ExtCtrls;
type
list=record
listbegin:tpoint;
point:byte;
end; //攻防表结构
TMainForm = class(TForm)
MainMenu: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
ShanShuoTimer: TTimer;
N3: TMenuItem;
N4: TMenuItem;
N5: TMenuItem;
procedure N2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormPaint(Sender: TObject);
procedure ShanShuoTimerTimer(Sender: TObject);
procedure N3Click(Sender: TObject);
procedure N4Click(Sender: TObject);
procedure N5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
Table:array[0..19,0..19]of byte; //棋盘
shanshuo:boolean; //用于闪烁处理
l:list; //用于闪烁处理
shengli:byte; //用于闪烁处理
finish:Boolean; //比赛是否结束
procedure Start; //初始化
procedure DrawTable; //画棋盘
procedure Computer; //电脑下棋
function IsWin(show:boolean):boolean; //是否有一方胜利
end;
var
MainForm: TMainForm;
implementation
{$R *.dfm}
procedure TMainForm.N2Click(Sender: TObject);
begin
showmessage('这是一个具有简单人工智能的五子棋程序。用户为黑子,计算机为白子。'+#13+'作者:曹扬。');
end;
procedure TMainForm.Computer;
label ok;
var
i,j:integer; //循环变量
x,y:integer; //计算机所选的位置
attacklist3,attacklist2:array[0..1000]of list;
defendlist3:array[0..1000]of list;
procedure addattackitem(num:byte;listbegin:tpoint;point:byte); //向攻击表添加内容
var
i:integer;
begin
case num of
3:
begin
for i:=0 to 1000 do
if attacklist3[i].point=0 then
begin
attacklist3[i].listbegin:=listbegin;
attacklist3[i].point:=point;
break;
end;
end;
2:
begin
for i:=0 to 1000 do
if attacklist2[i].point=0 then
begin
attacklist2[i].listbegin:=listbegin;
attacklist2[i].point:=point;
break;
end;
end;
end;
end;
procedure adddefenditem(num:byte;listbegin:tpoint;point:byte); //向防御表添加内容
var
i:integer;
begin
case num of
3:
begin
for i:=0 to 1000 do
if defendlist3[i].point=0 then
begin
defendlist3[i].listbegin:=listbegin;
defendlist3[i].point:=point;
break;
end;
end;
end;
end;
begin
for i:=0 to 1000 do
begin
attacklist3[i].point:=0;
attacklist2[i].point:=0;
defendlist3[i].point:=0;
end; //清空攻防表
repeat
x:=random(10)+5;
y:=random(10)+5;
until table[x,y]=0;
for i:=0 to 19 do
for j:=0 to 19 do
if (table[i,j]=2) then
case (x+y) mod 5 of
0:
if i<19 then
if table[i+1,j]=0 then
begin
x:=i+1;
y:=j;
end;
1:
if (i<19)and(j<19) then
if table[i+1,j+1]=0 then
begin
x:=i+1;
y:=j+1;
end;
2:
if j<19 then
if table[i,j+1]=0 then
begin
x:=i;
y:=j+1;
end;
3:
if i>0 then
if table[i-1,j]=0 then
begin
x:=i-1;
y:=j;
end;
4:
if j>0 then
if table[i,j-1]=0 then
begin
x:=i;
y:=j-1;
end;
end;
//建立攻击表
for i:=0 to 19 do
for j:=0 to 19 do
begin
if table[i,j]=2 then
begin
if i<19 then
if table[i+1,j]=2 then
begin
addattackitem(2,point(i,j),1);
if i<18 then
if table[i+2,j]=2 then
addattackitem(3,point(i,j),1);
end;
if (i<19)and(j<19) then
if table[i+1,j+1]=2 then
begin
addattackitem(2,point(i,j),2);
if (i<18)and(j<18) then
if table[i+2,j+2]=2 then
addattackitem(3,point(i,j),2);
end;
if j<19 then
if table[i,j+1]=2 then
begin
addattackitem(2,point(i,j),3);
if j<18 then
if table[i,j+2]=2 then
addattackitem(3,point(i,j),3);
end;
if (i>0)and(j<19) then
if table[i-1,j+1]=2 then
begin
addattackitem(2,point(i,j),4);
if (i>1)and(j<18) then
if table[i-2,j+2]=2 then
addattackitem(3,point(i,j),4);
end;
end
//建立防御表
else if table[i,j]=1 then
begin
if i<19 then
if (table[i+1,j]=1)and(i<18) then
if table[i+2,j]=1 then
adddefenditem(3,point(i,j),1);
if (i<19)and(j<19) then
if (table[i+1,j+1]=1)and(i<18)and(j<18) then
if table[i+2,j+2]=1 then
adddefenditem(3,point(i,j),2);
if j<19 then
if (table[i,j+1]=1)and(j<18) then
if table[i,j+2]=1 then
adddefenditem(3,point(i,j),3);
if (i>0)and(j<19) then
if (table[i-1,j+1]=1)and(i>1)and(j<18) then
if table[i-2,j+2]=1 then
adddefenditem(3,point(i,j),4);
end;
end;
//处理预测攻击
for i:=0 to 19 do
for j:=0 to 19 do
if table[i,j]=0 then
begin
table[i,j]:=2;
if iswin(false) then
begin
table[i,j]:=0;
x:=i;
y:=j;
goto ok;
end;
table[i,j]:=0;
end;
//处理预测防御
for i:=0 to 19 do
for j:=0 to 19 do
if table[i,j]=0 then
begin
table[i,j]:=1;
if iswin(false) then
begin
table[i,j]:=0;
x:=i;
y:=j;
goto ok;
end;
table[i,j]:=0;
end;
//处理预测次要攻击
for i:=0 to 19 do
for j:=0 to 19 do
begin
if table[i,j]=2 then
begin
if (i<16)and(i>0) then
if (table[i-1,j]=0)and(table[i+1,j]=2)and(table[i+2,j]=0)and(table[i+3,j]=2)and(table[i+4,j]=0) then
begin
x:=i+2;
y:=j;
goto ok;
end;
if (i<16)and(i>0) then
if (table[i-1,j]=0)and(table[i+1,j]=0)and(table[i+2,j]=2)and(table[i+3,j]=2)and(table[i+4,j]=0) then
begin
x:=i+1;
y:=j;
goto ok;
end;
if (i>0)and(j>0)and(i<16)and(j<16) then
if (table[i-1,j-1]=0)and(table[i+1,j+1]=2)and(table[i+2,j+2]=0)and(table[i+3,j+3]=2)and(table[i+4,j+4]=0) then
begin
x:=i+2;
y:=j+2;
goto ok;
end;
if (i>0)and(j>0)and(i<16)and(j<16) then
if (table[i-1,j-1]=0)and(table[i+1,j+1]=0)and(table[i+2,j+2]=2)and(table[i+3,j+3]=2)and(table[i+4,j+4]=0) then
begin
x:=i+1;
y:=j+1;
goto ok;
end;
if (j<16)and(j>0) then
if (table[i,j-1]=0)and(table[i,j+1]=2)and(table[i,j+2]=0)and(table[i,j+3]=2)and(table[i,j+4]=0) then
begin
x:=i;
y:=j+2;
goto ok;
end;
if (j<16)and(j>0) then
if (table[i,j-1]=0)and(table[i,j+1]=0)and(table[i,j+2]=2)and(table[i,j+3]=2)and(table[i,j+4]=0) then
begin
x:=i;
y:=j+1;
goto ok;
end;
if (i>3)and(j>0)and(i<19)and(j<16) then
if (table[i+1,j-1]=0)and(table[i-1,j+1]=2)and(table[i-2,j+2]=0)and(table[i-3,j+3]=2)and(table[i-4,j+4]=0) then
begin
x:=i-2;
y:=j+2;
goto ok;
end;
if (i>3)and(j>0)and(i<19)and(j<16) then
if (table[i+1,j-1]=0)and(table[i-1,j+1]=0)and(table[i-2,j+2]=2)and(table[i-3,j+3]=2)and(table[i-4,j+4]=0) then
begin
x:=i-1;
y:=j+1;
goto ok;
end;
end;
end;
//处理预测次要防御
for i:=0 to 19 do
for j:=0 to 19 do
begin
if table[i,j]=1 then
begin
if (i<16)and(i>0) then
if (table[i-1,j]=0)and(table[i+1,j]=1)and(table[i+2,j]=0)and(table[i+3,j]=1)and(table[i+4,j]=0) then
begin
x:=i+2;
y:=j;
goto ok;
end;
if (i<16)and(i>0) then
if (table[i-1,j]=0)and(table[i+1,j]=0)and(table[i+2,j]=1)and(table[i+3,j]=1)and(table[i+4,j]=0) then
begin
x:=i+1;
y:=j;
goto ok;
end;
if (i>0)and(j>0)and(i<16)and(j<16) then
if (table[i-1,j-1]=0)and(table[i+1,j+1]=1)and(table[i+2,j+2]=0)and(table[i+3,j+3]=1)and(table[i+4,j+4]=0) then
begin
x:=i+2;
y:=j+2;
goto ok;
end;
if (i>0)and(j>0)and(i<16)and(j<16) then
if (table[i-1,j-1]=0)and(table[i+1,j+1]=0)and(table[i+2,j+2]=1)and(table[i+3,j+3]=1)and(table[i+4,j+4]=0) then
begin
x:=i+1;
y:=j+1;
goto ok;
end;
if (j<16)and(j>0) then
if (table[i,j-1]=0)and(table[i,j+1]=1)and(table[i,j+2]=0)and(table[i,j+3]=1)and(table[i,j+4]=0) then
begin
x:=i;
y:=j+2;
goto ok;
end;
if (j<16)and(j>0) then
if (table[i,j-1]=0)and(table[i,j+1]=0)and(table[i,j+2]=1)and(table[i,j+3]=1)and(table[i,j+4]=0) then
begin
x:=i;
y:=j+1;
goto ok;
end;
if (i>3)and(j>0)and(i<19)and(j<16) then
if (table[i+1,j-1]=0)and(table[i-1,j+1]=1)and(table[i-2,j+2]=0)and(table[i-3,j+3]=1)and(table[i-4,j+4]=0) then
begin
x:=i-2;
y:=j+2;
goto ok;
end;
if (i>3)and(j>0)and(i<19)and(j<16) then
if (table[i+1,j-1]=0)and(table[i-1,j+1]=0)and(table[i-2,j+2]=1)and(table[i-3,j+3]=1)and(table[i-4,j+4]=0) then
begin
x:=i-1;
y:=j+1;
goto ok;
end;
end;
end;
//处理3号攻击表
for i:=0 to 1000 do
begin
if attacklist3[i].point <>0 then
begin
case attacklist3[i].point of
1:
if (attacklist3[i].listbegin.X>1)and(attacklist3[i].listbegin.X <16) then
if ord(table[attacklist3[i].listbegin.X-1,attacklist3[i].listbegin.y]=0)+ord(table[attacklist3[i].listbegin.X-2,attacklist3[i].listbegin.y]=0)+ord(table[attacklist3[i].listbegin.X+3,attacklist3[i].listbegin.y]=0)+ord(table[attacklist3[i].listbegin.X+4,attacklist3[i].listbegin.y]=0)>2 then
begin
if table[attacklist3[i].listbegin.X-1,attacklist3[i].listbegin.y]=0 then
begin
x:=attacklist3[i].listbegin.X-1;
y:=attacklist3[i].listbegin.y;
goto ok;
end;
if table[attacklist3[i].listbegin.X+3,attacklist3[i].listbegin.y]=0 then
begin
x:=attacklist3[i].listbegin.X+3;
y:=attacklist3[i].listbegin.y;
goto ok;
end;
end;
2:
if (attacklist3[i].listbegin.X>1)and(attacklist3[i].listbegin.y>1)and(attacklist3[i].listbegin.X<16)and(attacklist3[i].listbegin.y<16) then
if ord(table[attacklist3[i].listbegin.X-1,attacklist3[i].listbegin.y-1]=0)+ord(table[attacklist3[i].listbegin.X-2,attacklist3[i].listbegin.y-2]=0)+ord(table[attacklist3[i].listbegin.X+3,attacklist3[i].listbegin.y+3]=0)+ord(table[attacklist3[i].listbegin.X+4,attacklist3[i].listbegin.y+4]=0)>2 then
begin
if table[attacklist3[i].listbegin.X-1,attacklist3[i].listbegin.Y-1]=0 then
begin
x:=attacklist3[i].listbegin.X-1;
y:=attacklist3[i].listbegin.y-1;
goto ok;
end;
if table[attacklist3[i].listbegin.X+3,attacklist3[i].listbegin.y+3]=0 then
begin
x:=attacklist3[i].listbegin.X+3;
y:=attacklist3[i].listbegin.y+3;
goto ok;
end;
end;
3:
if (attacklist3[i].listbegin.y>1)and(attacklist3[i].listbegin.y<16) then
if ord(table[attacklist3[i].listbegin.X,attacklist3[i].listbegin.y-1]=0)+ord(table[attacklist3[i].listbegin.X,attacklist3[i].listbegin.y-2]=0)+ord(table[attacklist3[i].listbegin.X,attacklist3[i].listbegin.y+3]=0)+ord(table[attacklist3[i].listbegin.X,attacklist3[i].listbegin.y+4]=0)>2 then
begin
if table[attacklist3[i].listbegin.X,attacklist3[i].listbegin.y-1]=0 then
begin
x:=attacklist3[i].listbegin.X;
y:=attacklist3[i].listbegin.y-1;
goto ok;
end;
if table[attacklist3[i].listbegin.X,attacklist3[i].listbegin.y+3]=0 then
begin
x:=attacklist3[i].listbegin.X;
y:=attacklist3[i].listbegin.y+3;
goto ok;
end;
end;
4:
if (attacklist3[i].listbegin.X<18)and(attacklist3[i].listbegin.y>1)and(attacklist3[i].listbegin.X>3)and(attacklist3[i].listbegin.y<16) then
if ord(table[attacklist3[i].listbegin.X+1,attacklist3[i].listbegin.y-1]=0)+ord(table[attacklist3[i].listbegin.X+2,attacklist3[i].listbegin.y-2]=0)+ord(table[attacklist3[i].listbegin.X-3,attacklist3[i].listbegin.y+3]=0)+ord(table[attacklist3[i].listbegin.X-4,attacklist3[i].listbegin.y+4]=0)>2 then
begin
if table[attacklist3[i].listbegin.X+1,attacklist3[i].listbegin.y-1]=0 then
begin
x:=attacklist3[i].listbegin.X+1;
y:=attacklist3[i].listbegin.y-1;
goto ok;
end;
if table[attacklist3[i].listbegin.X-3,attacklist3[i].listbegin.y+3]=0 then
begin
x:=attacklist3[i].listbegin.X-3;
y:=attacklist3[i].listbegin.y+3;
goto ok;
end;
end;
end;
end
else
break;
end;
//处理3号防御表
for i:=0 to 1000 do
begin
if defendlist3[i].point <>0 then
begin
case defendlist3[i].point of
1:
begin
if defendlist3[i].listbegin.X>0 then
if table[defendlist3[i].listbegin.X-1,defendlist3[i].listbegin.y]=0 then
begin
if defendlist3[i].listbegin.X<17 then
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -