简体   繁体   English

显示同时具有一个元素的2D数组

[英]Showing 2D array with one element at the time

I have a 2 dimensional array filled with 0s and 1s. 我有一个二维数组,其中填充了0和1。 I have to display that array in way that: - 0s are always shown - 1s are shown one at the time. 我必须以以下方式显示该数组:-始终显示0-一次显示1。

It suppose to look like a maze where 0 is a wall and 1 is a current position. 它看起来像一个迷宫,其中0是墙,而1是当前位置。 How can I do that in c++? 如何在C ++中做到这一点?

EDIT: I came up with a solution but maybe there is simpler one. 编辑:我想出了一个解决方案,但也许有一个更简单的解决方案。 What if I'd create copy of my _array and copy 0s and blank spaces instead of 1s to it. 如果我要创建_array的副本并将0和空格而不是1复制到该副本,该怎么办。 Then in loop I'd assign one of _array "1" to second array then display whole array and then make swap 1 back with blank space? 然后在循环中,我将_array“ 1”之一分配给第二个数组,然后显示整个数组,然后将交换1换成空白?

EDIT2: 编辑2:

int _tmain(int argc, _TCHAR* argv[])
{
    file();
    int k=0,l=0;
    for(int i=0;i<num_rows;i++)
    {
        for(int j=0;j<num_chars;j++)
        {
            if(_array[i][j] == 1)
            {
                k=i;
                l=j;
                break;
            }
        }
    }
    while(1)
    {
        for(int i=0;i<num_rows;i++)
        {
            for(int j=0;j<num_chars;j++)
            {
                if(_array[i][j] == 0) printf("%d",_array[i][j]);
                else if(_array[i][j]==1)
                {
                    if(k==i && l==j)
                    {
                        printf("1");                

                    }
                    else printf(" ");

                }               
                l++;
                if(l>num_chars) break;
            }
            k++;
            l=0;
            printf("\n");               
        }
        k=0;
        system("cls");
    }
    return 0;
}

I wrote something like that but still i don't know how to clear screen in right moment. 我写了类似的东西,但我仍然不知道如何在适当的时候清除屏幕。 Function file() reads from file to 2D array. 函数file()从文件读取到2D数组。

hire you go* [ solved ] * 雇用你去* [ 解决 ] *

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
int x,y;
cin>>x>>y;
char map[x][y];
memset(map, 'a', sizeof(map));
int y_pos = 0;
for (int x_pos = 0; x_pos < x * y; x_pos++){
    if (x_pos == x){
       x_pos = 0;
       y_pos = y_pos + 1;
       cout<<endl;
       }
    if (y_pos == y){
       system("pause");
       return 0;       
       }
    cout<<map[x_pos][y_pos];
    }  

Assuming you want something like that 假设您想要这样的东西

000000
0    0
0000 0
0 1  0
0 0000
000000

You could print a 0 whenever it occurs and a blank space if not. 您可以在任何时候将其打印为0否则为空白。 To handle the current position you could use two additional variables like posX , posY . 要处理当前位置,您可以使用两个附加变量,例如posXposY Now everytime you find a 1 in your array you check if (j == posX && i = posY) and print 1 if so... 现在每次您在数组中找到1 ,都检查if (j == posX && i = posY) ,如果是则打印1

As you just need to visualize the maze at different possible positions I'd propose a simple display function. 由于您只需要在不同可能的位置可视化迷宫,我建议使用一个简单的显示功能。 DisplayMaze(int x, int y) is printing the maze in the required format to the screen. DisplayMaze(int x, int y)将迷宫以所需的格式打印到屏幕上。 If _array[y][x] == 1 there is also printed a single 1 ... 如果_array[y][x] == 1那么还会打印一个1 ...

void DisplayMaze(int x, int y)
{
    for (int row = 0; row < num_rows; row++)
    {
        for (int col = 0; col < num_chars; col++)
        {
            if (_array[row][col] == 0)
                std::cout << "0 ";
            else if (row == y && col == x)
                std::cout << "1 ";
            else
                std::cout << "  ";
        }
        std::cout << std::endl;
    }
    std::cout << std::endl;
}

In order to display all possible positions you have to iterate over all of them and check if the current position is marked with 1 in the array (otherwise displaying would't make sense) 为了显示所有可能的位置,您必须遍历所有位置,并检查当前位置是否在数组中标记为1 (否则显示是没有意义的)

for (int y = 0; y < num_rows; y++)
{
    for (int x = 0; x < num_chars; x++)
    {
        if (_array[y][x] == 1)
        {
            DisplayMaze(x, y);
        }
    }
}

The output should look like: 输出应如下所示:

0 0 0 0 0 0
0 1       0
0 0 0 0   0
0         0
0   0 0 0 0
0 0 0 0 0 0

0 0 0 0 0 0
0   1     0
0 0 0 0   0
0         0
0   0 0 0 0
0 0 0 0 0 0

0 0 0 0 0 0
0     1   0
0 0 0 0   0
0         0
0   0 0 0 0
0 0 0 0 0 0

0 0 0 0 0 0
0       1 0
0 0 0 0   0
0         0
0   0 0 0 0
0 0 0 0 0 0

0 0 0 0 0 0
0         0
0 0 0 0 1 0
0         0
0   0 0 0 0
0 0 0 0 0 0

... ...

However, i'd recommend a more C++ like approach as a maze could be implemented as a class. 但是,我建议使用一种更像C ++的方法,因为可以将迷宫作为一个类来实现。 This class could bring it's own display-method and would encapsulate the internal data. 此类可以带来自己的显示方法,并且可以封装内部数据。 It could basically look like: 它基本上看起来像:

class Maze
{
public:
    // generate empty maze with given size
    Maze(int width, int height);

    // destructor
    ~Maze();

    // print maze if the given position is marked with 1
    void printPosition(int x, int y) const;

    // takes a cstring as input to initialize the maze from
    Maze& operator<<(const char* input);

    // returns true if the given position is marked with 1
    bool isValidPosition(int x, int y) const;

private:
    // this is the actual representation of the maze
    std::vector<std::vector<int> > grid_;
};

it would be used as followes: 它的用法如下:

Maze myMaze(num_chars, num_rows);
myMaze << "000000"
    "011110"
    "000010"
    "011110"
    "010000"
    "000000";

for (int y = 0; y < num_rows; y++)
{
    for (int x = 0; x < num_chars; x++)
    {
        if (myMaze.isValidPosition(x,y))
        {
            myMaze.printPosition(x,y);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM