001    package aima.basic;
002    
003    /**
004     * @author Ravi Mohan
005     * 
006     */
007    public class XYLocation {
008            int xCoOrdinate, yCoOrdinate;
009    
010            public XYLocation(int i, int j) {
011                    xCoOrdinate = i;
012                    yCoOrdinate = j;
013            }
014    
015            public int getXCoOrdinate() {
016                    return xCoOrdinate;
017            }
018    
019            @Override
020            public boolean equals(Object o) {
021                    XYLocation anotherLoc = (XYLocation) o;
022                    return ((anotherLoc.getXCoOrdinate() == xCoOrdinate) && (anotherLoc
023                                    .getYCoOrdinate() == yCoOrdinate));
024            }
025    
026            public int getYCoOrdinate() {
027                    return yCoOrdinate;
028            }
029    
030            public XYLocation west() {
031                    return new XYLocation(xCoOrdinate - 1, yCoOrdinate);
032            }
033    
034            public XYLocation east() {
035                    return new XYLocation(xCoOrdinate + 1, yCoOrdinate);
036            }
037    
038            public XYLocation north() {
039                    return new XYLocation(xCoOrdinate, yCoOrdinate - 1);
040            }
041    
042            public XYLocation south() {
043                    return new XYLocation(xCoOrdinate, yCoOrdinate + 1);
044            }
045    
046            public XYLocation right() {
047                    return east();
048            }
049    
050            public XYLocation left() {
051                    return west();
052            }
053    
054            public XYLocation up() {
055                    return north();
056            }
057    
058            public XYLocation down() {
059                    return south();
060            }
061    
062            public XYLocation locationAt(String direction) {
063                    if (direction.equals("North")) {
064                            return north();
065                    }
066                    if (direction.equals("South")) {
067                            return south();
068                    }
069                    if (direction.equals("East")) {
070                            return east();
071                    }
072                    if (direction.equals("West")) {
073                            return west();
074                    } else {
075                            throw new RuntimeException("Unknown direction " + direction);
076                    }
077            }
078    
079            @Override
080            public String toString() {
081                    return " ( " + xCoOrdinate + " , " + yCoOrdinate + " ) ";
082            }
083    
084            @Override
085            public int hashCode() {
086                    int result = 17;
087                    result = 37 * result + xCoOrdinate;
088                    result = result + yCoOrdinate;
089                    return result;
090            }
091    
092    }