📄 unit1.~pas
字号:
begin
if table[defendlist3[i].listbegin.X+3,defendlist3[i].listbegin.y]=0 then
begin
x:=defendlist3[i].listbegin.X-1;
y:=defendlist3[i].listbegin.y;
end
else
continue;
end
else
continue;
goto ok;
end;
end;
2:
begin
if (defendlist3[i].listbegin.X>0)and(defendlist3[i].listbegin.y>0) then
if table[defendlist3[i].listbegin.X-1,defendlist3[i].listbegin.y-1]=0 then
begin
if (defendlist3[i].listbegin.X<17)and(defendlist3[i].listbegin.y<17) then
begin
if table[defendlist3[i].listbegin.X+3,defendlist3[i].listbegin.y+3]=0 then
begin
x:=defendlist3[i].listbegin.X-1;
y:=defendlist3[i].listbegin.y-1;
end
else
continue;
end
else
continue;
goto ok;
end;
end;
3:
begin
if defendlist3[i].listbegin.y>0 then
if table[defendlist3[i].listbegin.X,defendlist3[i].listbegin.y-1]=0 then
begin
if defendlist3[i].listbegin.y<17 then
begin
if table[defendlist3[i].listbegin.X,defendlist3[i].listbegin.y+3]=0 then
begin
x:=defendlist3[i].listbegin.X;
y:=defendlist3[i].listbegin.y-1;
end
else
continue;
end
else
continue;
goto ok;
end;
end;
4:
begin
if (defendlist3[i].listbegin.X<19)and(defendlist3[i].listbegin.y>0) then
if table[defendlist3[i].listbegin.X+1,defendlist3[i].listbegin.y-1]=0 then
begin
if (defendlist3[i].listbegin.X>2)and(defendlist3[i].listbegin.y<17) then
begin
if table[defendlist3[i].listbegin.X-3,defendlist3[i].listbegin.y+3]=0 then
begin
x:=defendlist3[i].listbegin.X+1;
y:=defendlist3[i].listbegin.y-1;
end
else
continue;
end
else
continue;
goto ok;
end;
end;
end;
end
else
break;
end;
//处理2号攻击表
for i:=0 to 1000 do
begin
if attacklist2[i].point <>0 then
begin
case attacklist2[i].point of
1:
begin
if attacklist2[i].listbegin.X>0 then
if table[attacklist2[i].listbegin.X-1,attacklist2[i].listbegin.y]=0 then
begin
x:=attacklist2[i].listbegin.X-1;
y:=attacklist2[i].listbegin.y;
goto ok;
end;
if attacklist2[i].listbegin.X<18 then
if table[attacklist2[i].listbegin.X+2,attacklist2[i].listbegin.y]=0 then
begin
x:=attacklist2[i].listbegin.X+2;
y:=attacklist2[i].listbegin.y;
goto ok;
end;
end;
2:
begin
if (attacklist2[i].listbegin.X>0)and(attacklist2[i].listbegin.y>0) then
if table[attacklist2[i].listbegin.X-1,attacklist2[i].listbegin.y-1]=0 then
begin
x:=attacklist2[i].listbegin.X-1;
y:=attacklist2[i].listbegin.y-1;
goto ok;
end;
if (attacklist2[i].listbegin.X<18)and(attacklist2[i].listbegin.y<18) then
if table[attacklist2[i].listbegin.X+2,attacklist2[i].listbegin.y+2]=0 then
begin
x:=attacklist2[i].listbegin.X+2;
y:=attacklist2[i].listbegin.y+2;
goto ok;
end;
end;
3:
begin
if attacklist2[i].listbegin.y>0 then
if table[attacklist2[i].listbegin.X,attacklist2[i].listbegin.y-1]=0 then
begin
x:=attacklist2[i].listbegin.X;
y:=attacklist2[i].listbegin.y-1;
goto ok;
end;
if attacklist2[i].listbegin.y<18 then
if table[attacklist2[i].listbegin.X,attacklist2[i].listbegin.y+2]=0 then
begin
x:=attacklist2[i].listbegin.X;
y:=attacklist2[i].listbegin.y+2;
goto ok;
end;
end;
4:
begin
if (attacklist2[i].listbegin.X<19)and(attacklist2[i].listbegin.y>0) then
if table[attacklist2[i].listbegin.X+1,attacklist2[i].listbegin.y-1]=0 then
begin
x:=attacklist2[i].listbegin.X+1;
y:=attacklist2[i].listbegin.y-1;
goto ok;
end;
if (attacklist2[i].listbegin.X>1)and(attacklist2[i].listbegin.y<18) then
if table[attacklist2[i].listbegin.X-2,attacklist2[i].listbegin.y+2]=0 then
begin
x:=attacklist2[i].listbegin.X-2;
y:=attacklist2[i].listbegin.y+2;
goto ok;
end;
end;
end;
end
else
break;
end;
ok: //选择出了最佳方案
table[x,y]:=2;
end;
procedure TMainForm.DrawTable;
var
i,j:byte;
buf:tbitmap;
begin
buf:=tbitmap.Create;
buf.Height:=550;
buf.Width:=550;
buf.Canvas.Pen.Width:=2;
buf.canvas.Brush.Color:=rgb(200,200,0);
buf.canvas.FillRect(mainform.ClientRect);
buf.canvas.Pen.Color:=clblack;
for i:=1 to 21 do
begin
buf.canvas.MoveTo(i*25,25);
buf.canvas.LineTo(i*25,525);
end;
for i:=1 to 21 do
begin
buf.canvas.MoveTo(25,i*25);
buf.canvas.LineTo(525,i*25);
end;
buf.canvas.Pen.Width:=18;
for i:=0 to 19 do
for j:=0 to 19 do
begin
case table[i,j] of
1:
buf.canvas.pen.Color:=clblack;
2:
buf.canvas.pen.Color:=clwhite;
0:
buf.canvas.pen.Color:=rgb(200,200,0);
end;
buf.canvas.Ellipse(i*25+35,j*25+35,i*25+40,j*25+40);
end;
canvas.Draw(0,0,buf);
buf.Free;
end;
procedure TMainForm.Start;
var
i,j:byte;
begin
finish:=false;
for i:=0 to 19 do
for j:=0 to 19 do
table[i,j]:=0;
ShanShuotimer.Enabled:=false;
shanshuo:=false;
drawtable;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
finish:=true;
randomize;
start;
end;
procedure TMainForm.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
i,j:byte;
begin
if finish then exit;
DrawTable;
i:=(x-25) div 25;
j:=(y-25) div 25;
if Table[i,j]=0 then
table[i,j]:=1
else
exit;
drawTable;
if iswin(true) then
begin
finish:=true;
shanshuotimer.Enabled:=true;
exit;
end;
computer;
drawTable;
if iswin(true) then
begin
finish:=true;
shanshuotimer.Enabled:=true;
end;
end;
function TMainForm.IsWin(show:boolean): boolean;
var
i,j:byte;
begin
for i:=0 to 19 do
for j:=0 to 19 do
begin
if table[i,j]=1 then
begin
if (i>=2)and(i<=17) then
if (table[i-2,j]=1)and(table[i-1,j]=1)and(table[i+1,j]=1)and(table[i+2,j]=1) then
begin
if show then showmessage('您获胜了。');
shengli:=1;
l.listbegin:=point(i-2,j);
l.point:=1;
result:=true;
exit;
end;
if (j>=2)and(j<=17) then
if (table[i,j-2]=1)and(table[i,j-1]=1)and(table[i,j+1]=1)and(table[i,j+2]=1) then
begin
if show then showmessage('您获胜了。');
shengli:=1;
l.listbegin:=point(i,j-2);
l.point:=3;
result:=true;
exit;
end;
if (i>=2)and(i<=17)and(j>=2)and(j<=17) then
begin
if (table[i-2,j+2]=1)and(table[i-1,j+1]=1)and(table[i+1,j-1]=1)and(table[i+2,j-2]=1) then
begin
if show then showmessage('您获胜了。');
shengli:=1;
l.listbegin:=point(i+2,j-2);
l.point:=4;
result:=true;
exit;
end;
if (table[i-2,j-2]=1)and(table[i-1,j-1]=1)and(table[i+1,j+1]=1)and(table[i+2,j+2]=1) then
begin
if show then showmessage('您获胜了。');
shengli:=1;
l.listbegin:=point(i-2,j-2);
l.point:=2;
result:=true;
exit;
end;
end;
end;
if table[i,j]=2 then
begin
if (i>=2)and(i<=17) then
if (table[i-2,j]=2)and(table[i-1,j]=2)and(table[i+1,j]=2)and(table[i+2,j]=2) then
begin
if show then showmessage('计算机获胜了。');
shengli:=2;
l.listbegin:=point(i-2,j);
l.point:=1;
result:=true;
exit;
end;
if (j>=2)and(j<=17) then
if (table[i,j-2]=2)and(table[i,j-1]=2)and(table[i,j+1]=2)and(table[i,j+2]=2) then
begin
if show then showmessage('计算机获胜了。');
shengli:=2;
l.listbegin:=point(i,j-2);
l.point:=3;
result:=true;
exit;
end;
if (i>=2)and(i<=17)and(j>=2)and(j<=17) then
begin
if (table[i-2,j+2]=2)and(table[i-1,j+1]=2)and(table[i+1,j-1]=2)and(table[i+2,j-2]=2) then
begin
if show then showmessage('计算机获胜了。');
shengli:=2;
l.listbegin:=point(i+2,j-2);
l.point:=4;
result:=true;
exit;
end;
if (table[i-2,j-2]=2)and(table[i-1,j-1]=2)and(table[i+1,j+1]=2)and(table[i+2,j+2]=2) then
begin
if show then showmessage('计算机获胜了。');
shengli:=2;
l.listbegin:=point(i-2,j-2);
l.point:=2;
result:=true;
exit;
end;
end;
end;
end;
result:=false;
end;
procedure TMainForm.FormPaint(Sender: TObject);
begin
drawTable;
end;
procedure TMainForm.ShanShuoTimerTimer(Sender: TObject);
var
i:byte;
begin
iswin(false);
if shengli=1 then
begin
if shanshuo then
begin
shanshuo:=false;
case l.point of
1:
for i:=0 to 4 do
table[l.listbegin.X+i,l.listbegin.Y]:=1;
2:
for i:=0 to 4 do
table[l.listbegin.X+i,l.listbegin.Y+i]:=1;
3:
for i:=0 to 4 do
table[l.listbegin.X,l.listbegin.Y+i]:=1;
4:
for i:=0 to 4 do
table[l.listbegin.X-i,l.listbegin.Y+i]:=1;
end;
end
else
begin
shanshuo:=true;
case l.point of
1:
for i:=0 to 4 do
table[l.listbegin.X+i,l.listbegin.Y]:=0;
2:
for i:=0 to 4 do
table[l.listbegin.X+i,l.listbegin.Y+i]:=0;
3:
for i:=0 to 4 do
table[l.listbegin.X,l.listbegin.Y+i]:=0;
4:
for i:=0 to 4 do
table[l.listbegin.X-i,l.listbegin.Y+i]:=0;
end;
end;
end
else
begin
if shanshuo then
begin
shanshuo:=false;
case l.point of
1:
for i:=0 to 4 do
table[l.listbegin.X+i,l.listbegin.Y]:=2;
2:
for i:=0 to 4 do
table[l.listbegin.X+i,l.listbegin.Y+i]:=2;
3:
for i:=0 to 4 do
table[l.listbegin.X,l.listbegin.Y+i]:=2;
4:
for i:=0 to 4 do
table[l.listbegin.X-i,l.listbegin.Y+i]:=2;
end;
end
else
begin
shanshuo:=true;
case l.point of
1:
for i:=0 to 4 do
table[l.listbegin.X+i,l.listbegin.Y]:=0;
2:
for i:=0 to 4 do
table[l.listbegin.X+i,l.listbegin.Y+i]:=0;
3:
for i:=0 to 4 do
table[l.listbegin.X,l.listbegin.Y+i]:=0;
4:
for i:=0 to 4 do
table[l.listbegin.X-i,l.listbegin.Y+i]:=0;
end;
end;
end;
drawtable;
end;
procedure TMainForm.N3Click(Sender: TObject);
begin
Start;
end;
procedure TMainForm.N4Click(Sender: TObject);
begin
start;
table[random(10)+5,random(10)+5]:=2;
drawtable;
end;
procedure TMainForm.N5Click(Sender: TObject);
procedure huan;
var
i,j:byte;
begin
for i:=0 to 19 do
for j:=0 to 19 do
if table[i,j]=1 then
table[i,j]:=2
else if table[i,j]=2 then
table[i,j]:=1;
end;
begin
start;
table[random(10)+5,random(10)+5]:=1;
while not iswin(false) do
begin
drawtable;
computer;
drawtable;
if iswin(false) then break;
huan;
computer;
huan;
end;
finish:=true;
shanshuotimer.Enabled:=true;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -