Public Group

This topic is 4507 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everyone. I have been playing around with arrays and made my own little map demos which have been pretty nice, but all the maps have been hardcoded so I have decided to try and load a map. I have a map-file("Map.txt") that looks like this: 111101111 Ok ill help you out its just a simple map represented by tiles. 1 is a wall and o is a picture of a tree. So heres what the map should be like in the game: 111 101 111 So could you please help me out and tell me if I am doing anything wrong whilst loading my map:
void LoadMap()
{
char Map[9];

ifstream MapFile("Map.txt");
MapFile >> Map;
MapFile.close();
}


So is that code above the same as doing this: char Map[9] = {1,1,1,1,0,1,1,1,1}; Also I have used char Map[3][3] in my demos aswell and find both easy to work with. What am I better off using? P.S If you need more code just say and i will post it up.

##### Share on other sites
hey, if it works, it works!

You are going to have some pitfalls in the future though. Eventualy, you will want to load maps of other than 3x3, so you are going to need to learn how to parse a file, and dynamicaly allocate memory. But this is a good start! Oh, almost forgot, I haven't used c++'s file IO, but I think you are still going to have to check for the newline character. Not sure though, it's been a while.

##### Share on other sites
You should use an int array. Consider if you have more than 10 different numbers for the map. Say you wanted the map array to contain 11, 1, and 0. the file would be 1110, wich will be interpreted as 3 ones and a zero. but if laoded into an int array you could save the file like this: 11 1 0 using white space to seperate the numbers.

So to basicaly change the char[] to int[] and space out your numbers int the text file. But if you never plan to have more than 10 different possible numbers your way will work ok, I guess.

##### Share on other sites
Hey thanks alot you guys and especially you ForeverNoobie thats exactly what i needed. I changed my array from char to int then changed my map file and everything works perfect. Thanks alot.

##### Share on other sites
Characters work fine if there are enough of them for your "tile types". Instead of using the digit characters, you might use symbols etc. so that the file looks like ASCII-art of your desired map. Of course, you have some translation work to do regardless. :)

Here's one way to read such a file into a dynamically allocated array (while making sure the map is rectangular by padding rows, and using 1-dimensional storage):

// not tested!#include <vector>#include <string>#include <algorithm>#include <iterator>class Map {  std::vector<char> storage;  int rowLength;  public:  Map(istream& source, char background = ' ');};Map::Map(istream& source, char background = ' ') : rowLength(0) {  std::vector<std::string> readBuffer;  std::string current;  while (std::getline(source, current)) {     readBuffer.push_back(current);    rowLength = std::max(current.length(), rowLength);  }  storage.resize(rowLength * readBuffer.length(), background);  std::vector<char>::iterator loc = storage.begin();  for (std::vector<std::string>::iterator it = readBuffer.begin();       it != readBuffer.end(); ++it) {    std::copy(it->begin(), it->end(), loc);    std::advance(loc, rowLength);  }}

1. 1
2. 2
JoeJ
20
3. 3
4. 4
frob
12
5. 5

• 13
• 18
• 13
• 20
• 13
• ### Forum Statistics

• Total Topics
632194
• Total Posts
3004681

×