Board.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#ifndef BOARD_H
#define BOARD_H
class Board
{
public:
Board (){};
int GetRowCount () {return m_RowCount;}
int GetColCount () {return m_ColCount;}
char GetElem (const int row, const int col);
void SetElem (const int row, const int col, const char elem);
void CheckRow (const int row);
void CheckCol (const int col);
void Load (const char *filename);
void Display ();
private:
static const int m_RowCount = 21;
static const int m_ColCount = 69;
char m_Fields[m_RowCount][m_ColCount];
};
#endif
|
Board.cpp
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
|
#include "Board.h"
#include <iostream>
#include <string>
#include <stdexcept>
#include <fstream>
using namespace std;
char Board::GetElem (const int row, const int col)
{
CheckCol (col);
CheckRow (row);
return m_Fields[row][col];
}
void Board::SetElem (const int row, const int col, const char elem)
{
CheckCol (col);
CheckRow (row);
m_Fields[row][col] = elem;
}
void Board::Load (const char *filename)
{
ifstream src (filename);
if (!src)
throw runtime_error ("Board::Load - Unable to load file");
int line = 0;
string input;
while (src && line < m_RowCount - 1)
{
getline (src, input);
if (input.size () > m_ColCount)
throw runtime_error ("Board::Load - Line > 69 chars ");
for (size_t i = 0; i < input.size (); i++)
{
SetElem (line, i, input[i]);
}
line++;
}
}
void Board::Display ()
{
for (int row = 0; row < m_RowCount - 1; row++)
{
for (int col = 0; col < m_ColCount - 1; col++)
{
cout << m_Fields[row][col];
}
cout << '\n';
}
}
void Board::CheckRow (const int row)
{
if (row < 0 || row > m_RowCount - 1)
throw out_of_range ("Board::CheckRow - Row out of range");
}
void Board::CheckCol (const int col)
{
if (col < 0 || col > m_ColCount - 1)
throw out_of_range ("CheckCol - Row out of range");
}
|
Main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <iostream>
#include <string>
#include "Board.h"
using namespace std;
int main ()
{
Board board;
try
{
board.Load ("Map.txt");
board.Display ();
}
catch (exception& ex)
{
cerr << "\a" << ex.what () << "\n\n";
}
system ("pause");
return 0;
}
|
Map.txt
####################################################################
#....................................................___...........#
#.....................................................|$|.........M#
#........@............................................|............#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
####################################################################