I am trying to create a simple map game and my map is based on a grid
of intigers. My map for some reason is not working well. I am not
sure what is wrong. I wrote a simple check program but outut dosn't
match my map of numbers.
Here is the object that holds the grid:
#include<fstream>
using namespace std;
#ifndef BOARD_H
#define BOARD_H
class board{
static const int xlen = 30;
static const int ylen = 25;
int b[ylen][xlen];
void create();
ifstream& cfill(ifstream& in, int& code); /*Reads map from file */
void fill();
public:
board();
int GetSpace(const int x, const int y){return b[y][x];}
int GetSizeX(){return xlen;}
int GetSizeY(){return ylen;}
};
#endif
The functions:
#include "board.h"
board::board(){
create();
}
void board::create(){
fill();
}
ifstream& board::cfill(ifstream& in, int& code){
in>>code;
return in;
}
void board::fill(){
int terrain;
ifstream f ("map.txt");
for(int l2 = 0; l2 != ylen; l2++){
for(int l1 = 0; l1 != xlen; l1++){
if(cfill(f, terrain)){
b[l2][l1] = terrain;
}
}
}
}
Finally the test program:
#include<iostream>
#include<fstream>
#include"board.h"
board b;
int main(){
for(int y = 0; y != b.GetSizeY(); y++){
for(int x = 0; x != b.GetSizeX(); x++){
std::cout<<b.GetSpace(y,x);
}
std::cout<<endl;
}
system("pause");
return 0;
}
My Data file:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
1 1 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1
1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1
1 0 0 4 0 0 1 0 0 1 0 1 0 2 2 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 1 0 0 0 0 0 0 2 2 2 0 0 0 0 1 0 0 0 0 1
1 0 1 5 0 0 1 0 0 0 0 1 0 2 2 2 2 0 2 0 0 0 0 0 0 1
1 0 0 6 0 0 1 0 0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