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

📄 unit1.pas

📁 五子棋的课程设计
💻 PAS
📖 第 1 页 / 共 3 页
字号:
        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('恭喜您!'+Form2.Edit1.Text+'获胜了。');
            shengli:=1;
            l.listbegin:=point(i,j-2);
            l.point:=3;
            result:=true;
            N3.Enabled:=false;
            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('恭喜您!'+Form2.Edit1.Text+'获胜了。');
            shengli:=1;
            l.listbegin:=point(i+2,j-2);
            l.point:=4;
            result:=true;
            N3.Enabled:=false;
            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('恭喜您!'+Form2.Edit1.Text+'获胜了。');
            shengli:=1;
            l.listbegin:=point(i-2,j-2);
            l.point:=2;
            result:=true;
            N3.Enabled:=false;
            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;
            N3.Enabled:=false;
            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;
            N3.Enabled:=false;
            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;
            N3.Enabled:=false;
            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;
            N3.Enabled:=false;
            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.ComputerFirstClick(Sender: TObject);
begin
  ComputerFirst.Checked := not ComputerFirst.Checked;
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;

procedure TMainForm.N10Click(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TMainForm.N11Click(Sender: TObject);
var
 i,j:integer;
begin
  saveCount:= 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.N8Click(Sender: TObject); //保存文件
var
 i: integer;
 saveTableName:string;
 TableList: TStringList;
 checkInput:boolean;
begin 
  checkInput := InputQuery('请输入','保存的文件名',saveTableName);
  while (saveTableName = 'Users') do
  begin
    showMessage('您不能使用此文件名,请另选文件名!');
    checkInput := InputQuery('请输入','保存的文件名',saveTableName);
    if checkInput = false then
      exit;
  end;
  if checkInput then
  begin
      TableList := TStringList.Create;
      ADOConnection1.GetTableNames(TableList);
      if TableList.IndexOf(saveTableName) <> - 1 then
         //提示用户用户表已存在
      begin
        if MessageDlg('已存在一个同名文件,是否覆盖掉?',mtConfirmation,[mbYes,mbNo],0) = 6 then
        begin
           ADOConnection1.Execute('DROP TABLE '+saveTableName+';');
           ADOConnection1.Execute('CREATE TABLE '+saveTableName+'(x varchar(50),y varchar(50),tablecount varchar(50));');
           ADOQuery1.Close;
           ADOQuery1.SQL.Clear;
           ADOQuery1.SQL.Add('select *from '+saveTableName+';');
           ADOQuery1.Open;
           for i:=0 to saveCount-1 do
           begin
              ADOQuery1.Append;
              ADOQuery1.FieldByName('x').Value := InttoStr(saveX[i]);
              ADOQuery1.FieldByName('y').Value := InttoStr(saveY[i]);
              ADOQuery1.FieldByName('tablecount').Value := InttoStr(saveTable[i]);
              ADOQuery1.Post;
           end;
           ShowMessage('保存成功!');
           ADOQuery1.Close;
        end;
      end
      else
      begin
         ADOConnection1.Execute('CREATE TABLE '+saveTableName+'(x varchar(50),y varchar(50),tablecount varchar(50));');
         ADOQuery1.Close;
         ADOQuery1.SQL.Clear;
         ADOQuery1.SQL.Add('select *from '+saveTableName+';');
         ADOQuery1.Open;
         for i:=0 to saveCount-1 do
         begin
            ADOQuery1.Append;
            ADOQuery1.FieldByName('x').Value := InttoStr(saveX[i]);
            ADOQuery1.FieldByName('y').Value := InttoStr(saveY[i]);
            ADOQuery1.FieldByName('tablecount').Value := InttoStr(saveTable[i]);
            ADOQuery1.Post;
          //  ADOQuery1.Refresh;
         end;
         ShowMessage('保存成功!');
         ADOQuery1.Close;
      end;
      FreeAndNil(TableList);
  end;
end;

procedure TMainForm.N7Click(Sender: TObject); //打开文件
var
  checkInput:boolean;
  openName:string;
  TableList: TStringList;
  openX,openY,openTablecount:integer;
begin
  checkInput := InputQuery('请输入','要打开的文件名',openName);
  while (openName = 'Users') do
  begin
    showMessage('该文件不存在,请另选文件!');
    checkInput := InputQuery('请输入','要打开的文件名',openName);
    if checkInput = false then
      exit;
  end;
  start;
  if checkInput then
  begin
   // finish := true;
    saveCount:= 0;
    TableList := TStringList.Create;
    ADOConnection1.GetTableNames(TableList);
    if TableList.IndexOf(openName) = - 1 then
         //提示用户用户表不存在
    begin
      showMessage('不存在此文件!');
      exit;
    end
    else
    begin 
      ADOQuery1.Close;
      ADOQuery1.SQL.Clear;
      ADOQuery1.SQL.Add('select *from '+openName+';');
      ADOQuery1.Open;
      ADOQuery1.RecNo := 1;
      while(ADOQuery1.RecNo <= ADOQuery1.RecordCount) do
      begin
        openX := StrToInt(ADOQuery1.FieldByName('x').Value);
        openY := StrToInt(ADOQuery1.FieldByName('y').Value);
        openTablecount := StrToInt(ADOQuery1.FieldByName('tablecount').Value);
        table[openX,openY] := openTablecount;

        saveX[saveCount]:=openX;
        saveY[saveCount]:=openY;
        saveTable[saveCount]:=openTablecount;
        saveCount := saveCount+1;

        DrawTable;
        if iswin(true) then
        begin
          finish:=true;
          shanshuotimer.Enabled:=true;
          exit;
        end;
        if ADOQuery1.RecNo < ADOQuery1.RecordCount then
        begin
          ADOQuery1.RecNo := ADOQuery1.RecNo + 1;
        end
        else
          break;
      end;
    end;
  end;
  DrawTable;
  ADOQuery1.Close;

end;

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Application.Terminate;
end;

procedure TMainForm.N3Click(Sender: TObject);
begin
if m<1 then
  begin
  N3.Enabled:=False;
  m:=0;
  end
  else
  begin
  N3.Enabled:=true;
      table[mx[m],my[m]]:=0;
      m:=m-1;
      table[mx[m],my[m]]:=0;
      m:=m-1;
      DrawTable;
      if m<1 then N3.Enabled:=False;
   end;
end;

procedure TMainForm.Button1Click(Sender: TObject);
begin
  N11.Click;
end;

procedure TMainForm.Button2Click(Sender: TObject);
begin
  N3.Click;
  Button2.Enabled:=N3.Enabled;
end;

procedure TMainForm.Button3Click(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TMainForm.N4Click(Sender: TObject);
begin
  Form4.Show;
end;

end.

⌨️ 快捷键说明

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