📄 uagent.~pas
字号:
unit UAgent;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TFagent = class(TForm)
I02: TImage;
I12: TImage;
I22: TImage;
I01: TImage;
I11: TImage;
I21: TImage;
I00: TImage;
I10: TImage;
I20: TImage;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Ieast: TImage;
Least: TLabel;
Iwest: TImage;
Lwest: TLabel;
Isouth: TImage;
Lsouth: TLabel;
Inorth: TImage;
Lnorth: TLabel;
Ifloor: TImage;
Lfloor: TLabel;
Label5: TLabel;
Idirt: TImage;
Ldirt: TLabel;
start: TButton;
stop: TButton;
Timer: TTimer;
Irobot: TImage;
Label6: TLabel;
E1: TEdit;
Label7: TLabel;
E2: TEdit;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure stopClick(Sender: TObject);
procedure turn(var direction:integer);
procedure suck(x,y:integer);
procedure move(direction:integer);
procedure startClick(Sender: TObject);
procedure TimerTimer(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
switch:boolean;
floorstatus:array[0..2,0..2] of boolean;
//robotposition:array[0..2,0..2] of boolean;
robotx,roboty:integer;
robotface:integer;
{ Public declarations }
end;
var
Fagent: TFagent;
implementation
{$R *.dfm}
procedure TFagent.move(direction:integer);
var
intx:integer;
inty:integer;
begin
if (direction=0)and(robotx<2) then
begin
for intx:=0 to 56 do
irobot.Left:=irobot.Left+1;
robotx:=robotx+1;
end;
if (direction=1)and(robotx>0) then
begin
for intx:=0 to 56 do
irobot.Left:=irobot.Left-1;
robotx:=robotx-1;
end;
if (direction=2)and(roboty<2) then
begin
for inty:=0 to 56 do
irobot.top:=irobot.top+1;
roboty:=roboty+1;
end;
if (direction=3)and(roboty>0) then
begin
for inty:=0 to 56 do
irobot.top:=irobot.top-1;
roboty:=roboty-1;
end;
end;
procedure TFagent.suck(x,y:integer);
begin
floorstatus[x,y]:=false;
if (x=0) and (y=0) then
i00.Picture.LoadFromFile('floor.bmp');
if (x=0) and (y=1) then
i01.Picture.LoadFromFile('floor.bmp');
if (x=0) and (y=2) then
i02.Picture.LoadFromFile('floor.bmp');
if (x=1) and (y=0) then
i10.Picture.LoadFromFile('floor.bmp');
if (x=1) and (y=1) then
i11.Picture.LoadFromFile('floor.bmp');
if (x=1) and (y=2) then
i12.Picture.LoadFromFile('floor.bmp');
if (x=2) and (y=0) then
i20.Picture.LoadFromFile('floor.bmp');
if (x=2) and (y=1) then
i21.Picture.LoadFromFile('floor.bmp');
if (x=2) and (y=2) then
i22.Picture.LoadFromFile('floor.bmp');
end;
procedure TFagent.turn(var direction:integer);
begin
if direction=0 then
begin
direction:=2;
irobot.Picture.LoadFromFile('south.bmp');
end
else if direction=1 then
begin
direction:=3;
robotface:=3;
irobot.Picture.LoadFromFile('north.bmp');
end
else if direction=2 then
begin
direction:=1;
robotface:=1;
irobot.Picture.LoadFromFile('west.bmp');
end
else if direction=3 then
begin
direction:=0;
robotface:=0;
irobot.Picture.LoadFromFile('east.bmp');
end;
end;
procedure TFagent.FormCreate(Sender: TObject);
var
inti,intj:integer;
begin
switch:=false;
//初试化图例
ieast.Picture.LoadFromFile('east.bmp');
iwest.Picture.LoadFromFile('west.bmp');
isouth.Picture.LoadFromFile('south.bmp');
inorth.Picture.LoadFromFile('north.bmp');
ifloor.Picture.LoadFromFile('floor.bmp');
idirt.Picture.loadfromfile('dirt.bmp');
randomize;
//初始化地板状态
for inti:=0 to 2 do
for intj:=0 to 2 do
begin
if random(2)>=1 then
floorstatus[inti,intj]:=true
else
floorstatus[inti,intj]:=false;
end;
if floorstatus[0,0] then
i00.Picture.LoadFromFile('dirt.bmp')
else
i00.Picture.LoadFromFile('floor.bmp');
if floorstatus[0,1] then
i01.Picture.LoadFromFile('dirt.bmp')
else
i01.Picture.LoadFromFile('floor.bmp');
if floorstatus[0,2] then
i02.Picture.LoadFromFile('dirt.bmp')
else
i02.Picture.LoadFromFile('floor.bmp');
if floorstatus[1,0] then
i10.Picture.LoadFromFile('dirt.bmp')
else
i10.Picture.LoadFromFile('floor.bmp');
if floorstatus[1,1] then
i11.Picture.LoadFromFile('dirt.bmp')
else
i11.Picture.LoadFromFile('floor.bmp');
if floorstatus[1,2] then
i12.Picture.LoadFromFile('dirt.bmp')
else
i12.Picture.LoadFromFile('floor.bmp');
if floorstatus[2,0] then
i20.Picture.LoadFromFile('dirt.bmp')
else
i20.Picture.LoadFromFile('floor.bmp');
if floorstatus[2,1] then
i21.Picture.LoadFromFile('dirt.bmp')
else
i21.Picture.LoadFromFile('floor.bmp');
if floorstatus[2,2] then
i22.Picture.LoadFromFile('dirt.bmp')
else
i22.Picture.LoadFromFile('floor.bmp');
//初始化机器人状态,包括位置,方向
{for inti:=0 to 2 do
for intj:=0 to 2 do
robotposition[inti,intj]:=false;}
robotx:=random(3);
roboty:=random(3);
{robotposition[robotx,roboty]:=true;}
irobot.Left:=i00.Left+57*robotx;
irobot.Top:=i00.Top+57*roboty;
robotface:=random(4);
case robotface of
0:irobot.Picture.LoadFromFile('east.bmp');
1:irobot.Picture.LoadFromFile('west.bmp');
2:irobot.Picture.LoadFromFile('south.bmp');
3:irobot.Picture.LoadFromFile('north.bmp');
end;
end;
procedure TFagent.stopClick(Sender: TObject);
begin
timer.Enabled:=false;
end;
procedure TFagent.startClick(Sender: TObject);
begin
timer.Enabled:=true;
end;
procedure TFagent.TimerTimer(Sender: TObject);
var
inti,intj:integer;
begin
if switch then
begin
{for inti:=0 to 2 do
for intj:=0 to 2 do
begin
if random(6)>=5 then
floorstatus[inti,intj]:=true
else
floorstatus[inti,intj]:=false;
end; }
randomize;
inti:=random(3);
intj:=random(3);
if random(5)>=4 then
floorstatus[inti,intj]:=true;
if floorstatus[0,0] then
i00.Picture.LoadFromFile('dirt.bmp')
else
i00.Picture.LoadFromFile('floor.bmp');
if floorstatus[0,1] then
i01.Picture.LoadFromFile('dirt.bmp')
else
i01.Picture.LoadFromFile('floor.bmp');
if floorstatus[0,2] then
i02.Picture.LoadFromFile('dirt.bmp')
else
i02.Picture.LoadFromFile('floor.bmp');
if floorstatus[1,0] then
i10.Picture.LoadFromFile('dirt.bmp')
else
i10.Picture.LoadFromFile('floor.bmp');
if floorstatus[1,1] then
i11.Picture.LoadFromFile('dirt.bmp')
else
i11.Picture.LoadFromFile('floor.bmp');
if floorstatus[1,2] then
i12.Picture.LoadFromFile('dirt.bmp')
else
i12.Picture.LoadFromFile('floor.bmp');
if floorstatus[2,0] then
i20.Picture.LoadFromFile('dirt.bmp')
else
i20.Picture.LoadFromFile('floor.bmp');
if floorstatus[2,1] then
i21.Picture.LoadFromFile('dirt.bmp')
else
i21.Picture.LoadFromFile('floor.bmp');
if floorstatus[2,2] then
i22.Picture.LoadFromFile('dirt.bmp')
else
i22.Picture.LoadFromFile('floor.bmp');
end;
if floorstatus[robotx,roboty] then
suck(robotx,roboty);
randomize;
if random(2)>=1 then
turn(robotface);
move(robotface);
e1.Text:='('+inttostr(robotx)+','+inttostr(roboty)+')' ;
e2.Text:=inttostr(robotface);
end;
procedure TFagent.Button1Click(Sender: TObject);
begin
if not switch then
switch:=true
else
switch:=false;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -