📄 workdos.cpp
字号:
Robot_Object->current_place.west=unusable;
Robot_Object->current_place.south=usable;
Robot_Object->current_place.east=usable;
Robot_Object->current_place.value=' ';
Robot_Object->READ_USED=FALSE;
}
//##ModelId=467AA51C0363
Robot::~Robot()
{
}
//##ModelId=467AA51C0381
void Robot::Move(Chess Chess_Object)
{
switch(face_direction)
{
case 'E':
{
if(current_place.east==usable)
{
current_place=Chess_Object.Get_point(current_place.x+1,current_place.y);
cout<<"当前坐标:"<<"("<<current_place.x<<","<<current_place.y<<")"<<"当前坐标的值:"<<current_place.value<<" ";
cout<<"当前机器人的方向:east"<<endl;
}
else
{
if(current_place.east==unusable)
{
if (current_place.sign==borderline)
{cout<<"您到边界了,不能向此方向移,请选者另一方向!"<<endl;}
else
{cout<<"您遇到障碍物了,不能向此方向移动,请选者另一方向!"<<endl;}
}
else
{cout<<"您遇到障碍物了,不能向此方向移动,请选者另一方向!"<<endl;}
}
break;
}
case 'W':
{
if(current_place.west==usable)
{
current_place=Chess_Object.Get_point(current_place.x-1,current_place.y);
cout<<"当前坐标:"<<"("<<current_place.x<<","<<current_place.y<<")"<<"当前坐标的值:"<<current_place.value<<" ";
cout<<"当前机器人的方向:west"<<endl;
}
else
{
if(current_place.west==unusable)
{
if (current_place.sign==borderline)
{cout<<"您到边界了,不能向此方向移,请选者另一方向!"<<endl;}
else
{cout<<"您遇到障碍物了,不能向此方向移动,请选者另一方向!"<<endl;}
}
else
{cout<<"您遇到障碍物了,不能向此方向移动,请选者另一方向!"<<endl;}
}
break;
}
case 'S':
{
if(current_place.south==usable)
{
current_place=Chess_Object.Get_point(current_place.x,current_place.y+1);
cout<<"当前坐标:"<<"("<<current_place.x<<","<<current_place.y<<")"<<"当前坐标的值:"<<current_place.value<<" ";
cout<<"当前机器人的方向:south"<<endl;
}
else
{
if(current_place.south==unusable)
{
if (current_place.sign==borderline)
{cout<<"您到边界了,不能向此方向移,请选者另一方向!"<<endl;}
else
{cout<<"您遇到障碍物了,不能向此方向移动,请选者另一方向!"<<endl;}
}
else
{cout<<"您遇到障碍物了,不能向此方向移动,请选者另一方向!"<<endl;}
}
break;
}
case 'N':
{
if(current_place.north==usable)
{
current_place=Chess_Object.Get_point(current_place.x,current_place.y-1);
cout<<"当前坐标:"<<"("<<current_place.x<<","<<current_place.y<<")"<<"当前坐标的值:"<<current_place.value<<" ";
cout<<"当前机器人的方向:nouth"<<endl;
}
else
{
if(current_place.north==unusable)
{
if (current_place.sign==borderline)
{cout<<"您到边界了,不能向此方向移,请选者另一方向!"<<endl;}
else
{cout<<"您遇到障碍物了,不能向此方向移动,请选者另一方向!"<<endl;}
}
else
{cout<<"您遇到障碍物了,不能向此方向移动,请选者另一方向!"<<endl;}
}
break;
}
}
}
//##ModelId=467AA51C0395
void Robot::Left(char face_direction)
{
switch(face_direction)
{
case 'E':
{
this->face_direction='N';
cout<<"现在机器人的方向:nouth"<<endl;
break;
}
case 'W':
{
this->face_direction='S';
cout<<"现在机器人的方向:south"<<endl;
break;
}
case 'S':
{
this->face_direction='E';
cout<<"现在机器人的方向:east"<<endl;
break;
}
case 'N':
{
this->face_direction='W';
cout<<"现在机器人的方向:west"<<endl;
break;
}
}
}
//##ModelId=467AA51C03BD
void Robot::Read(Chess Chess_Object,int x,int y)
{
READ_USED=TRUE;
if ( Chess_Object.get_point_value(x,y) == ' ' )
{cout<<"当前坐标点的值为空,无法读取!"<<endl;}
else
{
b1=Chess_Object.get_point_value(x,y);
cout<<"b1="<<b1<<endl;
}
}
//##ModelId=467AA51C039F
void Robot::Write(Chess Chess_Object,int x,int y,char filename[20])
{
if(b1 == ' ')
{
if(READ_USED==FALSE)
{cout<<"机器人当前值为空,尚未读取,无法写入!"<<endl;}
else
{cout<<"机器人当前值为空,无法写入!"<<endl;}
}
else
{
current_place.value=b1;
Chess_Object.set_char(x,y,b1,filename);
cout<<"机器人当前坐标("<<current_place.x<<","<<current_place.y<<"),值为:"<<current_place.value<<endl;
}
}
//##ModelId=467AA51C036D
void Robot::evaluate_b1_from_b2()
{
b1=b2;
cout<<"b1="<<b1<<"; "<<"b2="<<b2<<endl;
}
//##ModelId=467AA51C0377
void Robot::evaluate_b2_from_b1()
{
b2=b1;
cout<<"b1="<<b1<<"; "<<"b2="<<b2<<endl;
}
void reset(Robot Robot_Object,Chess Chess_Object,char filename[20]);
void handl_operate(Robot Robot_Object,int control_style,Chess Chess_Object,char filename[20])//手动控制函数
{
char control_order[10];
if(control_style==1)//手动控制
{
cout<<"请输入控制命令"<<endl;
cin>>control_order;
while(strcmp(control_order,"exit"))
{
if(!strcmp(control_order,"move"))
{Robot_Object.Move(Chess_Object);}
else
{
if(!strcmp(control_order,"left"))
{Robot_Object.Left( Robot_Object.Get_Robot_direction() );}
else
{
if(!strcmp(control_order,"read"))
{
Robot_Object.Read(Chess_Object,
Robot_Object.Get_Robot_Current_Place().x,
Robot_Object.Get_Robot_Current_Place().y);
}
else
{
if(!strcmp(control_order,"write"))
{
Robot_Object.Write(Chess_Object,
Robot_Object.Get_Robot_Current_Place().x,
Robot_Object.Get_Robot_Current_Place().y,
filename);
}
else
{
if(!strcmp(control_order,"b1=b2"))
{Robot_Object.evaluate_b1_from_b2();}
else
{
if(!strcmp(control_order,"b2=b1"))
{Robot_Object.evaluate_b2_from_b1();}
else
{
if(!strcmp(control_order,"reset"))
{
reset(Robot_Object,Chess_Object,filename);
Robot_Object.Init_Robot(&Robot_Object);
}
else
cout<<"输入错误,请重新输入"<<endl;
}
}
}
}
}
}
cout<<"请输入控制命令"<<endl;
cin>>control_order;
}
}
}
//////////////////////////////////////////////////自动控制
void handl_auto(Robot Robot_Object,Chess Chess_Object,char filename[20])//手动控制函数
{
fstream orderfile;
char orderfilename[20];
char control_order[10];
cout<<"请输入命令控制文件"<<endl;
cin>>orderfilename;
orderfile.open(orderfilename,ios::nocreate | ios::in | ios::out );
while (!orderfile.eof())
{
orderfile.getline(control_order,sizeof(control_order));
cout<<"命令:"<<control_order<<endl;
if(strcmp(control_order,"exit"))
{
if(!strcmp(control_order,"move"))
{Robot_Object.Move(Chess_Object);}
else
{
if(!strcmp(control_order,"left"))
{Robot_Object.Left( Robot_Object.Get_Robot_direction() );}
else
{
if(!strcmp(control_order,"read"))
{
Robot_Object.Read(Chess_Object,
Robot_Object.Get_Robot_Current_Place().x,
Robot_Object.Get_Robot_Current_Place().y);
}
else
{
if(!strcmp(control_order,"write"))
{
Robot_Object.Write(Chess_Object,
Robot_Object.Get_Robot_Current_Place().x,
Robot_Object.Get_Robot_Current_Place().y,
filename);
control_order[5]='\n';
control_order[4]='\n';
}
else
{
if(!strcmp(control_order,"b1=b2"))
{Robot_Object.evaluate_b1_from_b2();}
else
{
if(!strcmp(control_order,"b2=b1"))
{Robot_Object.evaluate_b2_from_b1();}
else
{
if(!strcmp(control_order,"reset"))
{
reset(Robot_Object,Chess_Object,filename);
Robot_Object.Init_Robot(&Robot_Object);
}
}
//cout<<"输入错误,请重新输入"<<endl;
}
}
}
}
}
}
}
orderfile.close();
}
//////////////////////////////////////////////////单步控制
void handl_step(Robot Robot_Object,Chess Chess_Object,char filename[20])//手动控制函数
{
fstream orderfile;
char orderfilename[20];
char control_order[10];
cout<<"请输入命令控制文件"<<endl;
cin>>orderfilename;
orderfile.open(orderfilename,ios::nocreate | ios::in | ios::out );
while (!orderfile.eof())
{
orderfile.getline(control_order,sizeof(control_order));
cout<<"命令:"<<control_order<<endl;
if(strcmp(control_order,"exit"))
{
if(!strcmp(control_order,"move"))
{Robot_Object.Move(Chess_Object);}
else
{
if(!strcmp(control_order,"left"))
{Robot_Object.Left( Robot_Object.Get_Robot_direction() );}
else
{
if(!strcmp(control_order,"read"))
{
Robot_Object.Read(Chess_Object,
Robot_Object.Get_Robot_Current_Place().x,
Robot_Object.Get_Robot_Current_Place().y);
}
else
{
if(!strcmp(control_order,"write"))
{
Robot_Object.Write(Chess_Object,
Robot_Object.Get_Robot_Current_Place().x,
Robot_Object.Get_Robot_Current_Place().y,
filename);
control_order[5]='\n';
control_order[4]='\n';
}
else
{
if(!strcmp(control_order,"b1=b2"))
{Robot_Object.evaluate_b1_from_b2();}
else
{
if(!strcmp(control_order,"b2=b1"))
{Robot_Object.evaluate_b2_from_b1();}
else
{
if(!strcmp(control_order,"reset"))
{
reset(Robot_Object,Chess_Object,filename);
Robot_Object.Init_Robot(&Robot_Object);
}
//else
//cout<<"输入错误,请重新输入"<<endl;
}
}
}
}
}
}
}
Sleep(1000);
}
orderfile.close();
}
void reset(Robot Robot_Object,Chess Chess_Object,char filename[20])
{
cout<<"重置程序!"<<endl;
Robot_Object.Init_Robot(&Robot_Object);
Chess_Object.init_Chess(filename);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
int control_style;
fstream chessfile;
char filename[20];
char orderfilename[20];
cout<<" Move:机器人朝着正前方移动一个交叉点 "<<endl;
cout<<" Left:机器人原地沿逆时针方向旋转90度。 "<<endl;
cout<<" Read:机器人读取其所在位置的字符,并将这个字符的值赋给b1;如果这个位置上没 "<<endl;
cout<<" 有字符,则不改变b1的当前值。 "<<endl;
cout<<" Write:机器人将b1中的字符写入机器人当前所在的位置,如果这个位置上已经有字符"<<endl;
cout<<" 该字符的值将会被b1的值替代。如果这时b1没有值,提示用户相应的错误信息。 "<<endl;
cout<<"******************************************************************************"<<endl;
cout<<"* 请选者控制方式: 1手动控制 2自动控制 3单步控制 4 重置 5 推出 *"<<endl;
cout<<"******************************************************************************"<<endl;
project_input_chessfile: cout<<"输入您需要的棋盘格式文件(输入时记得输入后缀名):";
cin>>filename;//输入时记得输入后缀名
chessfile.open(filename,ios::nocreate | ios::in | ios::out );
if (!chessfile)
{
cout<<"您要打开的文件不存在!"<<endl;
//cin.clear(cin.rdstate() | ios::badbit | ios::failbit);//清除文件读写的状态
goto project_input_chessfile;
}
chessfile.close();
Chess Chess_Object;//棋盘初始化
Chess_Object.init_Chess( filename );
Robot Robot_Object;
cout<<"选者控制方式:";
cin>>control_style;
switch(control_style)
{
case 1:
{
handl_operate(Robot_Object,control_style,Chess_Object,filename);//手动控制函数
break;
}
case 2:
{
handl_auto(Robot_Object,Chess_Object,filename);//自动控制
break;
}
case 3:
{
handl_step(Robot_Object,Chess_Object,filename);//单步控制
break;
}
case 4:
{
reset(Robot_Object,Chess_Object,filename);//终止机器人的活动,并回到虚拟世界的初始状态。
break;
}
default:
{
cout<<"推出程序!"<<endl;
exit(1);
}
}
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -