Ryan Bury

Where in the World is Carmen Sandiago?

This program doesn't like running outside of Visual Studio, so simply download the source code and build it in your own Visual Studio if you would like to run the program

Where in the World In Action

Download Source Code



main.cpp

 
/*
Project 7:
Programmer: Ryan Bury
Date: 4/16/2015
Additional Functionalities:
	-I finished the game so:
	-You can complete the game by either winning or losing
	-Hours left decreases rhythmically
	-The player and carmen's locations are tracked to see if the player is on the right track
*/

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include "fheaders.h"

using namespace std;

int getIntegerInput(string str = " ");
string welcome();
int navigate(int lastIndex);
string investigate(int index);

int main() {
	bool gameRunning = true;
	char input = ' ';

	while (gameRunning){

		int locationIndex = 0;
		initializeCityList();
		resetArrestCounter(8);
		string name = " ";

		name = welcome();

		while (getArrestCounter() > 0 && getHours() > 0) {

			locationIndex = getCurrentLocation();

			

			cout << "You are currently in : " << getCityName(locationIndex) << endl;

			//Output if the criminal is here
			if (locationIndex == getCriminalLocation())
			{
				cout << "I spotted one of Carmen's henchmen! She must have just been here." << endl;
			}
			else
			{
				cout << "No henchmen here!" << endl;
			}

			cout << "Do you want to investigate? (y, n) ";
			cin >> input;
			cin.ignore(256, '\n');
			if (input == 'y' || input == 'Y')
			{
				cout << endl << investigate(locationIndex) << endl;
			}
			else
			{
				cout << endl << "Alright, we won't investigate" << endl;
			}

			setCurrentLocation(navigate(locationIndex));
			
			printHours();
		}

		//Check to see if the player won and output the appropriate response
		if (getArrestCounter() == 0)
		{
			cout << endl << "You found Carmen, congratulations!" << endl;
		}
		else
		{
			cout << endl << "You failed to arrest the criminal in time. You're a terrible detective." << endl;
		}

		//Prompt the user if they want to play again
		cout << "Do you want to play again? (y, n) " << endl;
		cin >> input;
		if (input == 'y' || input == 'Y')
		{
			continue;
			cin.ignore(256, '\n');
		}
		else
		{
			gameRunning = false;
		}

	}

	return 0;
}


navigate.cpp

/*
Project 7
*/

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include "fheaders.h"

using namespace std;

int getIntegerInput(string str = " ");

string welcome() 
{

	int totalHours = 7 * 24 + 12;	//Total Hours available to the player
	int cityIndex = 0;
	string playerName;

	//Display welcome message, prompt user for name and enter name into "playerName" variable
	cout << "Welcome to \"Where In The World is Carmen Sandiego\"\n\n";
	cout << "Please enter your name: ";
	getline(cin, playerName);
	cout << "\n\n";

	cout << "Welcome, detective " << playerName << ". \n\n";

	//Initialize the hours the player has left from the value given to totalHours at the beginning of the program
	setHours(totalHours);

	//Tell player how many days and hours they have left to play
	printHours();

	return playerName;

}

int navigate(int lastIndex) 
{
	int choice, lastChoice = lastIndex;
	int max = getNumCities();

	cout << "Where would you like to go? " << endl;

	for (int i = 0; i < max; i++)
	{
		cout << i << ". " << getCityName(i) << endl;
	}
	cout << endl;
	choice = getIntegerInput("Please enter your selection: ");

	//If choice is not a valid choice repeat this loop
	while (choice < 0 || choice > max || choice == lastChoice)
	{
		cout << "Your choice is not valid" << endl;

		if (choice == lastChoice)
		{
			cout << "Your new destination must be different from your current location" << endl;
		}

		choice = getIntegerInput("Please enter your new selection: ");
	}

	cout << endl;
	cout << "Your input is: " << choice << endl;
	cout << "Your next destination is: " << getCityName(choice) << endl;

	//decrement hours by time it takes to fly
	decrementHours(4);

	//return index of choice
	return choice;
}

string investigate(int location) 
{
	string sdata = " ";
	int idata = 0;

	//input all cities from a text file to the vector "investigation"
	ifstream readFile;
	readFile.open("Investigation.txt");

	getline(readFile, sdata); //input the first number from the text file and create a max variable with it
	int max = stoi(sdata);

	vector<string> investigation(max); //create a vector with length max to fill with the names of the cities
	for (int i = 0; i < max; i++) 
	{
		getline(readFile, sdata);
		idata = stoi(sdata);
		getline(readFile, sdata);;
		investigation[idata] = sdata;
	}
	readFile.close();
	/*	When changing the text file make sure the top number is 1 more than the maximum
	iterator at the end of the program.
	e.g. currently the last selection is "10" then "Test 10" so the first number must be
	one more than 10, so it is 11.
	*/

	//input all strings from a text file to the vector "notSeen"
	readFile;
	readFile.open("NotSeen.txt");

	getline(readFile, sdata); //input the first number from the text file and create a max variable with it
	max = stoi(sdata);

	vector<string> notSeen(max); //create a vector with length max to fill with the names of the cities
	for (int i = 0; i < max; i++)
	{
		getline(readFile, sdata);
		idata = stoi(sdata);
		getline(readFile, sdata);;
		notSeen[idata] = sdata;
	}
	readFile.close();

	//Decrement hours by how long it takes to investigate
	decrementHours(2);

	if (location == getCriminalLocation())
	{
		setNextCriminalLocationRandom();

		decrementArrestCounter();

		return investigation[getCriminalLocation()];
	}
	else
	{
		return notSeen[location];
	}
}

