-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocation.java
143 lines (129 loc) · 3.38 KB
/
Location.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//Yuening Huang
public class Location
{
private int row;
private int col;
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;
public Location(int inRow, int inCol)
{
row= inRow;
col= inCol;
}
public int getRow()
{
return row;
}
public int getCol()
{
return col;
}
//----------------------------------------------------------------------
//Description: Gets the adjacent location in any one of the eight
// compass directions.
// It is possible to return a Location that is out of bounds.
//Precondition: dir is one of the 8 compass directions.
//Postcondition: returns the adjacent location in the direction that is closest to dir
public Location getAdjacentLoc(int dir)
{
if(dir==NORTH)
{
return new Location (row-1, col);
}
else if (dir==EAST)
{
return new Location(row, col+1);
}
else if (dir==SOUTH)
{
return new Location(row+1, col);
}
else if (dir==WEST)
{
return new Location(row, col-1);
}
else if (dir==NORTHEAST)
{
return new Location(row-1, col+1);
}
else if (dir==SOUTHEAST)
{
return new Location(row+1, col+1);
}
else if (dir==NORTHWEST)
{
return new Location(row-1, col-1);
}
else
{
return new Location(row+1, col-1);
}
}
//----------------------------------------------------------------------
//Description: Returns the direction from this location toward another
// location. The direction is one of the eight compass
// directions.
//Precondition: target - a location that is different from this location.
// target is an adjacent Location.
// target is valid in the matrix
//Postcondition: returns the closest compass direction from this location
// toward target
public int getDirectionToward(Location target)
{
if(target.getCol()==col)
{
if(target.getRow()<row)
return NORTH;
else if (target.getRow()>row)
return SOUTH;
}
if(target.getRow()==row)
{
if (target.getCol()<col)
return WEST;
else if (target.getCol()>col)
return EAST;
}
if(target.getCol()<col)
{
if(target.getRow()<row)
return NORTHWEST;
else if (target.getRow()>row)
return SOUTHWEST;
}
if (target.getCol()>col)
{
if(target.getRow()<row)
return NORTHEAST;
else if(target.getRow()>row)
return SOUTHEAST;
}
return NORTH;
}
//----------------------------------------------------------------------
//override .equals method
//Description: Determines if this Location is equal to otherLoc
//Precondition: otherLoc is a Location object and is valid in the matrix
//Postcondition: return true if otherLoc is a Location with the same row
// and column as this location; false otherwise.
public boolean equals(Object otherLoc)
{
if(row== ((Location)otherLoc).getRow() && col== ((Location)otherLoc).getCol())
return true;
else
return false;
}
//----------------------------------------------------------------------
//Postcondition: returns a String with the row and col of this location
// in the format: (row, col)
public String toString()
{
return "("+row +", "+col +")";
}
}