TextAdventure Game Constructor Confusion

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;


I looked at the tutorial page, but I could not figure out what i was missing:
http://www.cplusplus.com/doc/tutorial/classes/

Any help is appreciated.

Thanks,

Hirokachi
Last edited on
The non-trivial constructor would be invoked. http://coliru.stacked-crooked.com/a/923733714a71909a
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.

Please give me some suggestions if you have time.

Thanks,

-Hirokachi
Last edited on
This goes into an infinite loop (when move == "north", directionalNumber would be set to 1):
1
2
3
4
5
6
7
8
		do {
			std::getline (std::cin, move);
			if (move.compare("north") == 0)
				directionalNumber = 1;
			else {
				std::cout << "You didn't type north; Please try again.";
			}
		}while (move != "north" || directionalNumber != 0);


Perhaps you intended to write something like this?
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
	/*
	* 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 ; // *** modified

			else {
				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.
Ok thanks.

-Hirokachi
Topic archived. No new replies allowed.