# Map generation-reading from a file

## Recommended Posts

hi guys, im new to GDC after my friend highly recommended u guys for my help. im at uni studying comp games dev, and im currently on an assingment implementing AI on pathfinding. now the juicy bit. im currently stuck on a section where i have to read a file which contains 0,1,2 and 3's which make up a small 20x20 map. 0 are impassable walls, 1 is walkable ground, 2 are trees and the 3's are water terrain. the problem i ahve been having is that i am trying to read this file and ut all the numbers into a 2D char array in my front end update section, and then using those stored numbers in the array, create cubes that represent the corresponding numbers. however i have had problems jsut trying to read the file, and after hours or staring at this code, trying all sorts of diferent ways around it and even my friend coming over to have a crack we cant see the problem, at first i thought it was the original map read but i dont think it is. i have posted my code here for it all sry if its a mess, but its not quite complete can any1 see where im going wrong at all?? any help would be wonderous!!!!!!!!! x
// MapReading.cpp: A program using the TL-Engine

#include &lt;TL-Engine.h&gt;	// TL-Engine include file and namespace
using namespace tle;
#include &lt;fstream&gt; // include the file stream functions

//Global variables
I3DEngine* myEngine;
ICamera* myCamera;
IMesh* cubeMesh;
IModel* cube;
IFont* myFont;

char MapArray[20][20];
ifstream InFile;
#define MEDIA_FOLDER = "Media\\";

void FrontEndSetup();
void FrontEndUpdate();
void FrontEndShutdown();

void GameSetup();
void GameUpdate();
void GameShutdown();

void main()
{
// Create a 3D engine (using Irrlicht engine here) and open a window for it
myEngine = New3DEngine( kIrrlicht );
myEngine-&gt;StartWindowed();

// Add default folder for meshes and other media
myEngine-&gt;AddMediaFolder( "C:\\Documents and Settings\\Sean P Jones\\My Documents\\TL-Engine\\Media" );

FrontEndSetup();
// The front-end loop, repeat until stopped
while (myEngine-&gt;IsRunning())
{
FrontEndUpdate();
if(myEngine-&gt;KeyHit(Key_S))
{
break;
}
}
FrontEndShutdown();

GameSetup();
// The main game loop, repeat untill exited
while (myEngine-&gt;IsRunning())
{
GameUpdate();
if(myEngine-&gt;KeyHit(Key_Escape))
{
break;
}
}
GameShutdown();
}

void FrontEndSetup()
{
cube = cubeMesh-&gt;CreateModel(0,0,50);
myCamera = myEngine-&gt;CreateCamera(kManual);

}

void FrontEndUpdate()
{
// Draw the scene
myEngine-&gt;DrawScene();
cube-&gt;RotateX(0.5);
myFont-&gt;Draw("Press 'L' to load a map", 0, 0, kWhite);
myFont-&gt;Draw("Press 'S' to start", 0, 20, kWhite);
myFont-&gt;Draw("Map Generation!!", 225, 200, kRed);
if(myEngine-&gt;KeyHit(Key_L))
{
InFile.open("C:\\Documents and Settings\\Sean P Jones\\My Documents\\1Work\\Year2\\2301-GamesDev1\\MapReading\\Media\\map1.txt");
for(int x=0;x&lt;20;x++)
{
for(int y=0;y&lt;20;y++)
{
InFile&gt;&gt; MapArray[x][y];
}
}
InFile.close();
}
if(InFile.fail())
{
myFont-&gt;Draw("MAP FAILED",0,60,kBlack);
}
}

void FrontEndShutdown()
{
myEngine-&gt;RemoveFont(myFont);
cubeMesh-&gt;RemoveModel(cube);
myEngine-&gt;RemoveMesh(cubeMesh);
}

void GameSetup()
{

for (int i=0;i&lt;20;i++)
{
for (int j=0;j&lt;20;j++)
{
if(MapArray[0][0]==0)
{
cube = cubeMesh-&gt;CreateModel(i,j);
cube-&gt;Scale(0.05);
}
}
}
}

void GameUpdate()
{
// Draw the scene
myEngine-&gt;DrawScene();
myFont-&gt;Draw("Escape to Quit", 0, 0, kWhite);

}

void GameShutdown()
{
myEngine-&gt;RemoveFont(myFont);
// Delete the 3D engine now we are finished with it
myEngine-&gt;Delete();
}


[Edited by - 15Peter20 on February 20, 2008 5:29:18 PM]

##### Share on other sites
Original post by 15Peter20
char MapArray[20][20];

Don't do that, it is inefficent.

Use a STL container instead to loop through as a map. (Vector or List, your choice.)

##### Share on other sites
Hi, and welcome to the forums :)

You can get your code to appear in a 'source code' box by enclosing it in [source] tags (just click the 'edit' button, and you can add these to your original post). In most cases that will preserve the code formatting and make it easier for us to read.

Also, once you've been around the forums a bit, you'll find that many of us appreciate (and value) clear communication skills (and that, in fact, these are important skills for a programmer to have in general). Clear, correct writing (e.g. correct capitalization and punctuation, and avoidance of abbreviated words such as 'any1') will make your posts easier to read and follow. That, in turn, will make more folks likely to read them, which means you'll have a better chance of getting the help you need.

Finally, there are some rules on the forum regarding helping with (and posting about) homework problems. I would go ahead and clean up your post though, since even if we can't give you the answer outright, we might be able at least to point you in the right direction.

