Presentation is loading. Please wait.

Presentation is loading. Please wait.

Location Class Gridworld. Location Class Encapsulates the coordinates for an actor’s position in a grid – Row and Column number Rows increase going down.

Similar presentations


Presentation on theme: "Location Class Gridworld. Location Class Encapsulates the coordinates for an actor’s position in a grid – Row and Column number Rows increase going down."— Presentation transcript:

1 Location Class Gridworld

2 Location Class Encapsulates the coordinates for an actor’s position in a grid – Row and Column number Rows increase going down Columns increase going right – Also has methods that determine relationships between locations and compass directions – Every actor has a direction 0 for north, 45 is northeast, … – Location class also has eight constants that specify the constant direction NORTH, NORTHEAST, …

3 Location Class public Location(int r, int c) constructs a location with row r and column c public int getRow() returns the row of this location public int getCol() returns the column of this location public Location getAdjacentLocation(int direction) returns the adjacent location in the compass direction that is closest to direction public int getDirectionToward(Location target) returns the closest compass direction from this location toward target Testable API

4 Location Class return methods public boolean equals(Object other) returns true if other is a Location object with the same row and column values as this location, and false otherwise public int hashCode() returns a hash code (unique key) for this location public int compareTo(Object otherObject) returns a negative integer if this location is less than other (if the row is less than other.row or if the rows are equal and column is less than other.column), zero if the two locations are equal, or a positive integer if this location is greater than other. Locations are ordered in row-major order public String toString() returns a string with the row and the column of this location, in the format (row, col) Testable API

5 Location Class Compass directions: public static final int NORTH = 0; public static final int EAST = 90; public static final int SOUTH = 180; public static final int WEST = 270; public static final int NORTHEAST = 45; public static final int SOUTHEAST = 135; public static final int SOUTHWEST = 225; public static final int NORTHWEST = 315;

6 Location Class Turn angles: public static final int LEFT = -90; public static final int RIGHT = 90; public static final int HALF_LEFT = -45; public static final int HALF_RIGHT= 45; public static final int FULL_CIRCLE = 360; public static final int HALF_CIRCLE = 180 ; public static final int AHEAD = 0;

7 Examples Location loc1 = new Location(5,7); Location locOrig = new Location(0,0); Location loc2 = loc1.getAdjacentLocation(Location.WEST); // (5,6) Location loc3 = loc1.getAdjacentLocaton(Location.NORTHEAST); // (4,8) Location loc4 = locOrig.getAdjacentLocaton(Location.WEST); // (0,-1) int dir = loc1.getDirectionToward(new Location(6,8)); // 135

8 Turning by an Amount You may have been using multiple calls to the turn() method to turn an Actor – We can use setDirection and Location constants for this instead – setDirection(getDirection() + Location.HALF_RIGHT); // turn 45 degrees

9 Exercise Set 3 Location loc1 = new Location(4, 3); Location loc2 = new Location(3, 4); 1.How would you access the row value of loc1 ? loc1.getRow() 2.What is the value of b after the following statement is executed? boolean b = loc1.equals(loc2); false 3.What is the value of loc3 after the following? Location loc3 = loc2.getAdjacentLocation(Location.SOUTH); (4, 4) 4.What is the value of dir after the following? int dir = loc1.getDirectionToward(new Location(6,1)); 225 5.How does the getAdjacentLocation method know which location to return? It first takes the direction passed to it and converts it to the closest 45 degree direction. Then it looks in that direction to return the adjacent location


Download ppt "Location Class Gridworld. Location Class Encapsulates the coordinates for an actor’s position in a grid – Row and Column number Rows increase going down."

Similar presentations


Ads by Google