Site hosted by Angelfire.com: Build your free website today!
« February 2009 »
S M T W T F S
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
Entries by Topic
All topics  «
Blog Tools
Edit your Blog
Build a Blog
RSS Feed
View Profile
Open Community
Post to this Blog
You are not logged in. Log in
More C++ Programs from Random Places
Thursday, 7 September 2006
More banking...
//ANOTHER BANKING PROGRAM
//**NOTE**: THIS MATERIAL IS NOT COPYRIGHTED AND IS FREE FOR
//PERSONAL USE AND DISTRIBUTION

#include
struct process
{
int max[10],allocate[10],need[10],nallocate[10],nneed[10];
int finish;
}p[10];
int avail[10],navail[10],req[10];
int i,j,m,n,sum,s,pno;
int generate();
void newstatus();
void main()
{
int k,l;
printf("
ENTER THE TOTAL NUMBER OF RESOURCES :
");
scanf("%d",&n);
printf("
ENTER THE NUMBER OF AVAILABLE UNITS IN EACH RESOURCE TYPE
:
");
for(i=0;i scanf("%d",&avail[i]);
printf("
ENTER THE TOTAL NUMBER OF PROCESSES :
");
scanf("%d",&m);
printf("
ENTER THE MAXIMUM NUMBER OF UNITS NEEDED IN
");
printf("
EACH RESOURCE TYPE FOR EACH PROCESS:
");
for(i=0;i for(j=0;j scanf("%d",&p[i].max[j]);
printf("
ENTER THE ALLOCATED NUMBER OF UNITS IN
");
printf("
EACH RESOURCE TYPE FOR EACH PROCESS:
");
for(i=0;i for(j=0;j {
scanf("%d",&p[i].allocate[j]);
p[i].need[j]=p[i].max[j]-p[i].allocate[j];
}
for(j=0;j {
sum=0;
for(i=0;i sum+=p[i].allocate[j];
avail[j]=avail[j]-sum;
}
newstatus();
printf("
THE NEED MATRIX IS:

");
for(i=0;i printf("R%d ",i+1);
for(i=0;i {
printf("
");
for(j=0;j printf("%d ",p[i].need[j]);
}
l=generate();
if(l==0)
printf("
THE SYSTEM IS NOT IN THE SAFE STATE!!!
");
else
{
newstatus();
printf("
ENTER THE PROCESS NUMBER AND ITS REQUEST:
");
scanf("%d",&pno);
pno=pno-1;
k=s=0;
for(i=0;i {
scanf("%d",&req[i]);
if((req[i]>avail[i])||(req[i]>p[pno].need[i]))
k=1;
if(req[i]!=p[pno].need[i])
s=1;
}
if(k==1)
printf("
THE REQUEST CANNOT BE GRANTED!!!
");
else if(k==0)
{
for(i=0;i {
p[pno].nneed[i]=(p[pno].nneed[i])-req[i];
p[pno].nallocate[i]=(p[pno].nallocate[i])+req[i];
navail[i]=navail[i]-req[i];
if(s==0)
navail[i]=navail[i]+p[pno].nallocate[i];
}
printf("
");
if(s==0)
{
printf("

P%d ",pno+1);
p[pno-1].finish=1;
}
l=generate();
if(l==0)
printf("
THE REQUEST CANNOT BE GRANTED!!!
");
if(l==1)
printf("
THE REQUEST IS GRANTED!!!
");
}
}
}
void newstatus()
{
for(i=0;i navail[i]=avail[i];
for(i=0;i {
for(j=0;j {
p[i].nallocate[j]=p[i].allocate[j];
p[i].nneed[j]=p[i].need[j];
}
p[i].finish=0;
}
}
int generate()
{
int l,q,k,f;
l=0;
while(1)
{
for(i=0;i {
if(p[i].finish==0)
{
k=1;
for(j=0;j if(p[i].nneed[j]>navail[j])
k=0;
if(k==0)
l++;
else if(k==1)
{
printf("P%d ",i+1);
for(j=0;j navail[j]+=p[i].nallocate[j];
p[i].finish=1;

}
}
}
if(l>m)
return 0;
q=0;
for(f=0;f if(p[f].finish==1)
q++;
if(q==m)
{
printf("
ABOVE IS THE SAFE SEQUENCE AND THE SYSTEM
IS IN SAFE STATE!!!
");
return 1;
}
}
}

Posted by poetry/maddoxtheconqueror at 12:01 AM CDT
Post Comment | Permalink | Share This Post
Saturday, 12 August 2006
Tic Tac Toe
//THIS IS A COOL TIC-TAC-TOE PROGRAM
//**NOTE**:THIS IS NOT COPYRIGHED MATERIAL AND IS FREE FOR PERSONAL
//USE AND DISTRIBUTION
#include
#include

void Board();
void PlayerX();
void PlayerO();
void Player_win();
void check();
int win=0,wrong_X=0,wrong_O=0,chk=0;

char name_X[30];
char name_O[30];
int pos_for_X[3][3];
int pos_for_O[3][3];
int pos_marked[3][3];

void main()
{
int i,ch,j;
char ans;
/* clrscr();
printf("\n\t\t\t\tTIC TAC TOE");
printf("\n\t\t\t\t");
for(i=1;i<=11;i++)
{
delay(10000);
printf("*");
}*/
do
{
clrscr();
printf("\n\t\t\t\tTIC TAC TOE");
printf("\n\t\t\t\t");
for(i=1;i<=11;i++)
{
delay(10000);
printf("*");
}
printf("\n1.Start The Game");
printf("\n2.Quit The Game");
printf("\nEnter your choice(1-2) : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
chk=0;
win=0;
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
pos_for_X[i][j]=0;
pos_for_O[i][j]=0;
pos_marked[i][j]=0;
}
}
printf("\n\n");
clrscr();
printf("\nEnter the name of the player playing for \'X\': ");
fflush(stdin);
gets(name_X);
printf("\nEnter the name of the player playing for \'O\': ");
fflush(stdin);
gets(name_O);
Board();
for(;;)
{
if(win==1)
break;
check();
if(chk==9)
{
printf("\n\t\t\tMATCH DRAWS!!");
printf("\nPress any key....");
break;
}
else
chk=0;
printf("\nTURN FOR %s:",name_X);
PlayerX();
do
{
if(wrong_X!=1)
break;
wrong_X=0;
printf("\nTURN FOR %s:",name_X);
PlayerX();
}while(wrong_X==1);
check();
if(chk==9)
{
printf("\n\t\t\tMATCH DRAWS");
printf("\nPress any key....");
break;
}
else
chk=0;
printf("\nTURN FOR %s:",name_O);
PlayerO();
do
{
if(wrong_O!=1)
break;
wrong_O=0;
printf("\nTURN FOR %s:",name_O);
PlayerO();
}while(wrong_O==1);

}
Board();
if(win!=1)
{
printf("\n\t\t\tMATCH DRAWS!!");
printf("\nPress any key.......");
}
getch();
break;
case 2:
printf("\n\n\n\t\t\tThank You For Playing The Game.");
printf("\n\t\t\t###############################");
getch();
exit(1);
break;
}
printf("\nWant To Play(Y/N) ? ");
fflush(stdin);
scanf("%c",&ans);
}while(ans=='y' || ans=='Y');
}


void Board()
{
int i,j;
clrscr();
printf("\n\t\t\t\tTIC TAC TOE BOARD");
printf("\n\t\t\t\t*****************");
printf("\n\n\n");
printf("\n\t\t\t 1\t 2\t 3");
for(i=1;i<=3;i++)
{
printf("\n \t\t\t _____________________________");
printf("\n \t\t\tº\t º\t º\t º");
printf("\n\t\t%d\t",i);
for(j=1;j<=3;j++)
{

if(pos_for_X[i][j]==1)
{
printf(" X");
printf(" ");
}
else if(pos_for_O[i][j]==1)
{
printf(" O");
printf(" ");
}
else
{
printf(" ");
continue;
}
}
printf("\n\t\t\tº\t º\t º\t º");
}
printf("\n\t\t\t------------------------------");
Player_win();
}


void PlayerX()
{
int row,col;
if(win==1)
return;
printf("\nEnter the row no. : ");
fflush(stdin);
scanf("%d",&row);
printf("Enter the column no. : ");
fflush(stdin);
scanf("%d",&col);
if(pos_marked[row][col]==1 || row<1 || row>3 || col<1 || col>3)
{
printf("\nWRONG POSITION!! Press any key.....");
wrong_X=1;
getch();
Board();
}
else
{
pos_for_X[row][col]=1;
pos_marked[row][col]=1;
Board();
}
}
void PlayerO()
{
int row,col;
if(win==1)
return;
printf("\nEnter the row no. : ");
scanf("%d",&row);
printf("Enter the column no. : ");
scanf("%d",&col);
if(pos_marked[row][col]==1 || row<1 || row>3 || col<1 || col>3)
{
printf("\nWRONG POSITION!! Press any key....");
wrong_O=1;
getch();
Board();
}
else
{
pos_for_O[row][col]=1;
pos_marked[row][col]=1;
Board();
}
}
void Player_win()
{
int i;
for(i=1;i<=3;i++)
{
if(pos_for_X[i][1]==1 && pos_for_X[i][2]==1 && pos_for_X[i][3]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_X);
printf("\nPress any key............");
return;
}
}
for(i=1;i<=3;i++)
{
if(pos_for_X[1][i]==1 && pos_for_X[2][i]==1 && pos_for_X[3][i]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_X);
printf("\nPress any key............");
return;
}
}
if(pos_for_X[1][1]==1 && pos_for_X[2][2]==1 && pos_for_X[3][3]==1)
{
win=1;
printf("\n\nRESULTL: %s wins!!",name_X);
printf("\nPress any key......");
return;
}
else if(pos_for_X[1][3]==1 && pos_for_X[2][2]==1 &&
pos_for_X[3][1]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_X);
printf("\nPress any key.....");
return;
}

for(i=1;i<=3;i++)
{
if(pos_for_O[i][1]==1 && pos_for_O[i][2]==1 && pos_for_O[i][3]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_O);
printf("\nPress any key.....");
return;
}
}
for(i=1;i<=3;i++)
{
if(pos_for_O[1][i]==1 && pos_for_O[2][i]==1 && pos_for_O[3][i]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_O);
printf("\nPress any key.....");
return;
}
}
if(pos_for_O[1][1]==1 && pos_for_O[2][2]==1 && pos_for_O[3][3]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_O);
printf("\nPress any key.....");
return;
}
else if(pos_for_O[1][3]==1 && pos_for_O[2][2]==1 &&
pos_for_O[3][1]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_O);
printf("\nPress any key.....");
return;
}
}
void check()
{
int i,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
if(pos_marked[i][j]==1)
chk++;
else
continue;
}
}
}

Posted by poetry/maddoxtheconqueror at 12:01 AM CDT
Post Comment | Permalink | Share This Post
Friday, 28 July 2006
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



Posted by poetry/maddoxtheconqueror at 12:01 AM CDT
Post Comment | Permalink | Share This Post
City Chase part 1
//THIS PROGRAM IS VERY INTERESTING IN THAT IT INVOLVES A
//PURSUER CHASING A CRIMINAL. SORRY FOR ITS HUGE SIZE
//**NOTE**: THIS IS NOT COPYRIGHTED AND IS FREE FOR PERSONAL
//USE AND DISTRIBUTION

#include "city.h"
#include
#include
#include

using namespace std;

city::city(string n_name) //constructor
{
name=n_name;

is_evader=false;
}

void city::add_link(city *new_link)
{
links.push_back(new_link); //adds the pointer to the overall list for this city
}

void city::remove_link(city *link)
{
list::iterator p=links.begin();

bool found=false; //set to true if the link is found
for(; p!=links.end(); p++){ //checks links to see if the link in question actually exists

if(link==(*p)){
found=true;
links.erase(p); //if found, erases the link and sets found to true
break;
}
}

if(!found){ //prints an error message if the link didn't actually exist
cout << "remove-link " << name << " " << (*link).name << " failed" << endl;
}
}


list city::neighbors() //returns the names in alphabetical order of the cities next to this one
{
list::iterator p=links.begin();

list link_names;

for(; p!=links.end(); p++){ //adds the names of the links into the new list

link_names.push_back((*p)->name);

}

link_names.sort(); //alphabetizes the list

return link_names;
}

#ifndef _city_h_
#define _city_h_

#include
#include
#include


using namespace std;

class city{
public:

friend class graph;

city(string n_name); //constructor

void add_link(city *new_link); //adds a link between this city and parameter 1

void remove_link(city *link); //removes the link between this city and parameter 1

list neighbors(); //returns the names in alphabetical order of the neighbors of the city

private:

string name; //name of the city
bool is_evader; //true if an evader is present in the city
list links; //pointers to all cities that are neighbors to it

};

#endif

#include "graph.h"
#include "city.h"
#include
#include "time.h"
#include "stdlib.h"


using namespace std;

graph::graph()
//Purpose: constructor
{
evader=""; //initializes member variables

evader_location=NULL;
}

void graph::add_city(city *n_city)
//Purpose: adds a city to the graph
{
list::iterator p=cities.begin(); //iterator to check and see if the city is already present

bool found=false; //set to true if the city is already present

for(; p!=cities.end(); p++){ //searches the graph
if((*p)->name==(*n_city).name){
found=true;
cout << "add-city " << (*n_city).name << " failed" << endl;
break;
}
}
if(!found){ //if not already present
cities.push_back(n_city); //adds the city to the list and prints a message stated it
cout << "City " << n_city->name << " added to the graph" << endl;
}
}

void graph::remove_city(city *city_name)
//Purpose: removes a city and all links to it from the graph
{
list::iterator p_itr=pursuers.begin(); //iterator to search the list to see if a pursuer is in this city

for(; p_itr!=pursuers.end();){ //searches the list of pursuers and checks to see if their location
// equals this city
if((*p_itr)->location==city_name){
cout << "Pursuer " << (*p_itr)->name << " removed from the game" << endl;
p_itr=pursuers.erase(p_itr); //removes the pursuer and prints a message stating it
}
else{
p_itr++;
}
}

if(evader_location==city_name){ //checks to see if the evader is in this city
cout << "Evader " << evader << " removed from the game" << endl;
evader_location=NULL; //resets evader's values and prints a message stating that it was
//removed from the game
evader="";
}

list::iterator p=((*city_name).links).begin();
for(; p!=((*city_name).links).end(); p++){ //searches through this cities links and removes the links that point
(*p)->remove_link(city_name); // point back to itself
}

p=cities.begin(); //resets the iterator to the beginning of the list
for(; p!=cities.end(); p++){
if((*p)==city_name){ //finds and erases the city from the overall list
p=cities.erase(p);
break;
}
}
}

void graph::add_link(city *origin, city *dest, bool &error)
//Purpose: adds a link from one city to the other (not the link back, the main file calls this function twice)
{
if(!error){ //this bool is passed into the function. If it's true
// this function does nothing
list temp_links=(*origin).neighbors(); //creates a temporary alphabetized list of the names of the first cities neighbors

list::iterator p=temp_links.begin(); //iterator for the list of names

for(; p!=temp_links.end(); p++){
if((*p)==(*dest).name){ //if there is already a link to the destination city, prints an error message
error=true; // and sets the bool error to true to notify the main file of the error (so
// it doesn't print multiple error messages or continue)
cout << "add-link " << (*origin).name << " " << (*dest).name << " failed" << endl;
break;
}
}

if(!error){ //otherwise, it adds the link
(*origin).add_link(dest);
}
}
}

void graph::remove_link(city *origin, city *dest)
//Purpose: removes the links between two cities
{
(*origin).remove_link(dest); //removes the link from this city to the other
(*dest).remove_link(origin); //removes the link back
}

void graph::place_pursuer(string p_name, city *location)
//Purpose: places a pursuer with name p_name in the city pointed to by location
{
pursuer *new_pursuer=new pursuer(p_name, location); //creates a new pursuer object
pursuers.push_back(new_pursuer); //adds the pursuer to graph's list
}

void graph::place_evader(string e_name, city *location)
//Pursuer: redefines graph's member variables that describe the evader
{
(*location).is_evader=true; //sets a bool in the city that is the evader's location to true
evader_location=location; //sets the location of the evader
evader=e_name; //sets the name of the evader
}

void graph::move_pursuer(pursuer *p, city *dest)
//Purpose: takes in a pointer to a pursuer and resets its member variable to point to a new city
{
(*p).location=dest;
}

void graph::move_evader(city *dest)
//Purpose: moves the evader to city (*dest)
{
(*evader_location).is_evader=false; //sets the bool in the evader's previous location to false
(*dest).is_evader=true; //sets the bool in the new city to true
evader_location=dest; //updates the evader_location for the graph
}

city* graph::search_cities(string city_name)
//Purpose: takes in the name of a city, and returns a pointer to the city object with that name. Returns NULL if the city wasn't found.
{
list::iterator p=cities.begin(); //iterator for searching

int i=0; //int to end the while loop if it hits the end of the list of cities

while((*p)->name!=city_name){ //searches the list until it hits the right city
p++;
i++;
if(i==cities.size()){ //if it's hit the end of the list and the city hasn't been found
return NULL;
}
}

return (*p);
}

bool graph::tick()
//Purpose: manages all that must happen in one increment of time in the pursuer-evader game
{
cout << "TICK" << endl; //prints the initial message "TICK"

if(evader_location!=NULL){ //assuming the evader is in the game (he could have been
// erased in a city removal
cout << "Where should " << evader << " go next?" << endl; //Prints the question to the user and the first option
cout << " (0) stay in " << (*evader_location).name << endl;

list link_names=(*evader_location).neighbors(); //creates a temporary list of the names of the possible
// cities the evader can reach in one turn

list::iterator p=link_names.begin(); //iterator for the list of links

for(unsigned int i=0; i cout << " (" << i+1 << ") " << (*p) << endl;
p++;
}

int choice; //the user's choice

cin >> choice; //defines the user's choice

if(choice>link_names.size()){ //if the user makes a mistake and selects something greater than they
// were allowed to. Chooses the last choice for them
cout << "invalid input: choosing the next closest int" << endl;
choice=link_names.size();
}
if(choice==0){ //if the choice is 0, the evader doesn't move and a message stating such
// is printed to the screen
cout << evader << " stays in " << (*evader_location).name << endl;
}
else{
p=link_names.begin();
for(int j=1; j p++; // value the user input
}
city *destination=search_cities(*p); //finds the pointer to the city the user selected

cout << evader << " moves from " << (*evader_location).name; //prints the first part of the evader's move to the screen
move_evader(destination); //moves the evader
cout << " to " << (*evader_location).name << endl; //prints the final part of the evader's move
}
}
list::iterator q=pursuers.begin(); //iterator to go through all the pursuers
for(; q!=pursuers.end(); q++){

time_t seconds;

seconds = time (NULL);
srand(seconds); //seeds the random number generator using "time.h"
int choice; //an int that referres to the pursuers future destination
if((*q)->location->links.size()==0){ //if there are no links from the pursuer's city, forces them to choose '0'
choice=0;
}
else{
choice=rand()%((*q)->location->links.size()); //otherwise, uses the random number generator to pick the next city
}

if(choice==0){ //if choice was 0, the pursuer doesn't need to be moved, and a message
// is printed to the screen
cout << (*q)->name << " stays in " << ((*q)->location)->name << endl;
}
else{
list link_temp=((*q)->location)->links; //copies the list from the pursuer's current location
list::iterator link_itr=link_temp.begin();
for(int j=1; j // available to the pursuer
link_itr++;
}

pursuer *pur_ptr=(*q); //creates a new pointer to point to the current pursuer

cout << (*q)->name << " moves from " << pur_ptr->location->name << " to "; //prints where the pursuer has moved from

move_pursuer(pur_ptr, (*link_itr)); //moves the pursuer

cout << pur_ptr->location->name << endl; //prints where the pursuer has moved to

}

}

q=pursuers.begin();
for(; q!=pursuers.end(); q++){ //searches through the list of pursuer locations to see if any have hit
if((*q)->location->is_evader){ // the evader
cout << (*q)->name << " has caught " << evader << " in " << evader_location->name << endl;
return false; //if the evader has been caught, prints an appropriate message and returns
} // false so that the main program can end
}
return true; //if the evader isn't caught, returns true so that the main program continues
}

void graph::neighbors(city *query)
//Purpose: prints out the neighbors for the city that query points to in alphabetical order
{
cout << "Neighbors for city " << (*query).name << ": "; //prints a title to the screen
list temp_links=(*query).neighbors(); //creates a temporary list returned from the cities member function

if(temp_links.size()==0){ //if there's nothing in the list:
cout << "- none -" << endl; //prints none
}
else{
list::iterator p=temp_links.begin(); //iterator to go through the list of names

for(; p!=temp_links.end(); p++){ //iterates through the list and prints each item (city's member function
// alphabetized the list already)
cout << (*p) << " ";
}
cout << endl; //finishes the line
}
}

#ifndef _graph_h_
#define _graph_h_

#include "city.h"
#include
#include
#include
#include "pursuer.h"

using namespace std;


class graph{
public:

graph(); //constructor

void add_city(city *n_city); //adds a precreated city to the graph

void remove_city(city *city_name); //removes the city and its memory from the graph

void add_link(city *origin, city *dest, bool &error); //adds a link between the two cities

void remove_link(city *origin, city *dest); //removes a link between the two cities

void place_pursuer(string p_name, city *location); //creates and places a pursuer in the location

void place_evader(string e_name, city *location); //creates an evader and places it in the location

void move_pursuer(pursuer *p, city *dest); //moves the pursuer's location

void move_evader(city *dest); //moves the evader's location

void neighbors(city *query); //finds the neighbors' names of the city in question

city* search_cities(string city_name); //takes in the name of a city and returns a pointer to that city

bool tick(); //manages one time iteration in the game


private:

list cities; //a list of pointers to all cities in the graph
list pursuers; //a list of pointers to all the pursuers in the game

string evader; //the name of the evader

city *evader_location; //a pointer to the city the evader is in


};

Posted by poetry/maddoxtheconqueror at 12:01 AM CDT
Post Comment | Permalink | Share This Post

Newer | Latest | Older