Translator Code





/**
 * Translator.java
 *
 *
 * Created: Mon Nov 29 18:32:22 2004
 *
 * @author Jason M. Bindewald
 * @version 1.0
 */

public class Translator {

    /**
     * variable map - a 2D int array keeping track of 
     * all of the intersections and their adjacent intersections. */
    int map[][];
    
    /**
     * Creates a Translator instance.  Translator takes 
     * in a 2D int array.  This array is of size [x][4] where x is 
     * the number of intersections and the second dimension of the 
     * array keeps track of which intersections have spokes which 
     * directly lead to it.  The intersections are divided into 
     * quadrants [0,90), [90,180), [180,270), and [270, 360).  If 
     * there is no spoke going into a given quadrant it is 
     * represented as -1. For example, the following map:
     *
     *  __8_____
     * /  |     \
     * |  |     |
     * 4--x-----2
     *
     * would be represented as:
     *
     * map[x][0] = 8, map[x][1] = 2, map[x][2] = -1, map[x][3] = 4:
     *
     *
     * @param _map an int[][] value */
    public Translator(int[][] _map) {
	map = _map;
    }
    
    /**
     * translate - This method takes in an 
     * intersection that the robot will be considered coming from 
     * and will take in a goal path, with that path in mind it will 
     * then translate the path into a set of turns:  straight=1, 
     * right=2, back=3, left=4, goal reached = 0.
     *  
     * At this point there must be an assumption that there are no two 
     * paths which have the same start and end points
     *
     * @param startFrom an int value - intersection from 
     * which the current search is starting.
     * @param goalPath an int[] value - the path of 
     * intersections to be translated into turn form.
     * @return an int[] value - th array of turns */
    public int[] translate(int startFrom, int[] goalPath) {

	//The intersection from which you are travelling
	int comingFrom = startFrom;
	//The intersection to which you are going 
	int goingTo = goalPath[0]; 
	/**This will keep track of all of the turns that will take place at 
	   each of the consecutive intersections**/
	int[] turns = new int[goalPath.length];

	//Traverse the goal path and find the correct turn directions
	for(int i = 0; i < goalPath.length - 1; i++) {
	    
	    boolean found = false;  /**Have we found the spoke corresponding to
				       the direction from which we are coming, 
				       on the intersection we are going to?**/
	    int x = -1;             //Count which spoke it is

	    //Figure out which direction you are coming in from
	    while(!found) { 
		x++;
		if(map[goingTo][x] == comingFrom)
		    found = true;
		else if(x == 3) {
		    System.exit(0);
		}
	    }
	    /**Allign the looker so the first spoke it sees is the one straight
	       in front of it**/
	    x -= 2;
	    //x-= 1;
	    if(x < 0) x +=4;	    

	    //figure out where you want to turn from here
	    comingFrom = goingTo;
	    goingTo = goalPath[i + 1];
	    
	    //has it found the spoke that we want to get to next?
	    boolean turned = false; 
	    int count = 0;          //count which spoke we're currently at
	    while(!turned) {
		//increment a spoke that it will see
		if(map[comingFrom][x] != -1) count++;
		//if this is the spoke we want, stop incrementing
		if(map[comingFrom][x] == goingTo) { 
		    turned = true; 
		    turns[i] = count; //set the number of spokes to count
		}
		/**if it is not the spoke we want, increment to the next spoke 
		   in the order**/
		else {            
		    x++;
		    if(x == 4) x = 0;
		}
	    }  
	}
	turns[turns.length - 1] = 1;
	
	return turns;	
    }

    public static void main(String[] args) {
	//This is just a bunch of test code to run a simple search
	int[][] map = new int[11][4];
	map[0][0] = 1;  map[0][1] = -1; map[0][2] = 8;  map[0][3] = 4;
	map[1][0] = 2;  map[1][1] = -1; map[1][2] = 0;  map[1][3] = 5;
	map[2][0] = 3;  map[2][1] = -1; map[2][2] = 1;  map[2][3] = 5;	
	map[3][0] = 7;  map[3][1] = -1; map[3][2] = 2;  map[3][3] = 6;
	map[4][0] = 5;  map[4][1] = 0;  map[4][2] = -1; map[4][3] = 8;
	map[5][0] = 2;  map[5][1] = 1;  map[5][2] = 4;  map[5][3] = 9;
	map[6][0] = 7;  map[6][1] = 3;  map[6][2] = -1; map[6][3] = 10;
	map[7][0] = -1; map[7][1] = 3;  map[7][2] = 6;  map[7][3] = 10;
	map[8][0] = 9;  map[8][1] = 4;  map[8][2] = 0;  map[8][3] = -1;
	map[9][0] = 10; map[9][1] = 5;  map[9][2] = 8;  map[9][3] = -1;
	map[10][0] = 7; map[10][1] = 6; map[10][2] = 9; map[10][3] = -1;

	int comeFrom = 0;
	int start = 4;
	int goal = 7;
	SearchTest st = new SearchTest(map, start, goal);
	int[] path = st.findit();
	Translator trans = new Translator(map);
	int[] goalPath = trans.translate(comeFrom, path);
    }
} // Translator