📄 robot.h
字号:
#include <stdlib.h>
#include <iostream.h>
#include <string.h>
enum DIRECTION{usable,unusable,visit};//坐标点的方向属性:此方向是否可走,是否被访问过用来记录路径
enum SIGNATURE{borderline,obstacle,inner_point};//坐标点的基本属性:边界,障碍,棋盘内的点
typedef struct POINT_CHESS
{
int x,y;//坐标值x横坐标,y纵坐标
char value;//点的值
DIRECTION east, west, south, north;//4个方向
SIGNATURE sign;
} POINT_CHESS;
class Chess
{
public:
int NumOfMatrix;//使用此变量可以便于棋盘数的更改
POINT_CHESS *Chess_Point[14][14];//按行存储
Chess();
~Chess();
friend class Robot;//声明此友元类 方便操作
};
Chess::Chess()//初始化棋盘 按行存储 尚未设置障碍物
{
NumOfMatrix=14;
for(int i=0;i<NumOfMatrix;i++)
{
for(int j=0;j<NumOfMatrix;j++)
{
Chess_Point[i][j]->x=i;
Chess_Point[i][j]->y=j;
Chess_Point[i][j]->value=' ';//坐标点值初始化为空格
if(i==0 || j==0 || i==13 || j==13)
{
Chess_Point[i][j]->east=usable;
Chess_Point[i][j]->west=usable;
Chess_Point[i][j]->south=usable;
Chess_Point[i][j]->north=usable;
Chess_Point[i][j]->sign=borderline;
if(i==0)
{Chess_Point[i][j]->north=unusable;}
if(j==0)
{Chess_Point[i][j]->east=unusable;}
if(i==13)
{Chess_Point[i][j]->south=unusable;}
if(j==0)
{Chess_Point[i][j]->north=unusable;}
}
else
{
Chess_Point[i][j]->east=usable;
Chess_Point[i][j]->west=usable;
Chess_Point[i][j]->south=usable;
Chess_Point[i][j]->north=usable;
Chess_Point[i][j]->sign=inner_point;
}
}
}
}
Chess::~Chess()
{
for(int i=0;i<NumOfMatrix;i++)
{
for(int j=0;j<NumOfMatrix;j++)
{
Chess_Point[i][j]=NULL;
delete Chess_Point[i][j];
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
class Robot
{
Chess Chess_Object;//生成棋盘的一个对象
private:
char b1,b2;//机器人的变量
char face_derection;//机器人面对的方向
POINT_CHESS *current_place;//机器人当前的坐标点
BOOL READ_USED;//是否已经进行读操作,默认为false
public:
Robot();
~Robot();
Move(char face_derection);
Left(char face_derection);
Wright();
Read();
Output_error();
};
Robot::Robot()
{
current_place=Chess_Object.Chess_Point[0][0];//在此对当前机器人的位置进行初始化定位为(0,0),在主程序中再进行初始化
face_derection='N';//E, W,S,N分别代表东西南北4个方向
b1=b2=' ';//初始为空格
READ_USED=FALSE;
}
Robot::~Robot()
{
current_place=NULL;
delete current_place;
}
Robot::Output_error()
{
cout<<"sorry !this direction cannot get across !"<<endl;
}
Robot::Move(char face_derection)
{
switch(face_derection)
{
case 'E':
{
if(current_place->east==usable)
{
current_place=Chess_Object.Chess_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;}
}
}
break;
}
case 'W':
{
if(current_place->west==usable)
{
current_place=Chess_Object.Chess_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;}
}
}
break;
}
case 'S':
{
if(current_place->south==usable)
{
current_place=Chess_Object.Chess_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;}
}
}
break;
}
case 'N':
{
if(current_place->north==usable)
{
current_place=Chess_Object.Chess_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;}
}
}
break;
}
}
}
Robot::Left(char face_derection)
{
switch(face_derection)
{
case 'E':
{
this->face_derection='N';
cout<<"现在机器人的方向:nouth"<<endl;
break;
}
case 'W':
{
this->face_derection='S';
cout<<"现在机器人的方向:south"<<endl;
break;
}
case 'S':
{
this->face_derection='E';
cout<<"现在机器人的方向:east"<<endl;
break;
}
case 'N':
{
this->face_derection='W';
cout<<"现在机器人的方向:west"<<endl;
break;
}
}
}
Robot::Read()
{
READ_USED=TRUE;
if (current_place->value==' ')
{cout<<"当前坐标点的值为空,无法读取!"<<endl;}
else
{this->b1=current_place->value;}
}
Robot::Wright()
{
if (this->b1==' ' && this->READ_USED==FALSE)
{cout<<"机器人当前值为空,无法写入!"<<endl;}
else
{current_place->value=this->b1;}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -