📄 unit1.pas
字号:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, ExtCtrls, DB, ADODB, StdCtrls;
type
list=record
listbegin:tpoint;
point:byte;
end; //攻防表结构
TMainForm = class(TForm)
MainMenu: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
ShanShuoTimer: TTimer;
ComputerFirst: TMenuItem;
N5: TMenuItem;
N6: TMenuItem;
N7: TMenuItem;
N8: TMenuItem;
N9: TMenuItem;
N10: TMenuItem;
N11: TMenuItem;
N3: TMenuItem;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
DataSource1: TDataSource;
Panel1: TPanel;
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
N4: 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 ComputerFirstClick(Sender: TObject);
procedure N5Click(Sender: TObject);
procedure N10Click(Sender: TObject);
procedure N11Click(Sender: TObject);
procedure N8Click(Sender: TObject);
procedure N7Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure N3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure N4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
Table:array[0..19,0..19]of byte; //棋盘
mx:array[0..1000] of integer;
my:array[0..1000] of integer;
shanshuo:boolean; //用于闪烁处理
l:list; //用于闪烁处理
shengli:byte; //用于闪烁处理
finish:Boolean; //比赛是否结束
String1:String;
// attacklist33,attacklist22:array[0..1000]of list;
// defendlist33:array[0..1000]of list;
procedure Start; //初始化
procedure DrawTable; //画棋盘
procedure Computer; //电脑下棋
function IsWin(show:boolean):boolean; //是否有一方胜利
end;
var
MainForm: TMainForm;
saveX:array[0..1000] of integer;
saveY:array[0..1000] of integer;
saveTable: array[0..1000] of integer;
saveCount:integer;
m:integer;
n:integer;
implementation
{$R *.dfm}
uses
Unit2,saveNameList_Un;
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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -