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

📄 smain.pas

📁 用delphi编写的人工智能中的过河问题的源码
💻 PAS
📖 第 1 页 / 共 2 页
字号:

// check whether the instance is OK
function IsSafe : Boolean;
begin
   Result := True;
   if (LeftM > 0) and (LeftM < LeftC) then
      Result := False;
   if (RightM > 0) and (RightM < RightC) then
      Result := False;
end;

// Basic Action Performace
function MCAction_Basic(m : Integer; c : Integer; flag : Integer) : Boolean;
var
   s1, s2, s3, s4 : String;
begin
   Result := True;
   s1 := '';   s2 := '';   s3 := '';   s4 := '';
   with Form1.RichEdit1.Lines do
   case flag of
      0 :   // Go up from boat to Left Bank
      begin
         LeftM := LeftM + m;
         LeftC := LeftC + c;
         BoatM := 0;    BoatC := 0;

         if not IsSafe then
         begin
            Result := False;  Exit;
         end;

         DispLeftBank(LeftM, LeftC);
         DispBoat(BoatM, BoatC);
         DispRightBank(RightM, RightC);

         s1 := s1 + '有 ' + InttoStr(m) + ' 个修道士和 ' + InttoStr(c) + ' 个野人从船登陆到左岸.';
         s2 := s2 + '现在船上有 ' + InttoStr(BoatM) + ' 个修道士和 ' + InttoStr(BoatC) + ' 个野人';
         s3 := s3 + '现在左岸有 ' + InttoStr(LeftM) + ' 个修道士和 ' + InttoStr(LeftC) + ' 个野人';
         s4 := s4 + '现在右岸有 ' + InttoStr(RightM) + ' 个修道士和 ' + InttoStr(RightC) + ' 个野人';
         Insert(1, '***************************************');
         Insert(2, s1); Insert(3, s2); Insert(4, s3); Insert(5, s4);
         Insert(6, '***************************************');
      end;
      1 :   // Go down from Left Bank to boat
      begin
         LeftM := LeftM - m;
         LeftC := LeftC - c;
         BoatM := m;    BoatC := c;

         if not IsSafe then
         begin
            Result := False;  Exit;
         end;

         DispLeftBank(LeftM, LeftC);
         DispBoat(BoatM, BoatC);

         s1 := s1 + '有 ' + InttoStr(m) + ' 个修道士和 ' + InttoStr(c) + ' 个野人从左岸上到船.';
         s2 := s2 + '现在船上有 ' + InttoStr(BoatM) + ' 个修道士和 ' + InttoStr(BoatC) + ' 个野人';         
         s3 := s3 + '现在左岸有 ' + InttoStr(LeftM) + ' 个修道士和 ' + InttoStr(LeftC) + ' 个野人';
         s4 := s4 + '现在右岸有 ' + InttoStr(RightM) + ' 个修道士和 ' + InttoStr(RightC) + ' 个野人';
         Insert(1, '***************************************');
         Insert(2, s1); Insert(3, s2); Insert(4, s3); Insert(5, s4);
         Insert(6, '***************************************');
      end;
      2 :   // Go up from boat to Right Bank
      begin
         RightM := RightM + m;
         RightC := RightC + c;
         BoatM := 0;    BoatC := 0;

         if not IsSafe then
         begin
            Result := False;  Exit;
         end;

         DispRightBank(RightM, RightC);
         DispBoat(BoatM, BoatC);

         s1 := s1 + '有 ' + InttoStr(m) + ' 个修道士和 ' + InttoStr(c) + ' 个野人从船登陆到右岸.';
         s2 := s2 + '现在船上有 ' + InttoStr(BoatM) + ' 个修道士和 ' + InttoStr(BoatC) + ' 个野人';
         s3 := s3 + '现在左岸有 ' + InttoStr(LeftM) + ' 个修道士和 ' + InttoStr(LeftC) + ' 个野人';
         s4 := s4 + '现在右岸有 ' + InttoStr(RightM) + ' 个修道士和 ' + InttoStr(RightC) + ' 个野人';
         Insert(1, '***************************************');
         Insert(2, s1); Insert(3, s2); Insert(4, s3); Insert(5, s4);
         Insert(6, '***************************************');
      end;
      3 :   // Go down from Right Bank to boat
      begin
         RightM := RightM - m;
         RightC := RightC - c;
         BoatM := m;    BoatC := c;

         if not IsSafe then
         begin
            Result := False;  Exit;
         end;

         DispRightBank(RightM, RightC);
         DispBoat(BoatM, BoatC);

         s1 := s1 + '有 ' + InttoStr(m) + ' 个修道士和 ' + InttoStr(c) + ' 个野人从左岸上到船.';
         s2 := s2 + '现在船上有 ' + InttoStr(BoatM) + ' 个修道士和 ' + InttoStr(BoatC) + ' 个野人';         
         s3 := s3 + '现在左岸有 ' + InttoStr(LeftM) + ' 个修道士和 ' + InttoStr(LeftC) + ' 个野人';
         s4 := s4 + '现在右岸有 ' + InttoStr(RightM) + ' 个修道士和 ' + InttoStr(RightC) + ' 个野人';
         Insert(1, '***************************************');
         Insert(2, s1); Insert(3, s2); Insert(4, s3); Insert(5, s4);
         Insert(6, '***************************************');
      end;
      else
         ShowMessage('The Value of Flag is From 0 to 3.');
   end;
