简体   繁体   English

c ++生成带有O和X的二维数组

[英]c++ generate 2d array with O's and X's

I need help trying to generate a 2D array (20x20 grid) with O's and X's.我需要帮助尝试生成带有 O 和 X 的二维数组(20x20 网格)。 I'm later going to replace these with images to build somewhat of a map/grid.我稍后将用图像替换这些以构建某种地图/网格。 But I just need to fill them in with characters and not integers.但我只需要用字符而不是整数来填充它们。

I have this so far:到目前为止我有这个:

    char array[20][20];

srand(time(NULL));

for(int i=0;i<20;i++)
 {
     for(int j=0;j<20;j++) {
         array[i][j]= ((rand() % 2) == 0) ? 'O' : 'X';
     }
 }

I don't know if that formatted right because the code thing is being weird for me, but if I have this right.我不知道格式是否正确,因为代码对我来说很奇怪,但如果我有这个权利。 How exactly do I print it out for when I run it?当我运行它时,我如何准确地打印出来? I can't test it because I don't know how to print it out :/ But I feel like I have it wrong anyway.我无法测试它,因为我不知道如何打印出来:/但我觉得无论如何我都错了。

EDIT Then I also need to know how to swap the multidimensional array vertically... still keeping the same values/grid setup, but basically just reflecting it vertically.编辑然后我还需要知道如何垂直交换多维数组......仍然保持相同的值/网格设置,但基本上只是垂直反映它。 Not horizontally though..虽然不是水平的..

You have the generation part right.您拥有正确的生成部分。 As for printing, use至于打印,请使用

for(int i = 0; i < 20; ++i)
{
    for(int j = 0; j < 20; ++j)
    {
        std::cout << array[i][j];
    }
    std::cout << "\n";
}

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

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