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

📄 unit1.pas

📁 五子棋的课程设计
💻 PAS
📖 第 1 页 / 共 3 页
字号:
            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;
  saveX[saveCount]:=x;
  saveY[saveCount]:=y;
  saveTable[saveCount]:=table[x,y];
  saveCount := saveCount+1;
  //////////////////////////////////////
  
  m:=m+1;
  N3.Enabled:=true;
  Button2.Enabled:=N3.Enabled;
  mx[m]:=x;
  my[m]:=y;
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(100,200,0);
  buf.canvas.FillRect(mainform.ClientRect);
  buf.canvas.Pen.Color:=clblack;
  for i:=1 to 21 do        //每一个单元格的大小是25*25
  begin
    buf.canvas.MoveTo(i*25,25);    //单元格的边线从(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      //由table[i,j]的值决定画笔的颜色
      1:
        buf.canvas.pen.Color:=clblack;  //白棋,即计算机所下的棋
      2:
        buf.canvas.pen.Color:=clwhite;  //黑棋,即玩家所下的棋
      0:
        buf.canvas.pen.Color:=rgb(100,200,0);
      end;
      buf.canvas.Ellipse(i*25+35,j*25+35,i*25+40,j*25+40);
    end;
  canvas.Draw(0,0,buf);    //从窗体的(0,0)点位显示图
  buf.Free;
  n:=m div 2;
  Edit1.Text:=IntToStr(n);
end;

procedure TMainForm.Start;
var
  i,j:byte;
begin
  finish:=false;
  m:=0;
  for i:=0 to 19 do
    for j:=0 to 19 do
      table[i,j]:=0;
  ShanShuotimer.Enabled:=false;
  shanshuo:=false;
  N3.Enabled:=false;
  Button2.Enabled:=N3.Enabled;
  drawtable;
end;

procedure TMainForm.FormCreate(Sender: TObject);
var
  i,j:integer;
begin
  saveCount:= 0;
  N3.Enabled:=false;
  Button2.Enabled:=N3.Enabled;
  m:=0;
  finish:=false;
  if ComputerFirst.Checked = true then
  begin
    start;
    i:= random(10)+5;
    j:= random(10)+5;
    table[i,j]:=2;
    drawtable;

    saveX[saveCount]:=i;
    saveY[saveCount]:=j;
    saveTable[saveCount]:=table[i,j];
    saveCount := saveCount+1;
  end
  else
    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
  begin
    table[i,j]:=1;
    /////
    m:=m+1;
    N3.Enabled:=true;
    Button2.Enabled:=N3.Enabled;
    mx[m]:=i;
    my[m]:=j;
    end
  else
    exit;
  drawTable;

  saveX[saveCount]:=i;
  saveY[saveCount]:=j;
  saveTable[saveCount]:=table[i,j];
  saveCount := saveCount+1;

  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('恭喜您!'+Form2.Edit1.Text+'获胜了。');
            shengli:=1;
            l.listbegin:=point(i-2,j);
            l.point:=1;
            result:=true;
            N3.Enabled:=false;
            exit;
          end;

⌨️ 快捷键说明

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