在类中使用random来进行循环

时间:2014-03-28 23:30:08

标签: c++ class loops

我想要求用户将罗盘方向设置为char,使用n表示北,e表示东等等,或使用默认值设置为North。例如(0,0,' n')。

然后我想让它在任何方向上随机移动100次。 我现在对班上的循环感到困惑。我不知道应该在哪里添加循环。此外,输出显示我输入的值。

感谢您的帮助!

输出示例:

0 === 0 --- n

#include<iostream>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

class bug
{
public:
    bug();
    bug(int x_pos, int y_pos, char direction);
    void turn(char direction);
    void move(int x, int y,char direction);
    int get_X() const;
    int get_Y() const;
    char get_direction() const;

private:
    int x;
    int y;
    char direction;
};

bug::bug(int x_pos, int y_pos, char d)
{
    x = x_pos;
    y = y_pos;
    direction = d;
}

bug::bug()
{
    x = 0;
    y = 0;
    direction = 'n';
}

void bug::turn(char direction)
{
    int num = 1 + rand() % 3;
    if (direction == 'n')
    {
        if (num == 1)
            direction = 'e';
        else if (num == 2)
            direction = 'w';
        else if (num == 3)
            direction = 'n';
    }
    else if (direction == 'w')
    {
        if (num == 1)
            direction = 'w';
        else if (num == 2)
            direction = 'n';
        else if (num == 3)
            direction = 's';
    }
    else if (direction == 's')
    {
        if (num == 1)
            direction = 's';
        else if (num == 2)
            direction = 'w';
        else if (num == 3)
            direction = 'e';
    }
    else if (direction == 'e')
    {
        if (num == 1)
            direction = 'e';
        else if (num == 2)
            direction = 'n';
        else if (num == 3)
            direction = 's';
    }
}

void bug::move(int x, int y, char direction)
{
    if (direction == 'n')
            y = y + 1;
    else if (direction == 'w')
        x = x - 1;
    else if (direction == 's')
        y = y - 1;
    else if (direction == 'e')
        x = x + 1;
}

int bug::get_X() const
{
    return x;
}

int bug::get_Y() const
{
        return y;
}

char bug::get_direction() const
{
    return direction;
}

void display(bug start)
{
    cout << start.get_X()<<"==="<<start.get_Y()  << "---"<<start.get_direction() << endl;
}

int main()
{
    srand(time(0));
    bug first;
    int choice;
    cout << custom mode(1) or default mode(2) ?  ";
    cin >> choice;

    if (choice == 1)
    {
        int x, y;
        char dir;
        cout << "the x axis: ";
        cin >> x;
        cout << "the y axis: ";
        cin >> y;
        cout << "the direction: ";
        cin >> dir;

        bug first(x, y, dir);
        //first.turn(dir);
        //first.move(x,y);
        display(first);
    }
    else
    {
        for(int j =0;j<100;j++)
        {
            bug first;
            //first.turn;
            //first.move();
            //for(int j =0;j<100;j++)
            display(first);
        }
    }
    system("pause");
}

1 个答案:

答案 0 :(得分:0)

该行缺少"

cout << custom mode(1) or default mode(2) ?  ";

我认为目的是在custom这个词的前面加上双引号:

cout << "custom mode(1) or default mode(2) ?  ";

开始修改
通过逐步调试并观察内部变量与传递的值参数,可以更清楚地看到一些需要考虑的事情。传递的值参数碰巧被命名为与内部类数据成员变量相同。请仔细考虑这些事情,并确定turn的{​​{1}}和move函数所需的行为。{1}}。
结束修改