/************************************ * Umass Amherst * CS377 Operating System * Lab2: * Author: Qi Li, Andrew Fagg * Date: 9/22/2002 * * Class in this file: Abstract_Robot * Compile: javac Abstract_Robot.java * Run: n/a ***********************************/ import java.util.*; import java.lang.*; /*********************************** * ClassName: Abstract_Robot * SuperClass: Thread * Description: Beginnings of the Robot Thread * Methods: * GetPosX() GetPosY(): get the robot position * SetPosX() SetPosY(): set the robot position * SetDist(): Set the Dist array * initDist(): Create the distance array. Must be called * before SetDist() is called. * getNofEat(): Return the number of edible goals * * Methods required to be implemented: * run(): Top level thread code * ************************************/ abstract class Abstract_Robot extends Thread { private int X; //the robot's X Position private int Y; //the robot's Y Position char cColor; //the Robot 's Color int id; //Identity of the robot Map_Data myGrid; // Refer to the Map_Data in which the robot lives public void setColor(char inC){ cColor=inC ;} //set color to the robots int nMaxX,nMaxY; // the size of map, which is equal to nSizeX,nSizeY in Map protected int Dist[][]; //The Distance Array of this Robot. The attribte is protected ,so // it can only be accessed by the class method and its subclass method. int nEatable; // the number of eatable goals. boolean bDead=false; // False if the Robot is alive final int GetPosX(){return X;} //get x final int GetPosY(){return Y;} //get y final char GetColor(){return cColor;} //get color // Set the X-coordinate of the robot final void SetPosX(int newx) { // Do not remove this sleep try { sleep(1); } catch(InterruptedException e){}; X = newx; }; // Set the Y-coordinate of the robot final void SetPosY(int newy) { // Do not remove this sleep try { sleep(1); } catch(InterruptedException e){}; Y = newy; }; /////////////////////////////////// // Fill in the Dist[][] array. One of: // MAX_VALUE if an obstacle or the location of a dead robot // 0 if a goal that is edible for this robot (as long as it is not // covered by a dead robot). // -1 if a goal is not reachable for the robot // # number of grid squares away from the closest edible goal location // // // ASSUMPTIONS: // - Dead Robots will stay dead (so no synchronization is necessary) // - Goals do not move (so reading goal state does not require synchronization) // /////////////////////////////// public void SetDist() { // Set all the obstacles to MAX_VALUE in distance array; every other cell to -1. for (int i=0;iDist[i][j+1]) minVal=Dist[i][j+1]; } if (i-1>=0){ if ((Dist[i-1][j]!=-1)&&(Dist[i-1][j]Dist[i-1][j]) minVal=Dist[i-1][j]; } if (j-1>=0){ if ((Dist[i][j-1]!=-1)&&(Dist[i][j-1]Dist[i][j-1]) minVal=Dist[i][j-1]; } if (minVal==Integer.MAX_VALUE) return Dist[i][j]; else { return minVal+1; } } ////////////////////////////////////////////////////////////////////// // Get the number of current edible goals by this color robot ///////////////////////////////////////// public int getNofEat(){ int nCanEat=0; for (int i=0;i