Advertisement

Text Based Game Attempt c++

Started by December 29, 2014 06:00 PM
8 comments, last by BeerNutts 10 years, 1 month ago

Hi there first of all let me wish you all a happy new year!!!

So, i tried to make a Text based adventure game, and I realized I had to learn OOP, and I tried to do things the best way, so i ended up getting a little confused with OOP. i came up with 5files (main.cpp / game.cpp/ inventory.cpp/ player.cpp and map.cpp). but i can´t get where do i need to create objects and i came up a little confused, the game is running, the only thing is when it needs to return to menu i need to reset all variables, in best case destroy objects and re create as soon as player press play, but can´t figure how to do that.

main.cpp


#include<iostream>
#include<string>
#include<fstream>

#include"map.h"
#include"player.h"
#include"inventory.h"
#include"game.h"

void loadMap();
void getPlayerName();
void updateGame();
void displayGame();


std::string inventory();
std::string iventoryDeleteItem();

void handleHallEvents();
void handleKitchenEvents();
void handleLivingRoomEvents();
void handleLibraryEvents();
void handleHallWayEvents();
void handleBathroomEvents();
void handleHall1Events();
void handleBedroom1Events();
void handleBedroom2Events();
void handleBathroom1Events();
void handleCongratsEvents();

void handleHallFirstChoice();
void handleKitchenFirstChoice();
void handleLivingRoomFirstChoice();
void handleBathroomFirstChoice();
void handleBedroom2FirstChoice();
void handleBedroomFirstChoice();

std::string addInventoryItem(std::string);


enum gameStates{Menu,Running,Exit};
std::string iventoryItems[5] = { "empty", "empty", "empty", "empty", "empty" };


gameStates gameState;
Map isMap("Hall","0");
Player isPlayer;
Inventory isInventory;
Game isGame;

int main(){
	
	gameState = Menu;

	while (gameState != Exit)
	{
		switch (gameState)
		{
			case Menu:
			{
				int getMenuChoice = isGame.menuLogic();
				if (getMenuChoice == 1)
				{
					isGame.clearScreen();
					getPlayerName();
					gameState = Running;
				}else{
					gameState = Exit;
				}
				break;
			}
			case Running:
				updateGame();
				loadMap();
				displayGame();
				break;
			case Exit:
				break;
		}
	}

	return 0;
}

void getPlayerName(){
	std::string playerInput;
	std::cout << "Enter your name: ";
	std::cin >> playerInput;

	isPlayer.setName(playerInput);
}

void loadMap(){

	std::ifstream map;
	//std::cout << isMap.currentRoom;
	map.open(isMap.getCurrentRoom() + ".txt");

	if (map.fail()){
		std::cout << "Erro\n";
		system("pause");
		exit(1);
	}

	std::string mapData;

	while (getline(map, mapData)){
		std::cout << mapData <<"\n";
	}

	map.close();

}

void updateGame(){
	isGame.clearScreen();
}

void displayGame(){

	if (isMap.getCurrentRoom() == "Hall"){
		handleHallEvents();
	}
	else if (isMap.getCurrentRoom() == "Kitchen"){
		handleKitchenEvents();
	}
	else if (isMap.getCurrentRoom() == "LivingRoom"){
		handleLivingRoomEvents();
	}
	else if (isMap.getCurrentRoom() == "Library"){
		handleLibraryEvents();
	}
	else if (isMap.getCurrentRoom() == "HallWay"){
		handleHallWayEvents();
	}
	else if (isMap.getCurrentRoom() == "Bathroom"){
		handleBathroomEvents();
	}
	else if (isMap.getCurrentRoom() == "Hall1"){
		handleHall1Events();
	}
	else if (isMap.getCurrentRoom() == "Bedroom1"){
		handleBedroom1Events();
	}
	else if (isMap.getCurrentRoom() == "Bedroom2"){
		handleBedroom2Events();
	}
	else if (isMap.getCurrentRoom() == "Bathroom1"){
		handleBathroom1Events();
	}
	else if (isMap.getCurrentRoom() == "Congrats"){
		handleCongratsEvents();
	}
}