##### Share on other sites
o right, i didnt think of that, well i am jsut literally going through a tutorial now that is trying to teach me to use the STL, but its only covered deque at this time, but i think it goes onto vectors and list's, lists being the one im intrested in.

if u can give me a hand with a starting place i will be very appreciative. x

##### Share on other sites
http://www.cppreference.com/index.html
http://www.cplusplus.com/doc/

Practically all you need to know about STL classes.

##### Share on other sites
Quote:
 Original post by ChJeesOriginal post by 15Peter20*** Source Snippet Removed ***Don't do that, it is inefficent.Use a STL container instead to loop through as a map. (Vector or List, your choice.)
What do you mean by 'inefficient'?

You'll often see folks on this forum recommend alternatives to raw arrays, such as std::vector (which you alluded to above) or boost::multi_array. However, improved run-time efficiency (if that's what you're referring to) is usually not high on the list of reasons for recommending these alternatives.

Now, if you mean something else by 'efficient' - for example, using your development time wisely - then yes, using raw arrays is indeed 'inefficient' :)

##### Share on other sites
sorry about my bad typing skills, I just think of the words, and my hands do the rest :)but i will make an effort.

didnt know about the source tags, again sorry, and i'll go voer the forum rules etc as soon as i can.

##### Share on other sites
Quote:
 Original post by jykNow, if you mean something else by 'efficient' - for example, using your development time wisely - then yes, using raw arrays is indeed 'inefficient' :)

Mainly that, and i have a expandable array if i want to :P.

But i am not in the mood of argumenting and don't want to, so i will help the OP instead.

##### Share on other sites
Quote:
 Original post by ChJeesBut i am not in the mood of argumenting and don't want to, so i will help the OP instead.
I'm not arguing :) But, I also have to point out that, just like you, I'm trying to help the OP (in this forum especially, 'helping the OP' often takes the form of questioning or attempting to clarify the posts of other respondents).

[Edited by - jyk on February 20, 2008 7:42:47 PM]

##### Share on other sites
thanks again for the help people,

but can any-one spot the problem I was having with my code before? it has really been bugging me for me hours driving me crazy just can not find why!?

and as a reply to your ineeficent coding before, i know it is but it what i know at time of coding, and i intend to apadt it in future but right now i jsut want the crazy thing to work lol :)

##### Share on other sites
This code may be your savior if you know how to use it. :3

int loop = 0;for (int x = 0; x < 19; x++ ){	for (int y = 0; y < 19; y++ )	{		loop++;	}}

And why MapArray[20][20] this? it is a 21x21 array with 0 counted :p.

##### Share on other sites
yes i do use nested for loops almost every day xD

but jsut two querries,
1. would it not need to be <20 as the array goes up to 19, and if it <19 it will do nothing to the final two elements in the array[][].
2. i'm not sure what i would do with a loop counter right now, but ill give a thought about it

cheers anyway. 15Peter20

##### Share on other sites
Quote:
 And why MapArray[20][20] this? it is a 21x21 array with 0 counted :p.

Requote from previous post :P.

##### Share on other sites
You haven't posted the problem you are having. You have posted the code - which is an important step. But what is the problem?

You should describe what is happening, versus what you expect to happen.

The only thing that I can see is that you are reading the data as characters, so the elements of your array will be '0','1','2','3'. But in GameSetup() I see you are comparing the characters against the integer literal 0 "if(MapArray[0][0]==0)". To convert the incoming characters from characters representing numbers to the number itself, subtract '0' from the character:
// define a constant like this elsewhere so it is easy to change (or replace with a command line parameter)const char *dataFile = "C:\\Documents and Settings\\Sean P Jones\\My Documents\\1Work\\Year2\\2301-GamesDev1\\MapReading\\Media\\map1.txt";GameSetup(){// ...// dont use a global if you don't have to// make this localstd::ifstream InFile(dataFile);		for(int x = 0 ; x < 20 ; x++){	for(int y = 0 ; y < 20 ; y++)	{                char c;		InFile >> c;                MapArray[x][y] = c - '0';	}}// std::fstreams close themselves// InFile.close();// ...}

##### Share on other sites
omg people i feel like such a tool!
1. jees your right, declaring my array[20][20] will make a 21x21 matrix lol xD
2.rip-off omg, u made my brain just click, firstly yeah i forgot to add my problem i was having xD and secondly, i was comparing character to int, why the hell didnt i see that!!!!!!!!!!!!!

i have changed my code now to compare the array to chars and it works... for now xD i feel like a tard burglar ^^

u know i been going through that code here and there for about 11 hours xD

if iever see u out ill buy u a beer ;)

15Peter20 x

##### Share on other sites
Grats the problem is solved :P.

And i think i am a bit too far away to take a beer XD. (Lives in Sweden)

##### Share on other sites
array[20][20] is of size 20x20, not 21x21. Both your array declaration and iteration were correct in your original code.

It is the indicies that go from 0..19, which is 20 discrete values.

##### Share on other sites
that was annoying me all day :) u dont nwo how happy i am :)

now i jsut got to implement it into 3D, shouldnt be too hard and implement some path finding algorythems by friday 9am and ill get a grade lol dam why do i always leave thigns to the last moment xD

mucho lurv!

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628278
• Total Posts
2981785

• 10
• 11
• 17
• 13
• 9