//CMPT-212, ASSIGNMENT 3 2/2 - Field Source File //Raymond Kin Hei Lee //March 23, 2008 #include "field.h" using namespace std; Field :: Field(int x, int y) //Constructor { grid.reserve(x*y); //Reserve grid size vector temp(0); //Create a tempoary vector with zero size //to put into our grid for(long i=0; ipriority()priority()) { //Found a priority smaller, but further in the index.. swap needed temp = grid.at(point).at(i); grid.at(point).at(i) = grid.at(point).at(j); grid.at(point).at(j) = temp; } } } } agentInfo Field :: findInfo(const Point &p) //For a coordinate, find out its agentInfo { //agentInfo if s pair of info, agentInfo temp; for(signed int j=1; j>=-1; j--) { //For each coordinate... for(signed int i=-1; i<=1; i++) { if((p.x==0 && i==-1)||(p.x==(sizex-1)&&i==1) || (p.y==0 && j==-1) || (p.y==(sizey-1)&&j==1)) //Off boundary, return '*' temp.first.push_back('*'); else if(grid.at(sizex*(p.y + j) + (p.x+i)).size()==0) temp.first.push_back(' '); //Nothing there, size()=0, return ' ' else if(!(i==0 && j==0)) temp.first.push_back(grid.at(sizex*(p.y+j)+(p.x+i)).at(0)->state()); else for(unsigned long i=0; istate()); } } } return temp; } vector Field :: findDirection(Point p, vector dir) {//Changes the directions (N, NE, S, etc..) that is returned by act() into meaningful Points Point temp; vector dirVec; //Vector, since more than one Point is possible... for(unsigned long i=0; i= 0 && i < n_of_agents(p)) return grid.at(convert(p)).at(i); //return specified Agent* } return NULL; } void Field :: remove_agent(const Point &p, long i) { vector::iterator it; if(convert(p) < sizex * sizey) { if(i >0 || i < n_of_agents(p)) { it = grid.at(convert(p)).begin(); delete grid.at(convert(p)).at(i); //Delete specified Agent* grid.at(convert(p)).erase(it+i); //Remove the deleted Agent* from stack } } } void Field :: remove_agent(const Point &p) { long point = convert(p); if(point < sizex * sizey) { for(long i=grid.at(point).size()-1; i>=0; i--) { delete grid.at(point).at(i);//Delete grid.at(point).pop_back();//Pop the furthest one.. but all will be removed anyway } } } void Field :: remove_agent(const Region &r) { Point temp; for(int j=r.first.y; j< r.second.y; j++) { for(int i=r.first.x; i 0) return grid.at(point).at(0)->state(); //Calls state() else return ' '; } void Field :: step(long n) {//I followed the Professor's suggestion for this part. All surroundings are first stored, //then all agents are placed in the tempGrid according to act() being called by the field while(n>0) { vector> tempGrid; //tempoary grid for putting new actions vector temp123(0); for(long i=0; i> agentSurrounding; //Creating a vector for holding agent info vector temp1234(0); //agentInfo includes mySurrounding, and myPosition for(long i=0; iact(agentSurrounding.at(convert(tempPt)).at(l).first, agentSurrounding.at(convert(tempPt)).at(l).second, l);//agentSurrounding.at(convert(tempPt)).at(l).second); long point = convert(tempPt); vector tempDir = findDirection(tempPt, actResult.second); //breaks directions into something more useful: Points switch(actResult.first) {//Check which action case Agent::STAY: { //Stay in same position. ie, place the same agent on the same spot in tempGrid tempGrid.at(point).push_back(grid.at(point).at(l)); break; } case Agent::DIE: {//Nothing needs to be placed in the new grid, call delete on agent* delete grid.at(point).at(l); break; } case Agent::MOVE: {//If point is valid, copy the agent to all points stated by direction if(!((tempDir.at(0).x<0 || tempDir.at(0).x>= sizex) || (tempDir.at(0).y<0 || tempDir.at(0).y>= sizey))) tempGrid.at(convert(tempDir.at(0))).push_back(grid.at(point).at(l)); else delete grid.at(point).at(l); break; } case Agent::CLONE: {//Similar to MOVE, but create CLONE instead tempGrid.at(point).push_back(grid.at(point).at(l)); for(unsigned long k=0; k= sizex) || (tempDir.at(k).y<0 || tempDir.at(k).y>= sizey))) tempGrid.at(convert(tempDir.at(k))).push_back(grid.at(point).at(l)->clone()); } break; } } } } } grid=tempGrid; //Save the info into actual Field for(long j=0; j1 to begin, then keep going } } ostream & operator<<(ostream &os, const Field &s) { for(long j=s.sizey-1; j>=0; j--) {//For every point on the grid, starting from top left corner... for(long i=0; i0)//If there are at least one agent there... cout<state();//print the one with highest priority else cout << " "; //If no agents there, print ' ' } cout << endl; } return os; } ostream& operator<<(ostream& C, const Point &p) { C <<"("<