Reading in from a file to a private class member
Greetings! I am working on the Game of Life C++ project and I am struggling with bringing in data from my file to create a new board for the game.
Here is the function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
bool GameOfLife::makeBoardFile()
{
std::ifstream myfile;
myfile.open ("GOL.txt");
if (!myfile) return true;
for (int i = 0; i < MAX_Board; ++i)
{
for (int j = 0; j < MAX_Board; ++j)
{
myfile >> gameBoard[i][j].setState(true);
}
}
myfile.close();
return false;
}
|
Here are the classes:
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
|
namespace GOL
{
using std::string;
using std::ostream;
class Cell {
private:
bool state;
char face;
public:
static const char alive = '*';
static const char dead = '_';
Cell(bool state);
Cell();
bool getState() const;
void setState(bool newState);
friend ostream &operator << (ostream &out, const Cell &cell);
};
class GameOfLife
{
public:
static const unsigned int MAX_Board = 30;
GameOfLife();
GameOfLife(int, int);
~GameOfLife(){};
void setGameState(bool state);
bool getGameState();
//void printCurrentBoard(int game_Width, int game_Height);
void validateCells(Cell gameBoard[][MAX_Board], Cell nextLife[][MAX_Board]);
bool makeBoardFile();
void randomizeBoard();
void runGame(unsigned int, GameOfLife&);
int getIterations(unsigned int& num);
void Menu();
void printBoard(std::ostream &);
friend ostream &operator << (ostream &out, const GameOfLife &game);
private:
Cell gameBoard[MAX_Board][MAX_Board];
Cell nextLife[MAX_Board][MAX_Board];
};
}
|
the setState function just looks like this
1 2 3
|
void Cell::setState(bool newState){
state = newState;
}
|
Topic archived. No new replies allowed.