loading breakout leves from a file

Started by
7 comments, last by Zahlman 15 years, 10 months ago
So I am trying to load a level from a file. Below is how I load a map right now.

void LoadBlockMap()
{
int i = 1;
  for(int j = 1; j < 7; ++j)
  {
    for(int k = 1; k < 12; ++k )
    {
        sMap.alive = true;           //This tells the draw function to show that block
        sMap.y = j * 29;            //this is the y coord
       sMap.x = k * 50;             //this is the x coord
       sBlock.color = (rand()%6)+1; // this is the color of the block
      ++i;
    }
  }
}

The issue is I dont thinck it is the best way to load the map if I am going to be using a file. I need to be able to put a single number in the file to repersent the color (which is one through 6) and were it is placed in the file repersents the x and y of were it would be on the screen example: 0,0,0,1,1,0,0,0,1,0 0,0,1,1,1,1,0,1,1,0 0,1,1,1,1,1,1,1,1,0 1,1,1,1,1,1,1,1,1,1 0,0,0,0,0,0,0,0,0,0 so the zero is no block and 1 is a black block. what would be differnt ways for loading this from a file? once I fiure this out I want to beable to create a map editor so I can place the blocks with the mouse then save it and have it wright the levels for me. Thanks for reading this and hope some one can guide me in the right direction.
Advertisement
The way you're thinking of is quite ok, except I'd drop the commas and put the dimensions of the map on the first line, like this:
10 5
0001100010
0011110110
0111111110
1111111111
0000000000
Good thing about this is that you don't really need an editor, because you can just edit your maps in notepad. The dimensions on the first line can act as a form of error checking, so you don't accidentally load files that are not levels. They also make reading the file a bit easier, since you'll know in advance how many lines and columns you're supposed to read.
Or you could even store your 1s and 0s in a binary file! That particular level could be stored in 8 bytes, including space for the dimensions! :-D
Well I have the strangest issue now.

The for loop is not working they way it is supose to.

inFile.open("level.txt");int block[67];int i=0;while(!inFile.eof()){  inFile>>block;i++;}int l = 1;  for(int j = 1; j < 7; ++j)  {    for(int k = 1; k < 12; ++k )    {         if(block[l] == 0)           {                  sMap[l].alive = false;                           sMap[l].y = 0;                     sMap[l].x = 0;                    sBlock.color[l] = block[l];           }                  else if(block[l] > 0)          {               sMap[l].alive = true;         sMap[l].y = j * 29;                   sMap[l].x = k * 50;                    sBlock.color[l] = block[l];          }       ++l;    }  }


why does this not return what is in the map file?

It only return garbage.
The txt file has persisley the right amount of numbers in it for the array.
I think this part is probably not working as you expect:
while(!inFile.eof()){  inFile>>block;i++;}


The >> operator will read everything up to the next delimiter. If your format is 0011110110, it will try to read the whole thing as a single integer. If you're using >>, you'll neet to keep your delimiters between the numbers.

Can you check your block[] array to see if it's populated correctly? If it is, what makes you think your output is incorrect? can you show us?
Assuming you made the changes SnotBob mentioned, the first integer in the file is not zero, it's 1100010, as far as the formatted input stream operators are concerned. Either read character by character, or separate them by spaces so you can read them as integers.
this is from the input file
6 6 5 5 6 5 1 1 5 3 6 6 2 4 2 6 2 3 4 1 4 1 3 4 5 5 4 3 3 6 6 1 6 1 4 5 6 2 2 1 6 4 3 4 4 3 4 2 6 5 6 3 5 4 4 2 0 4 2 5 5 6 3 1 1 0

this is fron the output file which should be exactley the same
but it is only empty

Any clues why?
Try adding a check to see if the file is actually opened. Something like
inFile.open("level.txt");if( !inFile ) {    // Do your error handling}
This assumes that inFile is a std::ifstream. If it's not change the if to an appropriate error check.
0) Array indices start from zero. You want to treat them this way. You do not want to complicate your math all over the place and waste an element out of every array. You want to just learn to count starting at zero. Everything works out much more easily this way. Notice that the first time through, you start by storing file elements into element 0 onward - the natural way - but then you try to read them from element 1 onward. The first read is skipped, and the rest are used for the wrong blocks.

1) Doing arithmetic is the computer's job, not yours. Give names to important constants, and use them in formulas, instead of working out the required amounts of stuff yourself.

2) Use the constructor of streams to open them, in normal cases. When you want to read a whole file, use the read operation itself for a loop condition, because checking for .eof() this way *does not do what you want*. But when you're reading a fixed amount into an array, checking the array bounds is more important.

3) In C++, variables used to count the for-loop don't live past the loop, so you can reuse the name for a new variable.

4) You don't need a temporary array to read the file data. Read directly into the sBlock colours, and then check those in the loop.

5) Don't test else-if a colour is greater than zero. What are you going to do if it's less than zero? Currently, nothing. Is that what you want? I think it makes more sense to complain about invalid input. And you can do that while reading the data.

ifstream inFile("level.txt");int HEIGHT = 6;int WIDTH = 11;int block[WIDTH * HEIGHT];for (int i = 0; i < WIDTH * HEIGHT; ++i) {  if (!inFile >> sBlock.color || sBlock.color < 0) { /* throw an exception here */ }}int i = 0;for (int j = 0; j < HEIGHT; ++j) {  for (int k = 0; k < WIDTH; ++k) {    if (sBlock.color == 0) {      sMap.alive = false;               sMap.y = 0;         sMap.x = 0;      } else {             sMap.alive = true;       sMap.y = j * 29;                  sMap.x = k * 50;                 }     ++l;  }}

This topic is closed to new replies.

Advertisement