-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPosition.java
More file actions
66 lines (56 loc) · 1.63 KB
/
Position.java
File metadata and controls
66 lines (56 loc) · 1.63 KB
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
import java.util.Scanner;
import java.io.PrintWriter;
/** Represents a location determined by a row and column on the map
*
*/
public class Position {
private int row;
private int col;
public Position() {
row = 0;
col = 0;
}
public Position(int row, int col) {
this.row = row;
this.col = col;
}
@Override
public boolean equals(Object other) {
Position op = (Position) other;
// they are equal when both coordinates match
return this.row == op.row && this.col == op.col;
}
// returns whether a position is adjacent to another (or equal)
public boolean isAdjacent(Position other) {
int rowdiff = Math.abs(this.row - other.row);
int coldiff = Math.abs(this.col - other.col);
//returns a boolean whether this is true or false
return rowdiff + coldiff < 2;
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
/** Writes the row and column of the entity's position to the save file
*
* @param out the printwriter used to write data to a file
*/
public void save(PrintWriter out) {
out.println(row);
out.println(col);
}
/** A constructor used for reading in the row and column of the entity's position from the save file
*
* @param in the scanner used to read in data from the file
*/
public Position(Scanner in) {
row = in.nextInt();
//skipping the rest of the line
in.nextLine();
col = in.nextInt();
//skipping the rest of the line
in.nextLine();
}
}