⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 智能人机对弈五子棋游戏,由delphi7编写,赢棋时出现闪烁效果,采用结构优先算法.
💻 PAS
📖 第 1 页 / 共 2 页
字号:

        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
              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;

function TForm1.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('您获胜了。');
            victory:=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('您获胜了。');
            victory:=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('您获胜了。');
            victory:=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('您获胜了。');
            victory:=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('计算机获胜了。');
            victory:=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('计算机获胜了。');
            victory:=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('计算机获胜了。');
            victory:=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('计算机获胜了。');
            victory:=2;
            l.listbegin:=point(i-2,j-2);
            l.point:=2;
            result:=true;
            exit;
          end;
        end;
      end;
    end;
  result:=false;
end;

procedure TForm1.N2Click(Sender: TObject);
begin
  Start;
end;

procedure TForm1.N3Click(Sender: TObject);
begin
  start;
  table[random(10)+5,random(10)+5]:=2;
  drawtable;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -