1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
#include "Map_Segment.h"
#include "Map_Tile.h"
#include <iostream>
#include <stdlib.h>
#include <fstream>
using std::ifstream;
using std::string;
using std::cout;
using std::cin;
using std::endl;
using std::cerr;
Map_Segment::Map_Segment(const string roomName)
{
get_valid_exits(roomName);
get_area_description(roomName);
}
void Map_Segment::get_valid_exits(const string& areaName)
{
static const string Map_Data = "Map_Data/";
static const string Valid_Exits = "/Valid_Exits.txt";
static const string DocumentName = Map_Data + areaName + Valid_Exits;
ifstream document(DocumentName.c_str() );
if ( !document.is_open() )
{
cerr << "Can't find " << DocumentName << endl;
throw ifstream::failure( "File could not be opened." );
}
else
{
bool north, east, south, west;
for ( int x = 0; x < MAP_SIZE; x++ )
{
for ( int y = 0; y < MAP_SIZE; y++ )
{
if ( document >> north >> east >> south >> west )
{
game_board[x][y].set_exits( north, east, south, west );
}
else
{
throw ifstream::failure( "Failed to read data from file." );
}
}
}
}
document.close();
}
void Map_Segment::get_exit_descriptions(const string& areaName)
{
static const string Map_Data = "Map_Data/";
static const string Exit_Description = "/Exit_Descriptions.txt";
static const string DocumentName = Map_Data + areaName + Exit_Description;
ifstream document( DocumentName.c_str() );
if ( !document.is_open() )
{
cerr << "Can't find " << DocumentName << endl;
throw ifstream::failure( "File could not be opened." );
}
else
{
string north, east, south, west;
for ( int x = 0; x < MAP_SIZE; x++ )
{
for ( int y = 0; y < MAP_SIZE; y++ )
{
if ( getline(document,north) && getline(document,east) && getline(document,south) && getline(document,west) )
{
game_board[x][y].set_exit_descriptions( north, east, south, west );
}
else
{
throw ifstream::failure( "Failed to read data from file." );
}
}
}
}
}
void Map_Segment::get_area_description(const string& areaName)
{
static const string Map_Data = "Map_Data/";
static const string Area_Descriptions = "/Area_Descriptions.txt";
static const string DocumentName = Map_Data + areaName + Area_Descriptions;
ifstream document( DocumentName.c_str() );
if ( !document.is_open() )
{
cerr << "Can't find " << DocumentName << endl;
throw ifstream::failure( "File could not be opened." );
}
else
{
string areaNameDescription;
for ( int x = 0; x < MAP_SIZE; x++ )
{
for ( int y = 0; y < MAP_SIZE; y++ )
{
if ( getline(document, areaNameDescription) )
{
game_board[x][y].set_area_description( areaNameDescription );
}
else
{
throw ifstream::failure( "Failed to read data from file." );
}
}
}
}
document.close();
}
|