/* class name: Abstract_Distance * * Provides a distance matrix for the robots of * a particular color. Employs dynamic programming * to produce this matrix. * * public components * Dist[][] - the distance matrix * sem - a semaphore for triggering the computation * semDone - a semaphore for indicating that a computation pass * has completed */ abstract class Abstract_Distance extends Thread { public int Dist[][]; //The Distance Array for robots with gColor. public Semaphore sem; // Use to trigger the distance computation public Semaphore semDone; // Use to indicate that the first distance // computation has completed private int nMaxRow,nMaxCol; // The size of map, which is equal to nSizeRow,nSizeCol in Map private int DistTemp[][]; // Scratch space in which to do our work private char cColor; // The color of the goals for which we private Map_Data myGrid; // Refer to the Map_Data in which the robot lives /** public Abstract_Distance(char color,Map_Data m_Grid, int Row, int Col) */ public Abstract_Distance(char color,Map_Data m_Grid, int Row, int Col) { cColor = color; //get the group color myGrid = m_Grid; //get the refered map nMaxRow = Row; nMaxCol = Col; } /** final char getColor() Return the color for this distance matrix. */ final char getColor() { return cColor; } /** public void initDist() Create the space for the distance matrices Assumptions: - nMaxRow and nMaxCol have already been set */ public void initDist(){ Dist = new int[nMaxRow][nMaxCol]; DistTemp = new int[nMaxRow][nMaxCol]; } /** private int fromNeighbor(int i,int j) Perform one dynamic programming step for cell : Set this cell's value to be 1+ the minimum value of the neighbors. Neighbor cells with MAX_VALUE or -1 are not included in the computation. */ private int fromNeighbor(int i,int j) //i: row position; j: col position { int minVal = Integer.MAX_VALUE; if (i+1DistTemp[i][j+1]) minVal=DistTemp[i][j+1]; } if (i-1>=0){ if ((DistTemp[i-1][j]!=-1)&&(DistTemp[i-1][j]DistTemp[i-1][j]) minVal=DistTemp[i-1][j]; } if (j-1>=0){ if ((DistTemp[i][j-1]!=-1)&&(DistTemp[i][j-1]DistTemp[i][j-1]) minVal=DistTemp[i][j-1]; } if (minVal==Integer.MAX_VALUE) return DistTemp[i][j]; else { return minVal+1; } } /* * Fill in the Dist[][] array. Cells are set to: * MAX_VALUE: if it is an obstacle or * the other color goals, or * the location of a dead robot * 0: if it is a goal that is edible for this robot. * -1: if no goal is reachable by the robot * d > 0: where d is the distance to the closest goal * * ASSUMPTIONS: * - Dead Robots will stay dead (so no synchronization is necessary) * - Goals do not move (so reading goal state does not require synchronization) * */ private void SetDist() { /* Set all the obstacles to MAX_VALUE in distance array; * Set all other color goals except black goals as obstacles; * otherwise, set to -1. */ for (int i=0;i