Advertisement

10 x 10 grid

Started by May 06, 2021 02:44 AM
13 comments, last by pbivens67 3 years, 6 months ago

^ The above post was edited and that changed the formatting. (Naked code was not posted by OP.)

-- Tom Sloper -- sloperama.com

Oh, i've had some bad bug: i and j can't be used to index the grid directly, because they relate to drawn characters which were 4 (now 2) per grid cell.

I've also changed some other things and fixed bugs, now it works:


#include <iostream>
using namespace std;

const int ROWS = 10;
const int COLUMNS = 10;
int grid[10][10] = { 0 };
char arr[ROWS][COLUMNS];

int main() 
{
    grid[0][0] = 1;
    grid[1][1] = 1;
    grid[2][2] = 1;
    grid[0][3] = 1;
		for (int j = 0; j <= COLUMNS*2; j++)
		{
			for (int i = 0; i <= ROWS*2; i++)
			{
				bool h = (j % 2 == 0);
				bool v = (i % 2 == 0);
				if (h&&v)
				{
					cout << '+';
				}
				else if (h)
				{
					cout << '-';
				}
				else if (v) 
				{
					cout << '|';
				}
				else
				{
					if ((i + 1) % 2 == 0 && (j + 1) % 2 == 0) // you changed cell size from 4 to 2 chars, so we need to respect this here as well
					{
					    int gridX = i/2;
                        int gridY = j/2;

						int content = grid[gridY][gridX];
						if(content==1)
						{
							cout << "*";
						}
						else
						{
							cout << ' ';
						}
					}
					else cout << ' ';
				}
			}
			cout << endl;
		}

	return 0;
}

Notice your data grid was only 3 x 2, while drawn grid is 10 x 10. So even if it had worked, you should have got read access violations from reading grid data which was not allocated to the array.

Advertisement

I think for something like this it would be easier to have an array int grid[COLUMNS][ROWS] which is the size of your game map (and what any logic will manipulate), and deal with “scaling” just for the purpose of drawing it. Also not sure where you got the size [3][2] from.

Now drawing to the console you will probably be going line by line, if you look at your grid there is some simple cases and all but one doesn't depend on the data at all. Take this 2x2 example:

+---+---+
|   |   |
| * |   |
|   |   |
+---+---+
|   |   |
|   |   |
|   |   |
+---+---+

You have the horizontal border above the first row, then under each row: +---+---+

Then the first and 3rd row of each cell is just padding and borders, | | |

Then the 2nd row of each cell has the data | * | |

So you can render your grid/map row by row, with each map row being 4 lines of text, plus an extra line for one of the borders. Which could look something like this:

#include <iostream>
#include <math.h>
#include <time.h>
#include <string>

using namespace std;

const int ROWS = 5;
const int COLUMNS = 10;
int grid[COLUMNS][ROWS]; // arr[x][y]

/**Draw one of the horizontal border lines between cells.*/
void border_line()
{
    // Left most is always an intersection
    cout << '+';
    for (int x = 0; x < COLUMNS; ++x)
    {
        // Then each cell has "---+" above it, the + is the top-right of the cell
        cout << "---+";
    }
    cout << endl;
}
void padding_line()
{
    // Left most is always a pipe
    cout << '|';
    for (int x = 0; x < COLUMNS; ++x)
    {
        // Then each cell has "   |", the | is the right cell border
        cout << "   |";
    }
    cout << endl;
}
int main()
{
    grid[0][0] = 1;
    grid[8][2] = 1;
    // cout is going to be top to bottom, left to right
    // Top border first, no data
    border_line();
    for (int y = 0; y < ROWS; ++y)
    {
        // Every row after the first will be the padding line, the data line, a padding line, then the bottom border
        padding_line();
        // Left edge is always a border
        cout << '|';
        for (int x = 0; x < COLUMNS; ++x)
        {
            // The middle of cell has a left and right padding, then the value, then the right border
            auto cell = grid[x][y];
            char cell_chr = cell == 1 ? '*' : ' '; // Decide what char this cell should be in some way
            cout << ' ' << cell_chr << " |";
        }
        cout << endl;
        padding_line();
        border_line();
    }

    return 0;
}

Having a loop using integer % / etc. will work as well, so the exact way is just one of many possibilities.

@JoeJ well it finally worked, thanks a lot

This topic is closed to new replies.

Advertisement