//Hall handle events
void handleHallEvents(){
	std::string playerInput;
	//Check if is first visit to Hall so we can change text to fit
	if (isMap.getHallFirstVisit()){
		handleHallFirstChoice();
	}else{
		std::cout << "keep looking for your dog\n";
		std::cout << "[1] -> go to Kitchen\n";
		std::cout << "[2] -> go to Hallway\n";
		std::cout << "[3] -> go to Livingroom\n";
		std::cout << "[4] -> go Outside\n";
		std::cout << "[i] -> Open Invetory\n";
		retry:
		std::cin >> playerInput;
		
		isPlayer.setplayerDecision(playerInput);

		if (isPlayer.getPlayerDecision() == "1"){
			isMap.setLastRoom("Hall");
			isMap.setCurrentRoom("Kitchen");
		}
		else if (isPlayer.getPlayerDecision() == "2"){
			isMap.setLastRoom("Hall");
			isMap.setCurrentRoom("HallWay");
		}
		else if (isPlayer.getPlayerDecision() == "3"){
			isMap.setLastRoom("Hall");
			isMap.setCurrentRoom("LivingRoom");
		}
		else if (isPlayer.getPlayerDecision() == "4"){
			if (isInventory.getItemAsBeenUsed() == "mainKey"){
				isMap.setLastRoom("Hall");
				isMap.setCurrentRoom("Congrats");
			}
			else{
				std::cout << "This door is locked , you need the mainKey to open it ...\n";
				goto retry;
			}
		}
		else if (isPlayer.getPlayerDecision() == "i"){
			isPlayer.setplayerDecision("");
			isInventory.setItemAsBeenUsed(inventory());
		}
		else{
			std::cout << "That is not an option!\n";
			goto retry;
		}
	}
	
}

void handleHallFirstChoice(){
	std::string playerInput;

	std::cout << "Hi " << isPlayer.getName() << " you come up in this house in order to look for your dog, as you enter the door locked...Find your dog and get out of this place!\n";
	std::cout << "[1] -> go to Kitchen\n";
	std::cout << "[2] -> go to Hallway\n";
	std::cout << "[3] -> go to Livingroom\n";
	std::cout << "[4] -> go Outside\n";
	std::cout << "[i] -> Open Invetory\n";
	retry:
	std::cin >> playerInput;

	isPlayer.setplayerDecision(playerInput);
	//std::cout << playerDecision;

	if (isPlayer.getPlayerDecision() == "1"){
		isMap.setLastRoom("Hall");
		isMap.setCurrentRoom("Kitchen");
	}
	else if (isPlayer.getPlayerDecision() == "2"){
		isMap.setLastRoom("Hall");
		isMap.setCurrentRoom("HallWay");
	}
	else if (isPlayer.getPlayerDecision() == "3"){
		isMap.setLastRoom("Hall");
		isMap.setCurrentRoom("LivingRoom");
	}
	else if (isPlayer.getPlayerDecision() == "4"){
		if (isInventory.getItemAsBeenUsed() == "mainKey"){
			isMap.setLastRoom("Hall");
			isMap.setCurrentRoom("Congrats");
		}
		else{
			std::cout << "This door is locked , you need the mainKey to open it ...\n";
			goto retry;
		}
	}
	else if (isPlayer.getPlayerDecision() == "i"){
		isPlayer.setplayerDecision("");
		inventory();
	}
	else{
		std::cout << "That is not an option!\n";
		goto retry;
	}
	isMap.setHallFirstVisit(false);
}
//End Hall handle events

//Kitchen handle events
void handleKitchenEvents(){
	std::string playerInput;
	//Check if is first visit to kitchen so we can change text to fit
	if (isMap.getkitchenFirstVisit()){
		handleKitchenFirstChoice();
	}else{
		std::cout << "you already were here , i don´t think you will find anything here...\n";
		std::cout << "[1] -> go to Hall\n";
		std::cout << "[i] -> open Iventory\n";
		retry:
		std::cin >> playerInput;

		isPlayer.setplayerDecision(playerInput);

		if (isPlayer.getPlayerDecision() == "1"){
			isMap.setLastRoom("Kitchen");
			isMap.setCurrentRoom("Hall");
		}else if (isPlayer.getPlayerDecision() == "i"){
			isPlayer.setplayerDecision("");
			inventory();
		}else{
			std::cout << "That is not an option!\n";
			goto retry;
		}
	}
}