int getIntegerInput(string str)
{
	int returnInt = -1;
	string *returnString = new string();
	cout << str;	
	getline(cin, *returnString);
	try 
	{
		returnInt = stoi(*returnString);
	}
	catch (exception e)
	{}
	cout << endl;
	delete returnString;
	return returnInt;
}


functions.cpp

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <vector>

using namespace std;

int NUM_CITIES;
vector<string> cities;

int hours = 0;

int currentLocation = 0;
int criminalLocation = 0;

int arrestCounter = 3;

void initializeCityList()
{
	string sdata = " ";
	int idata = 0;

	//input all cities from a text file to the vector "cities"
	ifstream readFile;
	readFile.open("Cities.txt");

	getline(readFile, sdata); //input the first number from the text file and create a max variable with it
	NUM_CITIES = stoi(sdata);

	cities.resize(NUM_CITIES); //create a vector with length max to fill with the names of the cities
	for (int i = 0; i < NUM_CITIES; i++) {
		getline(readFile, sdata);
		idata = stoi(sdata);
		getline(readFile, sdata);
		cities[idata] = sdata;
	}
	readFile.close();
	/*	When changing the text file make sure the top number is 1 more than the maximum
	iterator at the end of the program.
	e.g. currently the last selection is "6" then "Tokyo" so the first number must be
	one more than 6, so it is 7.
	*/
}

int getNumCities(){
	return NUM_CITIES;
}

string getCityName(int i)
{
	return cities[i];
}

int getCurrentLocation()
{
	return currentLocation;
}

void setCurrentLocation(int i)
{
	currentLocation = i;
}

int getCriminalLocation(){
	return criminalLocation;
}

void setCriminalLocation(int i)
{
	criminalLocation = i;
}

int nextRandomCityIndex(int currentLoc)
{
	int nextIndex = 0;

	do
	{
		srand(time(NULL));
		nextIndex = rand() % NUM_CITIES;
	} while (nextIndex == currentLoc);

	return nextIndex;
}

void setNextCriminalLocationRandom()
{
	int curLoc = 0, nextLoc = 0;

	curLoc = getCriminalLocation();

	nextLoc = nextRandomCityIndex(curLoc);

	setCriminalLocation(nextLoc);
}

int getArrestCounter()
{
	return arrestCounter;
}

void decrementArrestCounter()
{
	--arrestCounter;
}

void resetArrestCounter(int i)
{
	arrestCounter = i;
}

int getHours()
{
	return hours;
}

void setHours(int i)
{
	hours = i;
}

void decrementHours(int i)
{
	hours -= i;
}

void printHours(){
	cout << "You have " << getHours() / 24 << " days and " << getHours() % 24
		<< " hours left to arrest the criminal.\n\n";
}


fheaders.h

/*
Collection of function prototypes to include in each .cpp file
*/

#include <string>

using namespace std;

void initializeCityList();
string getCityName(int);
int getNumCities();
int nextRandomCityIndex(int currentLocation);

int getCurrentLocation();
void setCurrentLocation(int);

int getCriminalLocation();
void setCriminalLocation(int);
void setNextCriminalLocationRandom();

int getArrestCounter();
void decrementArrestCounter();
void resetArrestCounter(int);

int getHours();
void setHours(int);
void decrementHours(int i);
void printHours();


Cities.txt

11
0
Beijing
1
New York
2
London
3
Cairo
4
Rio de Janeiro
5
Boston
6
Tokyo
7
Nottinghamshire
8
Djibouti
9
Shanghai
10
Perth


Investigation.txt

11
0
Carmen wanted to go to the capital of China
1
Carmen wanted to see the Hudson bay from a skyscraper
2
Carmen expressed interest in the old Roman city Londinium
3
Carmen said she likes the largest city in the middle east
4
Carmen wants to see the Christ the Redeemer statue
5
Carmen was interested in seeing MIT
6
Carmen wanted to visit the most populated city in the world
7
Carmen wanted to see the middle of England
8
Carmen showed interest in the Horn of Africa
9
Carmen wanted to explore the financial center of China
10
Carmen said she liked the west coast of Australia


NotSeen.txt

11
0
Sorry, I haven't seen him or her
1
Nope no one by that name here
2
Never seen her
3
Whatchu talkin' bout willis? She ain't here
4
Nope, sorry
5
I don't even know anyone named Carmen...
6
Who's Carmen
7
Sorry, No one named Carmen has ever been here
8
I'll tell you if I do see her, but she was not here
9
Maybe you should check a different city?
10
I'm sorry I couldn't be of help, I haven't seen her