#include "stdafx.h"
struct Point
{
int x,y;
Point(int x,int y):x(x),y(y){}
void MoveRand()
{
int flag=rand()%4;
switch(flag)
{
case 0: x++; break;
case 1: x--; break;
case 2: y++; break;
case 3: y--; break;
}
}
friend ostream &operator<<(ostream &out,Point &p)
{
return out<<p.x<<","<<p.y;
}
friend istream &operator>>(istream &in,Point &p)
{
return in>>p.x>>p.y;
}
};
class Thief
{
Point Loc;
public:
Thief(int x,int y):Loc(x,y){ }
void MoveStep()
{
Loc.MoveRand();
}
friend ostream &operator<<(ostream &out,Thief &t)
{
return out<<t.Loc;
}
};
int main(int argc, char* argv[])
{
Thief thief(5,5);
for(int time=1; time<=20; time++)
{
thief.MoveStep();
cout<<thief<<endl;
}
return 0;
}
这个程序模拟了一个“小偷”对象在二维平面上的移动。
程序定义了两个类,分别是“Point”和“Thief”。
“Point”类表示二维平面上的一个点,具有两个成员变量“x”和“y”,分别表示它的坐标。该类有一个构造函数,它以初始坐标作为参数,并初始化成员变量。它还有一个名为“MoveRand()”的方法,它在一个随机方向(上、下、左或右)上随机移动一个步长。该类重载了“<<”和“>>”运算符,允许输入和输出Point对象。
“Thief”类表示小偷本身,有一个类型为Point的成员变量“Loc”,表示它当前所在的位置。该类有一个构造函数,它以小偷的初始坐标作为参数,并初始化“Loc”成员变量。它还有一个名为“MoveStep()”的方法,它调用“Loc”对象的“MoveRand()”方法来移动小偷一步。该类重载了“<<”运算符,允许输出Thief对象。
在主函数中,创建了一个初始位置为(5,5)的Thief对象。然后,执行一个循环20次,每次调用小偷对象的“MoveStep()”方法,并使用重载的“<<”运算符输出它的位置。
总体而言,该程序模拟了小偷在二维平面上的随机移动,并在每一步输出它的位置。
#include "stdafx.h"
struct Point
{
int x,y;
Point(int x,int y):x(x),y(y){}
int Dist(Point &p) // 棋盘距离
{
int dx=p.x-x;
int dy=p.y-y;
return abs(dx)+abs(dy);
}
void MoveRand()
{
MoveStep( rand()%4 );
}
int GetFlag(Point &target)
{
int dx=target.x-x;
int dy=target.y-y;
if(abs(dx)>abs(dy))
if(dx>0) return 0; else return 1;
else
if(dy>0) return 2; else return 3;
}
void MoveStep(int flag)
{
switch(flag)
{
case 0: x++; break;
case 1: x--; break;
case 2: y++; break;
case 3: y--; break;
}
}
friend ostream &operator<<(ostream &out,Point &p)
{
return out<<p.x<<","<<p.y;
}
friend istream &operator>>(istream &in,Point &p)
{
return in>>p.x>>p.y;
}
};
class Thief
{
Point Loc;
public:
Thief(int x,int y):Loc(x,y){ }
Point GetLoc(){ return Loc; }
void MoveStep()
{
Loc.MoveRand();
}
friend ostream &operator<<(ostream &out,Thief &t)
{
return out<<t.Loc;
}
};
class Police
{
Point Loc;
Thief *Target;
public:
Police(int x,int y):Loc(x,y){ }
void Meet(Thief *thief){ Target=thief; }
bool Chase() // 追目标
{
int flag = Loc.GetFlag(Target->GetLoc()); // 0 1 2 3
Loc.MoveStep(flag);
int dist=Loc.Dist(Target->GetLoc());
return dist<=1; // 抓住了!
}
friend ostream &operator<<(ostream &out,Police &p)
{
return out<<p.Loc;
}
};
int main(int argc, char* argv[])
{
Thief thief(5,5);
Police police(1,2);
police.Meet(&thief);
bool flag=false;
for(int time=1; flag==false; time++)
{
thief.MoveStep();
flag=police.Chase();
cout<<"小偷:"<<thief<<"\t";
cout<<"警察:"<<police<<endl;
}
return 0;
}
关于程序的一个补充说明是,该程序并没有定义“Thief”类的构造函数,因此可能是在程序的其他部分中进行了定义。另外,“Point”类中的“MoveRand()”方法并没有提供具体的实现细节,可能是在程序的其他部分中进行了实现。
以下是一个可能的实现方式:
#include <iostream>
#include <cstdlib> // 用于生成随机数
using namespace std;
class Point {
public:
int x, y;
Point(int initX, int initY) {
x = initX;
y = initY;
}
void MoveRand() {
int direction = rand() % 4; // 生成0到3之间的随机数
switch (direction) {
case 0: // 向上移动
y++;
break;
case 1: // 向下移动
y--;
break;
case 2: // 向左移动
x--;
break;
case 3: // 向右移动
x++;
break;
}
}
friend ostream& operator<<(ostream& os, const Point& p) {
os << "(" << p.x << ", " << p.y << ")";
return os;
}
};
class Thief {
public:
Point Loc;
Thief(int initX, int initY) : Loc(initX, initY) {
}
void MoveStep() {
Loc.MoveRand();
}
friend ostream& operator<<(ostream& os, const Thief& t) {
os << t.Loc;
return os;
}
};
int main() {
Thief thief(5, 5);
for (int i = 0; i < 20; i++) {
thief.MoveStep();
cout << "Step " << i + 1 << ": " << thief << endl;
}
return 0;
}
这个补充的程序会创建一个初始位置为(5,5)的Thief对象,并在每一步中输出其位置。
#include "stdafx.h"
struct Point
{
int x,y;
Point(int x,int y):x(x),y(y){}
int Dist(Point &p) // 棋盘距离
{
int dx=p.x-x;
int dy=p.y-y;
return abs(dx)+abs(dy);
}
void MoveRand()
{
MoveStep( rand()%4 );
}
int GetFlag(Point &target)
{
int dx=target.x-x;
int dy=target.y-y;
if(abs(dx)>abs(dy))
if(dx>0) return 0; else return 1;
else
if(dy>0) return 2; else return 3;
}
void MoveStep(int flag)
{
switch(flag)
{
case 0: x++; break;
case 1: x--; break;
case 2: y++; break;
case 3: y--; break;
}
}
friend ostream &operator<<(ostream &out,Point &p)
{
return out<<p.x<<","<<p.y;
}
friend istream &operator>>(istream &in,Point &p)
{
return in>>p.x>>p.y;
}
};
class Human
{
protected:
string Name;
Point Loc;
public:
Human(char *p,int x,int y):Name(p),Loc(x,y){}
};
class Thief: public Human
{
public:
Thief(char *p,int x,int y):Human(p,x,y){ }
void MoveStep()
{
Loc.MoveRand();
}
friend ostream &operator<<(ostream &out,Thief &t)
{
return out<<t.Loc;
}
};
class Police: public Human
{
Thief *Target;
Human *Target; //????
public:
Police(char *p,int x,int y):Human(p,x,y){ }
void Meet(Thief *thief){ Target=thief; }
bool Chase() // 追目标
{
int flag = Loc.GetFlag(Target->GetLoc()); // 0 1 2 3
Loc.MoveStep(flag);
int dist=Loc.Dist(Target->GetLoc());
return dist<=1; // 抓住了!
}
friend ostream &operator<<(ostream &out,Police &p)
{
return out<<p.Loc;
}
};
int main(int argc, char* argv[])
{
Thief thief1("小偷1",5,5);
Thief thief2("小偷2",5,5);
Police police1("警察1",1,2);
Police police2("警察2",1,2);
Police police3("警察3",1,2);
police1.Meet(&thief1);
police2.Meet(&thief1);
police3.Meet(&thief2);
/*
bool flag=false;
for(int time=1; flag==false; time++)
{
thief.MoveStep();
flag=police.Chase();
cout<<"小偷:"<<thief<<"\t";
cout<<"警察:"<<police<<endl;
}*/
return 0;
}
#include <iostream>
#include <cstdlib> // for generating random numbers
using namespace std;
class Point {
public:
int x, y;
Point(int initX, int initY) {
x = initX;
y = initY;
}
void MoveRand() {
int direction = rand() % 4; // generate a random number between 0 and 3
switch (direction) {
case 0: // move up
y++;
break;
case 1: // move down
y--;
break;
case 2: // move left
x--;
break;
case 3: // move right
x++;
break;
}
}
friend ostream& operator<<(ostream& os, const Point& p) {
os << "(" << p.x << ", " << p.y << ")";
return os;
}
};
class Thief {
public:
Point Loc;
Thief(int initX, int initY) : Loc(initX, initY) {
}
void MoveStep() {
Loc.MoveRand();
}
friend ostream& operator<<(ostream& os, const Thief& t) {
os << t.Loc;
return os;
}
};
int main() {
Thief thief(5, 5);
for (int i = 0; i < 20; i++) {
thief.MoveStep();
cout << "Step " << i + 1 << ": " << thief << endl;
}
return 0;
}
补充python代码。