void handleKitchenFirstChoice(){
	std::string playerInput;
	std::cout << "You are in the kitchen and your dog is not here... the cabinets are a bit destroyed , seems to have something in it ...\n";
	std::cout << "[1] -> examine Cabinets\n";
	std::cout << "[2] -> go to Hall\n";
	std::cout << "[i] -> open Inventory\n";
	retry0:
	std::cin >> playerInput;

	isPlayer.setplayerDecision(playerInput);
	//std::cout << playerDecision;

	if (isPlayer.getPlayerDecision() == "1"){
			isGame.clearScreen();
			loadMap();
			isPlayer.setplayerDecision("");
			addInventoryItem("Ligth");
			std::cout << "You find yourself a flashlight ! check your inventory.\n";
			std::cout << "[1] -> go to Hall\n";
			std::cout << "[i] -> open Inventory\n";
			retry:
			std::cin >> playerInput;

			isPlayer.setplayerDecision(playerInput);
			//std::cout << playerDecision;

			if (isPlayer.getPlayerDecision() == "1"){
				isMap.setLastRoom("Kitchen");
				isMap.setCurrentRoom("Hall");
			}else if (isPlayer.getPlayerDecision() == "i"){
				isPlayer.setplayerDecision("");
				inventory();
			}else{
				std::cout << "That is not an option!\n";
				goto retry;
			}
		
		isMap.setkitchenFirstVisit(false);
	}
	else if (isPlayer.getPlayerDecision() == "2"){
		isMap.setLastRoom("Kitchen");
		isMap.setCurrentRoom("Hall");
	}
	else if (isPlayer.getPlayerDecision() == "i"){
		isPlayer.setplayerDecision("");
		inventory();
	}else{
		std::cout << "That is not an option!\n";
		goto retry0;
	}
}
//End  Kitchen handle events

//LivingRoom handel events
void handleLivingRoomEvents(){
	std::string playerInput;
	//Check if is first visit to LivingRoom so we can change text to fit
	if (isMap.getLivingRoomFirstVisit()){
		handleLivingRoomFirstChoice();
	}else{
		std::cout << "Besides the Dog Collar, i think there is not anything in this division... \n";
		std::cout << "[1] -> go to Library\n";
		std::cout << "[2] -> go to Hall\n";
		std::cout << "[i] -> open Inventory\n";
		retry:
		std::cin >> playerInput;

		isPlayer.setplayerDecision(playerInput);

		if (isPlayer.getPlayerDecision() == "1"){
			isMap.setLastRoom("LivingRoom");
			isMap.setCurrentRoom("Library");
		}
		else if (isPlayer.getPlayerDecision() == "2"){
			isMap.setLastRoom("LivingRoom");
			isMap.setCurrentRoom("Hall");
		}
		else if (isPlayer.getPlayerDecision() == "i"){
			isPlayer.setplayerDecision("");
			inventory();
		}
		else{
			std::cout << "That is not an option!\n";
			goto retry;
		}
	}
}

void handleLivingRoomFirstChoice(){
	std::string playerInput;

	std::cout << "You are in the Livingroom and your dog is not here, it seems that this room has an Library, wait ... what is this noise? Seems to come from the couch ...\n";
	std::cout << "[1] -> examine Couch\n";
	std::cout << "[2] -> go to Library\n";
	std::cout << "[3] -> go to Hall\n";
	std::cout << "[i] -> open Inventory\n";
	retry0:
	std::cin >> playerInput;

	isPlayer.setplayerDecision(playerInput);
	//std::cout << playerDecision;

	if (isPlayer.getPlayerDecision() == "1"){
		isGame.clearScreen();
		loadMap();
		isPlayer.setplayerDecision("");
		addInventoryItem("Dog Collar");
		std::cout << "You find yourself a rat gnawing on the collar of your dog !\n check your inventory.";
		std::cout << "[1] -> go to Library\n";
		std::cout << "[2] -> go to Hall\n";
		std::cout << "[i] -> open Inventory\n";
		retry:
		std::cin >> playerInput;

		isPlayer.setplayerDecision(playerInput);
		//std::cout << playerDecision;

		if (isPlayer.getPlayerDecision() == "1"){
			isMap.setLastRoom("LivingRoom");
			isMap.setCurrentRoom("Library");
		}
		else if (isPlayer.getPlayerDecision() == "2"){
			isMap.setLastRoom("LivingRoom");
			isMap.setCurrentRoom("Hall");
		}
		else if (isPlayer.getPlayerDecision() == "i"){
			isPlayer.setplayerDecision("");
			inventory();
		}
		else{
			std::cout << "That is not an option!\n";
			goto retry;
		}

		isMap.setLivingRoomFirstVisit(false);
	}
	else if (isPlayer.getPlayerDecision() == "2"){
		isMap.setLastRoom("LivingRoom");
		isMap.setCurrentRoom("Library");
	}
	else if (isPlayer.getPlayerDecision() == "3"){
		isMap.setLastRoom("LivingRoom");
		isMap.setCurrentRoom("Hall");
	}
	else if (isPlayer.getPlayerDecision() == "i"){
		isPlayer.setplayerDecision("");
		inventory();
	}
	else{
		std::cout << "That is not an option!\n";
		goto retry0;
	}
}
//End LivingRoom handle events

