Advertisement

10 x 10 grid

Started by May 06, 2021 02:44 AM
13 comments, last by pbivens67 3 years, 6 months ago
I have drawn a 10 x 10 grid using 3 underscore characters and 1  pipe symbol for each square, I want to draw 3 pipe symbols on top of each other in each square. this is a very easy question I just want somebody else opinion. I just want to draw a 10 x 10 grid where I can move a point in each square, from square to square.

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

using namespace std;

const int ROWS = 10;
const int COLUMNS = 10;

string arr[ROWS][COLUMNS];

int main() 
{
		for (int i = 0; i < ROWS; i++)
		{
			for (int j = 0; j < COLUMNS; j++)
			{
				arr[i][j]="|___";
				cout << arr[i][j];
			}
			cout << endl;
		}

	return 0;
}

sorry but I put my question in my code

Advertisement

You know a question ends with a question mark?

As for my opinion, it doesn't look like a 3 lines high square to me.

Maybe what you want would be somewhat like this:

		for (int j = 0; j < COLUMNS; j++)
		{
			for (int i = 0; i < ROWS; i++)
			{
				bool h = (i%4 == 0);
				bool v = (j%4 == 0);
				if (h && v) cout << '+';
				else if (h) cout << '-';
				else if (v) cout << '|';
				else cout << ' ';
			}
			cout << endl;
		}

(Should draw a grid, but did not run to check if it works.)

well joe it does not draw a grid

It does, but i had confused i and j: http://cpp.sh/6cs35

#include <iostream>
#include <string>

int main()
{
  for (int j = 0; j < 13; j++)
		{
			for (int i = 0; i < 21; i++)
			{
				bool h = (j%4 == 0);
				bool v = (i%4 == 0);
				if (h && v) std::cout << '+';
				else if (h) std::cout << '-';
				else if (v) std::cout << '|';
				else std::cout << ' ';
			}
			std::cout << std::endl;
		}
}

+---+---+---+---+---+
|   |   |   |   |   |
|   |   |   |   |   |
|   |   |   |   |   |
+---+---+---+---+---+
|   |   |   |   |   |
|   |   |   |   |   |
|   |   |   |   |   |
+---+---+---+---+---+
|   |   |   |   |   |
|   |   |   |   |   |
|   |   |   |   |   |
+---+---+---+---+---+
Advertisement

well I adjusted your code and I made a 10 x 10 grid, thanks alot

one last question, how do I draw a “*” in one the grid squares?

pbivens67 said:
one last question, how do I draw a “*” in one the grid squares?

Sounds you want to make a game.

Then, you probably need game world and actors in memory. Above code has no memory - it just draws the grid procedurally.

So you want an array, e.g.:

int grid[10][10];

Set it all to zero, and set grid[3][2] = 1; to mark where the ‘*’ is.

then, while drawing look up what's in current cell and draw it too:


  for (int j = 0; j < 13; j++)
		{
			for (int i = 0; i < 21; i++)
			{
				bool h = (j%4 == 0);
				bool v = (i%4 == 0);
				if (h && v) std::cout << '+';
				else if (h) std::cout << '-';
				else if (v) std::cout << '|';
				else 
				{
					if ((i+2)%4 == 0 && (j+2)%4 == 0) // we are at the middle of a cell
					{ 
						int content = grid[j][i];
						if (content == 1) 
							std::cout << '*';
						else
							std::cout << ' ';
					}
					else std::cout << ' ';
				}
			}
			std::cout << std::endl;
		}

Ofc. that drawing code could be optimized. Actually it's becoming too much if else else else…

well I have almost solved my problem, is my code correct? it does not draw the asterix on the board

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

using namespace std;

const int ROWS = 21;
const int COLUMNS = 21;
//int grid[10][10] = { 0 };
int grid[3][2] = { 1 };
char arr[ROWS][COLUMNS];

int main() 
{
		for (int j = 0; j < COLUMNS; j++)
		{
			for (int i = 0; i < ROWS; 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 + 2) % 4 == 0 && (j + 2) % 4 == 0)
					{
						int content = grid[j][i];
						if(content==1)
						{
							cout << "*";
						}
						else
						{
							cout << ' ';
						}
					}
					cout << ' ';
				}
			}
			cout << endl;
		}

	return 0;
}

This topic is closed to new replies.

Advertisement