Map generation-reading from a file

Started by
16 comments, last by 15Peter20 16 years, 1 month ago
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.
Advertisement
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
Quote:And why MapArray[20][20] this? it is a 21x21 array with 0 counted :p.

Requote from previous post :P.
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();// ...}
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
Grats the problem is solved :P.

And i think i am a bit too far away to take a beer XD. (Lives in Sweden)
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.
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!

This topic is closed to new replies.

Advertisement