//Library handle events
void handleLibraryEvents(){
	std::string playerInput;

	std::cout << "This division appears to be haunted , perhaps it is best to get out of here...\n";
	std::cout << "[1] -> go to Livingroom\n";
	std::cout << "[i] -> open Inventory\n";
	retry:
	std::cin >> playerInput;

	isPlayer.setplayerDecision(playerInput);

	if (isPlayer.getPlayerDecision() == "1"){
		isMap.setLastRoom("Library");
		isMap.setCurrentRoom("LivingRoom");
	}else if (isPlayer.getPlayerDecision() == "i"){
		isPlayer.setplayerDecision("");
		inventory();
	}else{
		std::cout << "That is not an option!\n";
		goto retry;
	}
}
//End Library handle events

//HallWay handle events
void handleHallWayEvents(){
	std::string playerInput;

	std::cout << "you're in the hallway \n";
	std::cout << "[1] -> go to 1st floor\n";
	std::cout << "[2] -> go to Bathroom\n";
	std::cout << "[3] -> go to Hall\n";
	std::cout << "[i] -> open Inventory\n";
	retry:
	std::cin >> playerInput;

	isPlayer.setplayerDecision(playerInput);

	if (isPlayer.getPlayerDecision() == "1"){
		isMap.setLastRoom("HallWay");
		isMap.setCurrentRoom("Hall1");
	}
	else if (isPlayer.getPlayerDecision() == "2"){
		isMap.setLastRoom("HallWay");
		isMap.setCurrentRoom("Bathroom");
	}
	else if (isPlayer.getPlayerDecision() == "3"){
		isMap.setLastRoom("HallWay");
		isMap.setCurrentRoom("Hall");
	}
	else if (isPlayer.getPlayerDecision() == "i"){
		isPlayer.setplayerDecision("");
		inventory();
	}
	else{
		std::cout << "That is not an option!\n";
		goto retry;
	}
}
//End HallWay handle events

//Bathroom handle events
void handleBathroomEvents(){
	std::string playerInput;

	if (isMap.getBathroomFirstVisit()){
		handleBathroomFirstChoice();
	}else{
		std::cout << "It seems this division doesn´t have anything else that interests us...\n";
		std::cout << "[1] -> go to Hallway\n";
		std::cout << "[i] -> open Inventory\n";
	retry:
		std::cin >> playerInput;

		isPlayer.setplayerDecision(playerInput);

		if (isPlayer.getPlayerDecision() == "1"){
			isMap.setLastRoom("Bathroom");
			isMap.setCurrentRoom("HallWay");
		}
		else if (isPlayer.getPlayerDecision() == "i"){
			isPlayer.setplayerDecision("");
			inventory();
		}
		else{
			std::cout << "That is not an option!\n";
			goto retry;
		}
	}
}

void handleBathroomFirstChoice(){
	std::string playerInput;
	if (isInventory.getItemAsBeenUsed() == "Ligth"){
		std::cout << "You are using the flashlight, now you can see better... Look ! a key on the floor!\n";
		std::cout << "[1] -> grab Key\n";
		std::cout << "[2] -> go to Hallway\n";
		std::cout << "[i] -> open Inventory\n";
	retry0:
		std::cin >> playerInput;

		isPlayer.setplayerDecision(playerInput);

		if (isPlayer.getPlayerDecision() == "1"){
			isGame.clearScreen();
			loadMap();
			isPlayer.setplayerDecision("");
			addInventoryItem("Silver Key");
			std::cout << "You found a key!\ncheck your Inventory.\n";
			std::cout << "[1] -> go to HallWay\n";
			std::cout << "[i] -> open Inventory\n";
		retry1:
			std::cin >> playerInput;

			isPlayer.setplayerDecision(playerInput);
			//std::cout << playerDecision;

			if (isPlayer.getPlayerDecision() == "1"){
				isMap.setLastRoom("Bathroom");
				isMap.setCurrentRoom("HallWay");
			}
			else if (isPlayer.getPlayerDecision() == "i"){
				isPlayer.setplayerDecision("");
				inventory();
			}
			else{
				std::cout << "That is not an option!\n";
				goto retry1;
			}

			isMap.setBathroomFirstVisit(false);
		}
		else if (isPlayer.getPlayerDecision() == "2"){
			isMap.setLastRoom("Bathroom");
			isMap.setCurrentRoom("HallWay");
		}
		else if (isPlayer.getPlayerDecision() == "i"){
			isPlayer.setplayerDecision("");
			isInventory.setItemAsBeenUsed(inventory());
		}
		else{
			std::cout << "That is not an option!\n";
			goto retry0;
		}
	}
	else{
		std::cout << "You are in Bathroom, it´s a little dark in here...\n";
		std::cout << "[1] -> go to Hallway\n";
		std::cout << "[i] -> open Inventory\n";
	retry:
		std::cin >> playerInput;

		isPlayer.setplayerDecision(playerInput);

		if (isPlayer.getPlayerDecision() == "1"){
			isMap.setLastRoom("Bathroom");
			isMap.setCurrentRoom("HallWay");
		}
		else if (isPlayer.getPlayerDecision() == "i"){
			isPlayer.setplayerDecision("");
			isInventory.setItemAsBeenUsed(inventory());
		}
		else{
			std::cout << "That is not an option!\n";
			goto retry;
		}
	}
}
//End Bathroom handle events

