警察抓小偷

#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代码。

  • 26
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Arduion是一款开源的硬件平台,可以用来制作智能小车。智能小车可以通过各种传感器和控制器实现多种功能,例如避障、巡线和追踪等。 智能小车可以用于模拟小偷和警察的追逐过程。首先,我们可以给小车装备一个红外线传感器,用于检测周围环境。当传感器检测到有障碍物靠近时,小车会自动停下来或改变方向,避免碰撞。 在模拟小偷和警察的追逐过程中,我们可以使用线性编码器或红外线传感器配合电机控制来实现巡线功能。小车可以根据线路上的颜色变化进行判断,保持沿着线路的方向前行。当小偷的小车离开了预定的线路时,警察的小车可以通过传感器检测到偏离线路,并及时追踪。 除了巡线功能,我们还可以添加其他功能来增强智能小车的能力,例如声音检测、摄像头监控等。小偷携带的装置如果发出声音,就可以通过声音传感器检测到,并通知警察的小车。同时,摄像头监控可以实时录制和传输小车周围的画面,警察可以通过遥控器观察并及时采取行动。 总的来说,通过Arduion智能小车,我们可以模拟小偷和警察之间的追逐过程。小车装备了多种传感器和控制器,可以实现避障、巡线、追踪等功能,使得游戏更加有趣和富有挑战性。同时,我们也可以根据自己的需求来扩展小车的功能,提升整个游戏体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值