簡體   English   中英

如何解決此c ++控制台映射問題?

[英]How can I fix this c++ console map problem?

我的地圖有問題,因為我想在那兒放很多機器人。 我應該使用其他繪圖想法,或者只是使其更好地工作。 看起來像這樣(有很多點,我不知道如何解決):

... @ ...

@ ......

。@ .....

.....

.....

但我想達到它:

... @ ..

@ .....

。@ ....

.....

.....

#include <iostream>
#include <vector>
#include <string>
#include <ctime>
#include <cstdlib>
#include <Windows.h>
#include <iomanip>

class Bot
{
public:
    Bot(const int mapX, const int mapY)
    {
        x = rand() % mapX;
        y = rand() % mapY;
    }
    int getPosX()
    {
        return x;
    }
    int getPosY()
    {
        return y;
    }
private:
    int x;
    int y;
    const char sym = '@';
};
class Map
{
public:
    std::vector<Bot> bots;
    Map()
    {
        for (int i = 0; i < startingBotsNumber; i++)
        {
            bots.push_back(Bot(getMapSizeX(), getMapSizeY()));
        }
    }
    int getMapSizeX()
    {
        return x;
    }
    int getMapSizeY()
    {
        return y;
    }
    void draw()
    {
        for (int i = 0; i < y; i++)
        {
            for (int j = 0; j < x; j++)
            {
                for (int k = 0; k < startingBotsNumber; k++)
                {
                    if (bots[k].getPosX() == j && bots[k].getPosY() == i)
                        std::cout << '@';
                }
                std::cout << '.';
            }
            std::cout << std::endl;
        }
    }
private:
    const int x = 5;
    const int y = 5;
    const int startingBotsNumber = 3;
};
class Game
{
public:
    void run()
    {
        while (true)
        {
            m.draw();
            system("cls");
        }
    }
    Map m;
private:
};

我需要提示或其他BCS,我不知道如何解決。

for (int k = 0; k < startingBotsNumber; k++)
{
    if (bots[k].getPosX() == j && bots[k].getPosY() == i)
        std::cout << '@';
}
std::cout << '.';

即使已經為該位置打印了“ @”,此代碼也會打印另一個“。”。 這導致一個額外的“。” 對於每個“ @”。

相反,您可以使用如下所示的內容:

bool found = false;
for (int k = 0; k < startingBotsNumber; k++)
    if (bots[k].getPosX() == j && bots[k].getPosY() == i)
    {
        found = true;
        break;
    }
if (found)
    std::cout << '@';
else
    std::cout << '.';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM