Everytime, in my TextAdventure program (that can be found on github here; https://github.com/Hirokachi/TextAdventure/tree/RandomMapEnhancement ) that I write a constructor it doesn't seem to run the constructor code even though I write either MyClass theClass; or MyClass theClass = new MyClass();.
It's weird I thought that maybe the second one would surely run the constructor code. In the constructor code read from a file and then i have it append to the classes variable that is supposed to store all rooms.
The following is the code that i am using for my constructor in the Gamefield.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class GameField {
std::list<room> listOfRooms;
std::list<roomConnections> connections;
public:
GameField () {
std::string line;
std::ifstream myfile("roomsfile.txt");
if (myfile.is_open()) {
while (std::getline(myfile, line)) {
std::cout << line << '\n';
}
myfile.close();
}
else
std::cout << "Unable to open file.";
}
Let me know if you see anything that is wrong with my code or anything.
Right now, the code is only printing out the contents of that file, that is intentional.
The following code is where I call the Class in the TextAdventure.cpp:
1 2 3 4 5 6 7 8 9 10
#include "GameField.h"
/**
* Where the program starts.
*/
int main() {
//The first answer to start the game.
std::string randomize = "non";
std::string mapSize = "non";
GameField gamemap;
Do you think that i should put that into the gamefield constructor method instead; just make the randomly pick the set of lines in the file to generate the number of rooms needed to make the game world?
As in the following pseudo code:
1 2 3 4 5 6 7 8 9 10 11 12 13
void generateGameMap (int totalRooms, bool randomize) {
//Load room description and name/id from file into the listOfRooms.
//for player input:
// for the total number of Rooms:
// Generate the connections between rooms
// Generate start and end positions and set in the room
// run test verify if connections create a route to start to end
// store as the current connected gameMap
// or load a pregenerated map of said size and store
}
I know the constructor there is non-trival, but i am wondering what i should do differently.
/*
* Function/method directionalMove:
* takes input for one of the possible exits for room & validates &
* moves from one room to another when valid.
*/
void directionalMove () {
//Define the variable storeing player choice
std::string move;
int directionalNumber = 0 ;
//printRoomDesc (playerPosition.uniqueID);
//Tell the player they can move now.
std::cout << "Please enter a direction. In this case 1 = north, 2 = south, etc.\n ";
//get direction from player.
// std::getline (std::cin, move); // *** removed (there is a getline within the do-while)
//convert direction to number because binary '==' operation for enum doesn't work for strings.
do {
std::getline (std::cin, move);
// if (move.compare("north") == 0)
// directionalNumber = 1;
if( move == "north" ) directionalNumber = commandList::north ; // *** modifiedelse {
std::cout << "You didn't type north; Please try again.";
}
} // while (move != "north" || directionalNumber != 0);
while ( move != "north" || directionalNumber != commandList::north ); // *** modified
//move.erase; (move.find_last_not_of (" \t") + +1);
//istringstream ss (move);
//Check if command is valid
if (commandList::north == directionalNumber) {
std::cout << "You did try to move North which means that I program correctly. Hurray for me!!!\n";
}
else {
std::cout << "You didn't try to move north you silly player. Boo, I don't know how to use enums to hold commands.\n";
}
//Move player into room from that direction.
//Print out new Room Description.
}
Try running it with these changes, and see if it is what you wanted the program to do.