// constructor gets a reference to the world state public Grid(ControllerWorldState ws, double WorldWidth, double WorldHeight, int numX, int numY) { this.prevLocations = new Dictionary<Robot, Coordinates>(); this.prevLocationsForMark = new Dictionary<Robot, Coordinates>(); this.WorldWidth = WorldWidth; this.WorldHeight = WorldHeight; this.NumSquaresX = numX; this.NumSquaresY = numY; cellData = new CellData[NumSquaresX, NumSquaresY]; for (int i = 0; i < NumSquaresX; i++) { for (int j = 0; j < NumSquaresY; j++) { cellData[i, j] = new CellData(i,j); } } }
// public void Mark(Robot robot, bool continuous){ // if(!continuous || !prevLocationsForMark.ContainsKey(robot)){ // getGridLoc(robot.Location).pheromoneLevel++; // return; // } // Coordinates newLoc = new Coordinates(robot.Location.X, robot.Location.Y); // Coordinates prevLoc = new Coordinates(prevLocationsForMark[robot].X, prevLocations[robot].Y); // Coordinates curLoc = new Coordinates(prevLocationsForMark[robot].X, prevLocations[robot].Y); // Coordinates startLoc = new Coordinates(prevLocationsForMark[robot].X, prevLocations[robot].Y); // // Mark continuously with interpolation // // Assume previous location is already marked // //Console.WriteLine("In Mark:: new location is for " + newLoc.X + ", " + newLoc.Y); // CellData finalSpot = getGridLoc(newLoc); // CellData startSpot = getGridLoc(startLoc); // //Console.WriteLine("In Mark:: destination grid coordinates is " + finalSpot.row + ", " + finalSpot.col); // //Console.WriteLine("In Mark: Getting grid loc current spot for " + curLoc.X + "," + curLoc.Y); // CellData curSpot = getGridLoc(curLoc); // //Console.WriteLine("In Mark:: old grid coordinates is " + finalSpot.row + ", " + finalSpot.col); // if (finalSpot.row == curSpot.row && finalSpot.col == curSpot.col) // { // // don't mark anything // return; // } // double incr = (double)Math.Min((WorldHeight / NumSquaresY), (WorldWidth / NumSquaresX)) / 5.0; // double dX = robot.Location.X - prevLocationsForMark[robot].X; // double dY = robot.Location.Y - prevLocationsForMark[robot].Y; // double incrX; // double incrY; // if (dX != 0 || dY != 0) // { // incrX = incr * dX / (double)Math.Sqrt(dX * dX + dY * dY); // incrY = incr * dY / (double)Math.Sqrt(dX * dX + dY * dY); // } // else // { // // 0 distance // incrX = 0; // incrY = 0; // } // //Console.WriteLine("Incrx is " + incrX + " and incrY is " + incrY); //// Console.WriteLine("Start spot: " + curSpot.col + " " + curSpot.row); // // Console.WriteLine("End spot: " + finalSpot.col + " " + finalSpot.row); // do // { // CellData nextSpot; // //Console.WriteLine("In Mark: Before incrementing, we were at location " + curLoc.X + ", " + curLoc.Y + " trying to reach " + newLoc.X + ", " + newLoc.Y); // //Console.WriteLine("In Mark: We are at grid location " + curSpot.row + ", " + curSpot.col + " trying to reach " + finalSpot.row + ", " + finalSpot.col); // // Increment X and Y // curLoc.X += incrX; // curLoc.Y += incrY; // //Console.WriteLine("In Mark: After incrementing, we are at location " + curLoc.X + ", " + curLoc.Y + " trying to reach " + newLoc.X + ", " + newLoc.Y); // // Get next grid spot // nextSpot = getGridLoc(curLoc); // // Console.WriteLine("Current spot: " + nextSpot.col + " " + nextSpot.row); // // If different mark it // if (nextSpot != curSpot) // { // nextSpot.pheromoneLevel++; // curSpot = nextSpot; // } // //if (!inBounds(curSpot, startSpot, finalSpot)) // //{ // // break; // //} // } while (curSpot.row != finalSpot.row || curSpot.col != finalSpot.col); // return; // } public bool inBounds(CellData currentLocation, CellData startLocation, CellData finalLocation) { int px = currentLocation.col; int py = currentLocation.row; int maxx = Math.Max(startLocation.col, finalLocation.col); int maxy = Math.Max(startLocation.row, finalLocation.row); int minx = Math.Min(startLocation.col, finalLocation.col); int miny = Math.Min(startLocation.row, finalLocation.row); return (px <= maxx && px >= minx && py <= maxy && py >= miny); }