//Hall1 handle events
void handleHall1Events(){
	std::string playerInput;

	std::cout << "You are in 1st floor Hallway\n";
	std::cout << "[1] -> go to Bedroom 1\n";
	std::cout << "[2] -> go to Bedroom 2\n";
	std::cout << "[3] -> go to Bathroom\n";
	std::cout << "[4] -> go to Ground floor\n";
	std::cout << "[i] -> open Inventory\n";
	retry:
	std::cin >> playerInput;

	isPlayer.setplayerDecision(playerInput);

	if (isPlayer.getPlayerDecision() == "1"){
		isMap.setLastRoom("Hall1");
		isMap.setCurrentRoom("Bedroom1");
	}
	else if (isPlayer.getPlayerDecision() == "2"){
		if (isInventory.getItemAsBeenUsed() == "Silver Key"){
			isMap.setLastRoom("Hall1");
			isMap.setCurrentRoom("Bedroom2");
		}
		else{
			std::cout << "This door is locked , you need the Silver Key to open...\n";
			goto retry;
		}
	}
	else if (isPlayer.getPlayerDecision() == "3"){
		isMap.setLastRoom("Hall1");
		isMap.setCurrentRoom("Bathroom1");
	}
	else if (isPlayer.getPlayerDecision() == "4"){
		isMap.setLastRoom("Hall1");
		isMap.setCurrentRoom("HallWay");
	}
	else if (isPlayer.getPlayerDecision() == "i"){
		isPlayer.setplayerDecision("");
		isInventory.setItemAsBeenUsed(inventory());
	}
	else{
		std::cout << "That is not an option!\n";
		goto retry;
	}
}
//End Hall1 handle events

//Bedroom1 handle events
void handleBedroom1Events(){
	std::string playerInput;
	if (isMap.getBedroom1FirstVisit()){
		handleBedroomFirstChoice();
	}
	else{
		std::cout << "You´ve been here before, it seems this division as nothing to us...\n";
		std::cout << "[1] -> go to Hall\n";
		std::cout << "[i] -> open Inventory\n";
	retry:
		std::cin >> playerInput;

		isPlayer.setplayerDecision(playerInput);

		if (isPlayer.getPlayerDecision() == "1"){
			isMap.setLastRoom("Bedroom1");
			isMap.setCurrentRoom("Hall1");
		}
		else if (isPlayer.getPlayerDecision() == "i"){
			isPlayer.setplayerDecision("");
			inventory();
		}
		else{
			std::cout << "That is not an option!\n";
			goto retry;
		}
	}
}

void handleBedroomFirstChoice(){
	std::string playerInput;
	std::cout << "it's cold in here, you have a chest in front of you with abandoned aspect...\n";
	std::cout << "[1] -> examine Chest\n";
	std::cout << "[2] -> go to Hallway\n";
	std::cout << "[i] -> open Inventory\n";
retry0:
	std::cin >> playerInput;

	isPlayer.setplayerDecision(playerInput);

	if (isPlayer.getPlayerDecision() == "1"){
		isGame.clearScreen();
		loadMap();
		isPlayer.setplayerDecision("");
		addInventoryItem("mainKey");
		std::cout << "You´ve found the mainKey!\ncheck your Inventory.\n";
		std::cout << "[1] -> go to Hallway\n";
		std::cout << "[i] -> open Inventory\n";
	retry:
		std::cin >> playerInput;

		isPlayer.setplayerDecision(playerInput);
		//std::cout << playerDecision;

		if (isPlayer.getPlayerDecision() == "1"){
			isMap.setLastRoom("Bedroom1");
			isMap.setCurrentRoom("Hall1");
		}
		else if (isPlayer.getPlayerDecision() == "i"){
			isPlayer.setplayerDecision("");
			inventory();
		}
		else{
			std::cout << "That is not an option!\n";
			goto retry;
		}

		isMap.setBedroom1FirstVisit(false);
	}
	else if (isPlayer.getPlayerDecision() == "2"){
		isMap.setLastRoom("Bedroom1");
		isMap.setCurrentRoom("Hall1");
	}
	else if (isPlayer.getPlayerDecision() == "i"){
		isPlayer.setplayerDecision("");
		inventory();
	}
	else{
		std::cout << "That is not an option!\n";
		goto retry0;
	}
}
//End Bedroom1 handle events

