简体   繁体   中英

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. 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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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