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

📄 unit1.~pas

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

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, Menus;

type
  list=record
    listbegin:tpoint;
    point:byte;
  end;  //攻防表结构
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    N4: TMenuItem;
    Timer1: TTimer;
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormCreate(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    Table:array[0..19,0..19]of byte;  //棋盘
    Scintillation:boolean;   //用于闪烁处理
    l:list;             //用于闪烁处理
    Victory:byte;       //用于闪烁处理
    finish:Boolean;  //比赛是否结束
    procedure Start;  //初始化
    procedure DrawTable;  //画棋盘
    procedure Computer;  //电脑下棋
    function IsWin(show:boolean):boolean;  //是否有一方胜利
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Start;
var
  i,j:byte;
begin
  finish:=false;
  for i:=0 to 19 do
    for j:=0 to 19 do
      table[i,j]:=0;
  timer1.Enabled:=false;
  Scintillation:=false;
  drawtable;
end;

procedure TForm1.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;
    timer1.Enabled:=true;
    exit;
  end;
  computer;
  drawTable;
  if iswin(true) then
  begin
    finish:=true;
    timer1.Enabled:=true;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  finish:=true;
  randomize;
  start;
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
  drawTable;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  i:byte;
begin
  iswin(false);
  if Victory=1 then
  begin
    if Scintillation then
    begin
      Scintillation:=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
      Scintillation:=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 Scintillation then
    begin
      Scintillation:=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
      Scintillation:=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 TForm1.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(Form1.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 TForm1.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

⌨️ 快捷键说明

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