//Bedroom2 handle events
void handleBedroom2Events(){
	std::string playerInput;
	if (isMap.getBedroom2FirstVisit()){
		handleBedroom2FirstChoice();
	}
	else{
		std::cout << "you've been here before, look for a way out and leave this house!\n";
		std::cout << "[1] -> go to Hallway\n";
		std::cout << "[i] -> open Invetory\n";
	retry:
		std::cin >> playerInput;

		isPlayer.setplayerDecision(playerInput);

		if (isPlayer.getPlayerDecision() == "1"){
			isMap.setLastRoom("Bedroom2");
			isMap.setCurrentRoom("Hall1");
		}
		else if (isPlayer.getPlayerDecision() == "i"){
			isPlayer.setplayerDecision("");
			inventory();
		}
		else{
			std::cout << "That is not an option!\n";
			goto retry;
		}
	}
}

void handleBedroom2FirstChoice(){
	std::string playerInput;
	std::cout << "Hey you´ve found your Dog! he is ok, look for a way out and leave this house!\n";
	std::cout << "[1] -> go to Hallway\n";
	std::cout << "[i] -> open Inventory\n";
retry:
	playerInput;

	isPlayer.setplayerDecision(playerInput);

	if (isPlayer.getPlayerDecision() == "1"){
		isMap.setLastRoom("Bedroom2");
		isMap.setCurrentRoom("Hall1");
	}
	else if (isPlayer.getPlayerDecision() == "i"){
		isPlayer.setplayerDecision("");
		inventory();
	}
	else{
		std::cout << "That is not an option!\n";
		goto retry;
	}
}
//End Bedroom2 handle events

//Bathroom1 handle events
void handleBathroom1Events(){
	std::string playerInput;
	std::cout << "You 're in the bathroom on the 1st floor \n";
	std::cout << "[1] -> go to Hallway\n";
	std::cout << "[i] -> open Inventory\n";
	retry:
	std::cin >> playerInput;

	isPlayer.setplayerDecision(playerInput);

	if (isPlayer.getPlayerDecision() == "1"){
		isMap.setLastRoom("Bathroom1");
		isMap.setCurrentRoom("Hall1");
	}
	else if (isPlayer.getPlayerDecision() == "i"){
		isPlayer.setplayerDecision("");
		inventory();
	}
	else{
		std::cout << "That is not an option!\n";
		goto retry;
	}
}
//End Bathroom1 handle events

//Congrats handle events
void handleCongratsEvents(){
	std::string playerInput;
	std::cout << "Congratulations!!\n";
	std::cout << "[1] -> go to Menu\n";
	std::cout << "[2] -> Exit\n";
retry:
	std::cin >> playerInput;

	isPlayer.setplayerDecision(playerInput);

	if (isPlayer.getPlayerDecision() == "1"){

		gameState = Menu;
	}
	else if (isPlayer.getPlayerDecision() == "2"){
		gameState = Exit;
	}
	else{
		std::cout << "That is not an option!\n";
		goto retry;
	}
}
//End Congrats handle events

std::string inventory(){
	std::string item = "";
	std::string playerInput;
	while (isPlayer.getPlayerDecision() != "i"){
		isGame.clearScreen();
		loadMap();
		isPlayer.setplayerDecision("");
		
		for (int a = 0; a < 5; a++){
			if (iventoryItems[a] == "Ligth" && isMap.getCurrentRoom() == "Bathroom"){
				std::cout << "Item " << a + 1 << " -> " << iventoryItems[a] << "   [u]-> Use this item\n";
				item = iventoryItems[a];
			}
			else if (iventoryItems[a] == "Silver Key" && isMap.getCurrentRoom() == "Hall1"){
				std::cout << "Item " << a + 1 << " -> " << iventoryItems[a] << "   [u]-> Use this item\n";
				item = iventoryItems[a];
			}
			else if (iventoryItems[a] == "mainKey" && isMap.getCurrentRoom() == "Hall"){
				std::cout << "Item " << a + 1 << " -> " << iventoryItems[a] << "   [u]-> Use this item\n";
				item = iventoryItems[a];
			}
			else{
				std::cout << "Item " << a + 1 << " -> " << iventoryItems[a] << "\n";
			}
			
		}

		std::cout << "[i] -> close iventory\n";
		
		std::cin >> playerInput;

		isPlayer.setplayerDecision(playerInput);

		if (isPlayer.getPlayerDecision() == "u"){
			std::cout << "You used your item!\n";
			//item = iventoryDeleteItem();
			return item;
			//system("pause");
		}
	}
	return item;
}

