// Arup Guha
// 10/28/09
// Example of using the gridcell class.
// This program chooses a random gridcell as the "secret" cell.
// The user must choose a starting gridcell on her search for the secret cell.
// At each step she must choose a direction to move. Upon moving, she'll be 
// told her relative position to the secret cell. She has 20 moves to find it!

import java.util.*;

public class gridcell {
	
	final private static int LASTROW = 10;
	final private static char LASTCOL = 'J';
	final private static int NUMMOVES = 20;
	private int row;
	private char col;
	
	// Sets the grid object to be located at row r and 
	// column c. (5 pts)
	public gridcell(int r, char c) {
		row = r;
		col = c;
	}
	
	// Returns true if and only if this object has the
	// same row and column as g. (10 pts)
	public boolean equals(gridcell g) {
		 return this.row == g.row && this.col == g.col;
	}

	public boolean eastOf(gridcell g) {
		return (this.col > g.col);
	}

	// Returns true if and only if this object is north
	// of g. (5 pts)
	public boolean northOf(gridcell g) {
		return this.row < g.row;
	}
	
     // If this object is not on the southern most row
	// of the grid, it is moved one unit south and true
	// is returned. Otherwise, no movement is made to this
	// and false is returned. (15 pts)
	public boolean moveSouthOneUnit() {
		
		// Move if we can.
		if (row < LASTROW) {
			row++;
			return true;
		}
		return false; // If we get here, we can't move south.
	}
	
	public boolean moveNorthOneUnit() {
		
		// Move if we can.
		if (row > 1) {
			row--;
			return true;
		}
		return false; // If we get here, we can't move north.
	}
	
	public boolean moveEastOneUnit() {
		
		// Move if we can.
		if (col < LASTCOL) {
			col++;
			return true;
		}
		return false; // If we get here, we can't move east.
	}
	
	public boolean moveWestOneUnit() {
		
		// Move if we can.
		if (col > 'A') {
			col--;
			return true;
		}
		return false; // If we get here, we can't move east.
	}
		
	// Returns the distance between this and g. The 
	// distance is in units based on row and col.
	// For example, the distance between (2, 'G') and
	// (5, 'C') is 5, since they are 3 rows and 4 columns
	// apart. (15 pts)
	public double distance(gridcell g) {
		return Math.sqrt(Math.pow(this.row-g.row,2)+Math.pow(this.col-g.col,2));
	}
	
	// Returns a string representation of this object.
	// As an example, (1,A) is how a grid cell in row 1
	// column A should be represented. (5 pts)
	public String toString() {
		return "("+row+", "+col+")";
	}	
		
	public static void main(String[] args) throws Exception {
		
		// Create a random grid cell.
		Random r = new Random();
		int row = Math.abs(r.nextInt()%10) + 1;
		char col = (char)('A'+Math.abs(r.nextInt()%10));
		gridcell secret = new gridcell(row, col);
		
		// Get a starting point.
		Scanner stdin = new Scanner(System.in);
		System.out.println("Where do you want to start your search, enter row(1-10) followed by a space and then col(A-J)");
		int my_row = stdin.nextInt();
		char my_col = stdin.next().charAt(0);
		gridcell me = new gridcell(my_row, my_col);
		
		int nummoves = 0;
		
		// Keep on going until the user finds the secret point or the user takes 20 moves.
		while (nummoves < gridcell.NUMMOVES && !me.equals(secret)) {
			
			// Get user direction.
			System.out.println("Enter the direction you want to move. (1=North, 2=East, 3=South, 4=West)");
			int answer = stdin.nextInt();
			
			// Process North.
			if (answer == 1) {
				if (me.moveNorthOneUnit())
					System.out.println("You have moved to "+me);
				else
					System.out.println("Sorry, you can not move north now.");
			}
			
			// Process East.
			else if (answer == 2) {
				if (me.moveEastOneUnit())
					System.out.println("You have moved to "+me);
				else
					System.out.println("Sorry, you can not move east now.");
			}
			
			// Process South.
			else if (answer == 3) {
				if (me.moveSouthOneUnit())
					System.out.println("You have moved to "+me);
				else
					System.out.println("Sorry, you can not move south now.");
			}
			
			// Process West.
			else {
				if (me.moveWestOneUnit())
					System.out.println("You have moved to "+me);
				else
					System.out.println("Sorry, you can not move west now.");
			}
			
			// Break out to avoid unnecessary directional message.
			if (me.equals(secret))
				break;
			
			// Give N-S bearing.
			if (me.northOf(secret))
				System.out.println("You are north of your target.");
			else
				System.out.println("You are NOT north of your target.");
				
			// Give E-W bearing.
			if (me.eastOf(secret))
				System.out.println("You are east of your target.");
			else
				System.out.println("You are NOT east of your target.");
				
			nummoves++;
		}
		
		// Print out winning message.
		if (me.equals(secret))
			System.out.println("Congrats on winning in "+nummoves+" number of moves.");
		
		// Losing message...
		else
			System.out.println("Sorry, you didn't find the target. It was "+secret+".");
	}
}
