City Chase part 2
//THIS IS PART 2 TO THE CITY CHASE PROGRAM
//AGAIN, SORRY FOR THE HUGE SIZE
//**NOTE**: THIS IS NOT COPYRIGHTED MATERIAL AND IS
//FREE FOR PERSONAL USE AND DISTRIBUTION
#include
#include
#include
#include "graph.h"
#include "city.h"
#include
using namespace std;
void input_sorter(ifstream& input, graph& network)
//Purpose: sorts the input file and calls the appropriate commands
{
string command, info1, info2; //three strings to be defined by the input file
bool evader=false; //set to true when an evader is created (in case the user tries to make more than 1)
bool ticker=true; //when false, the evader has been caught and the game is over
while(input >> command && ticker){
if(command=="add-city"){
input >> info1; //reads in the city to be added
city *new_city=new city(info1); //creates a new city object
network.add_city(new_city); //calls appropriate function in the graph
}
else if(command=="add-link"){
input >> info1 >> info2; //reads in the cities that have the link between them
city *city1=network.search_cities(info1); //finds the pointer for the first city
city *city2=network.search_cities(info2); //finds the pointer for the second city
if(city1==NULL || city2==NULL){ //if either doesn't exist, print an error
cout << "add-link " << info1 << " " << info2 << " failed" << endl;
}
else{
bool error=false; //resets error to false, if either add_link fails, the other add_link won't
network.add_link(city1, city2, error); // be implemented and the success message won't be printed
network.add_link(city2, city1, error);
if(!error){
cout << "Link added between " << info1 << " and " << info2 << endl;
}
}
}
else if(command=="remove-city"){
input >> info1; //reads in the city to be removed
city *doomed_city=network.search_cities(info1); //finds the pointer for the city to be removed
if(doomed_city==NULL){ //if the city wasn't found, prints an error message
cout << "remove " << info1 << " failed" << endl;
}
else{
cout << "City " << info1 << " removed from the graph" << endl;
network.remove_city(doomed_city); //removes the city
}
}
else if(command=="remove-link"){
input >> info1 >> info2; //reads in the cities that have the link between them to be removed
city *city1=network.search_cities(info1); //finds the pointer to the first city
city *city2=network.search_cities(info2); //finds the pointer to the second city
network.remove_link(city1, city2); //removes the link
cout << "Link between " << info1 << " and " << info2 << " removed" << endl;
}
else if(command=="neighbors"){
input >> info1; //reads in the city whose neighbors are requested
city *city1=network.search_cities(info1); //finds the pointer to that city
if(city1==NULL){ //if the city wasn't found, prints an error message
cout << "neighbors " << info1 << " failed" << endl;
}
else{ //otherwise, calls the function to print the neighbors
network.neighbors(city1);
}
}
else if(command=="place-evader"){
input >> info1 >> info2; //reads in the evader name and the city
city *city1=network.search_cities(info2); //finds a pointer to the city
if(city1==NULL || evader){ //if the city wasn't found or there is already an evader in the game
// prints an error message
cout << "place-evader " << info1 << " in " << info2 << " failed" << endl;
}
else{
cout << "Evader " << info1 << " placed in " << info2 << endl;
network.place_evader(info1, city1); //places the evader
evader=true; //sets evader to true so that another evader isn't put in the game
}
}
else if(command=="place-pursuer"){
input >> info1 >> info2; //reads in the pursuer name and the city
city *city1=network.search_cities(info2); //finds the pointer to that city
if(city1==NULL){ //if the city wasn't found, prints an error message
cout << "place-pursuer " << info1 << " in " << info2 << " failed" << endl;
}
else{
cout << "Pursuer " << info1 << " placed in " << info2 << endl;
network.place_pursuer(info1, city1); //else, calls a function to place the pursuer
}
}
else if(command=="tick"){
ticker=network.tick(); //if ticker is set to false, the game will end
}
else{ //error message in case the input gives an unknown command
cout << command << " is an invalid command" << endl;
}
}
}
int main(int argc, char* argv[])
{
if(argc!=2){ //Prints a standard error message if the user inputs the wrong number of command
cerr << "Usage" << endl; // line arguments.
cerr << argv[0] << " input-file" << endl;
return 1; //Program terminates.
}
ifstream input(argv[1]);
if(!input){ //Prints an error message if the input file doesn't exist.
cerr << "Cannot open " << argv[1] << " for input." << endl;
return 1; //Program terminates.
}
graph network; //initializes an empty graph
input_sorter(input, network); //calls input sorter to sort and implement the commands in the input file
return 0;
}
#include "pursuer.h"
#include
using namespace std;
pursuer::pursuer(string n_name, city *n_location)
{
name=n_name;
location=n_location;
}
#include "city.h"
#include
using namespace std;
class pursuer{
public:
friend class graph;
pursuer(string n_name, city *n_location); //constructor
void set_location(city *new_location){
location=new_location;
}
private:
string name; //name of the pursuer
city *location; //pointer to the city the pursuer is currently in
};
#include
#include "person.h"
#include "city.h"
#include "graph.h"
bool Graph::tick() {
cout << "TICK" << endl;
// make choices
City* ec;
if (m_evader != NULL) ec = evader_choice(m_evader,this);
vector pc(m_pursuers.size());
int i;
for (i = 0; i < m_pursuers.size(); i++) {
pc[i] = pursuer_choice(m_pursuers[i],this);
}
// move them
if (m_evader != NULL) m_evader->move(ec);
for (i = 0; i < m_pursuers.size(); i++) {
m_pursuers[i]->move(pc[i]);
if (m_evader != NULL && m_pursuers[i]->location() == m_evader->location()) {
cout << m_pursuers[i]->name() << " has caught " << m_evader->name() << " in "
<< m_pursuers[i]->location()->name() << endl;
// evader has been caught
return false;
}
}
// evader safe for another turn
return true;
}
#ifndef _person_h_
#define _person_h_
// A Person object stores its name and a pointer to the city where it is currently located.
#include
#include "city.h"
using namespace std;
class Person {
public:
// CONSTRUCTOR
Person(const string& name, City *city) : m_location(city), m_name(name) {}
// ACCESSORS
const string& name() const { return m_name; }
city* location() const { return m_location; }
// MODIFIER
void move(city *city);
private:
// REPRESENTATION
city* m_location;
string m_name;
};
#endif
#include
#include
#include
#include "person.h"
#include "city.h"
#include "graph.h"
#include
using namespace std;
// This file includes a selection of evader functions.
// Use the #if statements to specify which function to use.
// Code inside of #if 0 / #endif is ignored.
// For extra credit, create your own strategy, enable it
// within #if 1 / #endif, and enter the contest.
#if 0
// NEVER MOVES
City* evader_choice(Person *p, Graph *city_graph) {
assert (p != NULL);
assert (city_graph != NULL);
assert (p->location() != NULL);
return p->location();
}
#endif
#if 0
// MOVES RANDOMLY
City* evader_choice(Person *p, Graph *city_graph) {
assert (p != NULL);
assert (city_graph != NULL);
assert (p->location() != NULL);
const vector& neighbors = p->location()->get_neighbors();
// select a random number
int r = int(rand()%(neighbors.size()+1));
if (r == 0) return p->location();
assert (r >= 1 && r <= neighbors.size());
return neighbors[r-1];
}
#endif
#if 1
// QUERY USER
city* evader_choice(Person *p, graph *city_graph) {
assert (p != NULL);
assert (city_graph != NULL);
assert (p->location() != NULL);
// print selections on the screen
cout << "Where should " << p->name() << " go next?" << endl;
const vector& neighbors = p->location()->neighbors();
cout << " (0) stay in " << p->location()->name() << endl;
int i;
for (i = 0; i < neighbors.size(); i++)
cout << " (" << i+1 << ") " << neighbors[i]->name() << endl;
do { cin >> i; } while (i < 0 || i > neighbors.size());
// now go there
if (i == 0) return p->location();
assert (i > 0 && i <= neighbors.size());
return neighbors[i-1];
}
#endif
#if 0
// MY STRATEGY
City* evader_choice(Person *p, Graph *city_graph) {
}
#endif