std::string iventoryDeleteItem(){
	std::string itemDeleted = "";
	for (int a = 0; a < 5; a++){
		if (iventoryItems[a] == "Ligth" && isMap.getCurrentRoom() == "Bathroom"){
			//std::cout << "You have deleted Item " << a + 1 << " -> " << iventoryItems[a] << "   [u]-> Use this item\n";
			itemDeleted = iventoryItems[a];
			iventoryItems[a] = "empty";
			return itemDeleted;
		}
	}
	return itemDeleted;
}

std::string addInventoryItem(std::string item){
	for (int a = 0; a < 5; a++){
		if (iventoryItems[a] == "empty")
		{
			iventoryItems[a] = item;
			break;
		}
	}
	return item;
}

Thank you.

I´ve got some help already, on menu part, game is now working as it should.

Please criticize and give opinions of what can be improved, also what the best way to access a object from other .cpp file that was declared in main.cpp?

Thank you

Advertisement

Check out this old topics to get some ideas of how to make your game more OOP.

http://www.gamedev.net/topic/658228-text-adventure-user-input/

http://www.gamedev.net/topic/632562-text-adventure-help/

You really need to make your rooms Objects, and have them handle the inputs and what they contain, etc. Keep at it, you'll get it.

Good luck, and have fun.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Check out this old topics to get some ideas of how to make your game more OOP.

http://www.gamedev.net/topic/658228-text-adventure-user-input/

http://www.gamedev.net/topic/632562-text-adventure-help/

You really need to make your rooms Objects, and have them handle the inputs and what they contain, etc. Keep at it, you'll get it.

Good luck, and have fun.

Thank you, going to check those.

By reducing your dependency on globals as much as possible (unless they are const), by passing const-references (read-only) or references (read/write) into functions that need them, you create code that is stabler and easier to maintain and expand long-term (important in larger projects).

Check out this old topics to get some ideas of how to make your game more OOP.

http://www.gamedev.net/topic/658228-text-adventure-user-input/

http://www.gamedev.net/topic/632562-text-adventure-help/

You really need to make your rooms Objects, and have them handle the inputs and what they contain, etc. Keep at it, you'll get it.

Good luck, and have fun.

Hi BeerNuts i´m trying to follow your example on 2nd link, i can understand that you first call LoadRooms(), here you make your rooms objects and items as well, then you add item to a room object, then you set exit options for rooms objects and then you push to a vector array? i get lost here,