end;

// Action Performace
procedure MCAction(m : Integer; c : Integer; step : Integer; flag : Integer);
begin
   with Form1.RichEdit1 do
   begin
      Paragraph.Alignment := taCenter;
      Lines.Insert(0, String('Step ' + InttoStr(step)));
//      Paragraph.Alignment := taLeftJustify;
      MCAction_Basic(m, c, flag);
      Form1.GroupBox1.Refresh;
      Form1.RichEdit1.Refresh;
      Sleep(2000);
   end;
end;

// Increase myself by one
function IncM(var i : Integer) : Integer;
begin
   Inc(i);
   Result := i;
end;

//***************************************************
// Save the result of procedure
procedure TForm1.Button4Click(Sender: TObject);
begin
   with SaveDialog1 do
   begin
      DefaultExt := 'doc';
      FileName:='new.doc';
      Filter:='Word文件(*.doc)|*.doc|文本文件(*.txt)|*.txt';
      if (Filename<>'') and execute then
         Form1.RichEdit1.Lines.SaveToFile(FileName);
   end;
end;

// Quit the program
procedure TForm1.Button3Click(Sender: TObject);
begin
   Form1.Close;
end;

// Action performce of "自我介绍" button
procedure TForm1.Button2Click(Sender: TObject);
begin
   with Richedit2 do
   begin
      if Showing then
      begin
         Hide;
         exit;
      end
      else
         Show;

      WordWrap := True;
      Clear;
      Paragraph.Alignment := taCenter;
      Lines.Append('自我介绍');
      Paragraph.Alignment := taLeftJustify;

      Lines.Append('    修道士过河问题,是"专家系统开发初步"题目内容,采用Delphi7.0为开发工具,演示了3个修道士和3个野人的过河问题。');
      Lines.Add('');
      Lines.Add('组长:×××');
      Lines.Add('组员:×××、×××、×××');
      Lines.Add('      ×××、×××');

      Lines.Add('');
      Lines.Add('班级:02级计算机1系');
      Lines.Add('时间:2005年5月5日');
      
      Lines.Add('');
      Lines.Add('Notice : 由于该演示是单线程独占方式,故演示过程任何对该程序的操作都不予以反应。请耐心等候!');
   end;
end;

// Initial the program
procedure TForm1.FormCreate(Sender: TObject);
begin
   with Form1 do
   begin
      RichEdit2.Hide;
      Button4.Hide;
   end;
end;

// Action Performace of '自动演示' Button
procedure TForm1.Button1Click(Sender: TObject);
var
   i : Integer;
begin
   i := 0;
   Form1.RichEdit1.Clear;
   ShowMessage('演示总需时46秒钟');
   Button1.Enabled := False;
   Initial;

   MCAction(0,0,IncM(i),0);   // 0修0野上左岸
   MCAction(0,2,IncM(i),1);   // 0修2野下船
   MCAction(0,2,IncM(i),2);   // 0修2野上右岸
   MCAction(0,1,IncM(i),3);   // 0修1野下船

   MCAction(0,1,IncM(i),0);   // 0修1野上左岸
   MCAction(0,2,IncM(i),1);   // 0修2野下船
   MCAction(0,2,IncM(i),2);   // 0修2野上右岸
   MCAction(0,1,IncM(i),3);   // 0修1野下船

   MCAction(0,1,IncM(i),0);   // 0修1野上左岸
   MCAction(2,0,IncM(i),1);   // 2修0野下船
   MCAction(2,0,IncM(i),2);   // 2修0野上右岸
   MCAction(1,1,IncM(i),3);   // 1修1野下船

   MCAction(1,1,IncM(i),0);   // 1修1野上左岸
   MCAction(2,0,IncM(i),1);   // 2修0野下船
   MCAction(2,0,IncM(i),2);   // 2修0野上右岸
   MCAction(0,1,IncM(i),3);   // 0修1野下船

   MCAction(0,1,IncM(i),0);   // 0修1野上左岸
   MCAction(0,2,IncM(i),1);   // 0修2野下船
   MCAction(0,2,IncM(i),2);   // 0修2野上右岸
   MCAction(0,1,IncM(i),3);   // 0修1野下船

   MCAction(0,1,IncM(i),0);   // 0修1野上左岸
   MCAction(0,2,IncM(i),1);   // 0修2野下船
   MCAction(0,2,IncM(i),2);   // 0修2野上右岸

   ShowMessage('演示完毕...');
   Button1.Enabled := True;
   Button4.Show;
end;

end.

⌨️ 快捷键说明

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