# drawing a 2d array

• 09-05-2002
Sekti
drawing a 2d array
ok im makeing a game and the maps are 80 accross 15 down and the only way i can think of drawing it is with a lot of
couts or with alot of loops but i cant get the loop idea to work any have some sample code from an old program basicly its like if it is 0 draw the floor if it is 1 draw the wall.
• 09-05-2002
matheo917
i don't have any sample programs, but it sounds like relatively easy iterative program, the idea lies in the logic, have a nested (double) loop , first one iterates 15 times and the inner loop iterates 80 times, and within the inner loop make a conditional check....

Code:

if(x == 0) cout << something;

else if(x == 1) cout<< somthingelse;

hope this can get you started.....

try to start on your own if you get stuck then post some code and we'll try to help you..

good luck...

matheo917
• 09-05-2002
datainjector
i am sorry
here is a tip..

use a 2d array like map [5][5]
Code:

then say map[5][5] = { 1, 0, 1, 0, 0, 1, 0, 0, 1, , 0};
for ( row =0; row <= 4; row++ )
{
for ( col =0; col <= 4; col++ )
if ( map[row][col] == 1 )
//print *
else
}        //print " "

• 09-05-2002
harryP
Those work, but as you've got a rather large map, consider putting it in a text file. You could make '-'s be grass and then '~' be water. Then, you could load the file, and based on the character it reads, it could draw a certain character. Here's some minimal code. It will load up a file, assign some values into a map[][] array based on the characters on the file, and then print the map:
Code:

#include <fstream.h>
#include <stdio.h>

int map[80][15];
#define GRASS 1
#define WATER 2

...

void Map()
{
int i;
int j;
ifstream fmap("map.txt");

char str[1200]; // That's a big array but it needs to go through the file

for(i = 0; i < 15; i++)
{
fmap >> str;
for(j = 0; j < 80; j++)
{
switch((int)str[j])
{
case '-':
map[j][i] = GRASS;
break;
case '~':
map[j][i] = WATER;
break;
}
}
}

fmap.close();

// Now show the map
char tile = (char)176;
HANDLE std = GetStdHandle(STD_OUTPUT_HANDLE);

for (i = 0; i < 15; i++)
{
for (j = 0; j < 80; j++)
{
switch(map[j][i])
{
case GRASS:
gotoxy(j,i);
SetConsoleTextAttribute(std, F_D_GREEN | B_GREEN); // For simplicty's sake, pretend I have all the colors pre-defined as these
printf("%c", tile);
SetConsoleTextAttribute(std, F_GREY | 0); // I like changing the text back to grey
break;
case WATER:
gotoxy(j,i);
SetConsoleTextAttribute(std, F_BLUE | F_GREEN | B_BLUE);
printf("%c", tile);
SetConsoleTextAttribute(std, F_GREY | 0);
break;
}
}
printf("\n");
}
}

That's some basic code. Hope that helps a little.

Brendan