int main(int argc, char *argv[])
{
	LoadRooms();

	TPlayer Player("Mark");

	std::cout << "Hello " << Player.GetName() << " Welcome to My test Adventure Game.";

	bool bRunning = true;
	int currentRoom = 0;

	// Print the first room's description
	std::string temp;
	Rooms[currentRoom].Update("l", temp, Player);// <---- What are you doing here??? Rooms[0].Update(); wher .Update() comes from?? and where those arguments go from there???

Rooms[0].Update(); where .Update() comes from?? and where those arguments go from there???

nevermind

i get it now, it cames from TRoom since Room[currentRoom] is a Room.

Thank you.

Advertisement

Haven't done C++ in a long time but looks good. Wish I could help more but haven't touched that language for like 10 years.


Hi BeerNuts i´m trying to follow your example on 2nd link

Remember, that was only an example to get you to think more in objects. It wasn't fleshed out very far or thought out very much, and, looking at it, I realize there are things I could have done better. But, hopefully, it will give you something to think about as far as using objects.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

By reducing your dependency on globals as much as possible (unless they are const), by passing const-references (read-only) or references (read/write) into functions that need them, you create code that is stabler and easier to maintain and expand long-term (important in larger projects).

Thank you very much.


Hi BeerNuts i´m trying to follow your example on 2nd link

Remember, that was only an example to get you to think more in objects. It wasn't fleshed out very far or thought out very much, and, looking at it, I realize there are things I could have done better. But, hopefully, it will give you something to think about as far as using objects.

I just perused that old code a bit, and I realize how much better I could have made it. Let me add to it here, just because I have some time on my hands.

Firstly, in the TRoom class, I shouldn't be adding the exits in the description I give it. For example, when I add the closet room, I give it the description "You Are in a Large Closest. There is an Exit to the North."

But, the closet already know where the exits are, and what rooms they lead to. I should modify TRoom.cpp, Update() to print the direction and where it leads (code to be shown later).

Also, instead of using the function SpecializedCheck() in GameMain.cpp, I should give options to each room, and a function to call to parse the update.

So, I would add this to TRoom.h


// need to include map to map string options to functions at top of file
#include <map>
 
// in the public: area
// TParseFunc is a function type called when string options are matched for a room
// inputString is the input typed in, and room is a pointer to the room the input is for
typedef bool (*TParseFunc)(std::string inputString, TRoom& room, TPlayer& player);
 
// AddOption() takes a string to compare with, and on match, it calls the function given
void AddOption(std::string inputToParse, TParseFunc functionToCall);
 
// in private: area, add the map to store the options
 
// a map of possible string options this room can handle, and the functions to call on match
typedef std::map<std::string, TParseFunc> TParseFuncMap;
TParseFuncMap ParseFunctionMap;

Now, in TRoom.cpp, I'll modify Update, such that, on look, we'll print the possible exits, and where they lead, and we'll also check for the options stored in ParseFunctionMap, and, on match, call the function the user passed in


void TRoom::Update(std::string command, std::string& currentRoom,
                 TPlayer &player)
{
// Check for look first
if (command == "look" || command == "l") {
  std::cout << Description << std::endl;
  for (int i = 0; i < Items.size(); i++) {
    std::cout << "You see a " << Items[i].GetName() << std::endl;
  }
 
  std::cout << "Valid Exits are:";
  for (int i = 0; i < RoomsToMove.size(); i++) {
    if (RoomsToMove[i].IsEnabled) {
 
      // print not only the possible directions, but the name of the room it leads to
      std::cout << " " << RoomsToMove[i].Direction << ", leading to " << RoomsToMove[i].RoomToMove << "\n";
    }
  }
  return;
}
 
// Check for the options given in AddOption
if (ParseFunctionMap.find(command) != ParseFunctionMap.end()) {
  if (ParseFunctionMap[command](command, *this, player)) {
    return;
  }
}
 
// continue as normal
..
 
// include AddOption too
void TRoom::AddOption(std::string inputToParse, TParseFunc functionToCall)
{
  ParseFunctionMap[inputToParse] = functionToCall;
 
}

Now, when setting up the Rooms in LoadRooms(), I would not need to mention the exits in the descriptions, and I will call AddOption() to the Kitchen room, such that if "use broom" is typed, it will call the function I've defined earlier.

In GameMain.cpp:

// before main(), we need to define the OnUseBroom function, to be called when in the kitchen
bool OnUseBroom(std::string input, TRoom& room, TPlayer& player);

...

// this is our new LoadRooms()
void LoadRooms()
{
// Typically, you would load the rooms from a file here, but I'll hard code it for now
// We create the rooms then link them togetehr After
TRoom Closest("You Are in a Large Closest.", "Closest");

// This room has a broom
TItem Broom("broom", "It's an old Broom.");
Closest.AddItem(Broom);

// The Closest is linked to a kitchen
TRoom Kitchen("This is an old kitchen, With a Dirty Floor.", "Kitchen");

// No Item, but if you use Broom, it will show a trap door leading down
TRoom TreasureRoom("You've Found a Treasure Room with chests filled with Gold! You can retire rich now. Congratulations!", "TreasureRoom");

// now set the exits; Trap door going down not set yet, so make usable false
Closest.SetExit("n", Kitchen.GetName());
Kitchen.SetExit("s", Closest.GetName());
Kitchen.SetExit("d", TreasureRoom.GetName(), false);
TreasureRoom.SetExit("u", Kitchen.GetName());
 
// before adding the rooms, we need to add an option to Kitchen, so when "use broom" is called, it will call OnUseBroom()
Kitchen.AddOption("use broom", OnUseBroom);
 

Rooms.push_back(Closest);
Rooms.push_back(Kitchen);
Rooms.push_back(TreasureRoom);
}
 
// we can remove SpecializedCheck(), and add the specific functions we need, currently only OnUseBroom()
bool OnUseBroom(std::string input, TRoom& room, TPlayer& player)
{
  // ensure the player can use the broom (ie, has it and hasn't already been used), then enable the trap door exit
  if (player.UseItem("broom")) {
    std::cout << "You sweep the floor, and find a trap door leading Down!";
    
     // No need to Change the room's description so only need to set exit usable
     currentRoom.EnableExit("d", true);
     return true;
  }
}
return false;
 
}

Anyway, just a quick idea of how to expand it using function pointers. Maybe a more OOP method would be to use an interface to a class, but, IMO, that's overkill when a simple function is better suited to the task.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement