-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathPosition.java
More file actions
200 lines (157 loc) · 4.36 KB
/
Position.java
File metadata and controls
200 lines (157 loc) · 4.36 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package chess;
public record Position(
Column column,
Row row
) {
public Position(final Row row, final Column column) {
this(column, row);
}
public boolean canMoveUp() {
return row.canMoveUp(1);
}
public boolean canMoveUp(final int step) {
return row.canMoveUp(step);
}
public Position moveUp() {
return moveUp(1);
}
public Position moveUp(final int step) {
return new Position(row.moveUp(step), column);
}
public boolean canMoveDown() {
return canMoveDown(1);
}
public boolean canMoveDown(final int step) {
return row.canMoveDown(step);
}
public Position moveDown() {
return moveDown(1);
}
public Position moveDown(final int step) {
return new Position(row.moveDown(step), column);
}
public boolean canMoveLeft() {
return canMoveLeft(1);
}
public boolean canMoveLeft(final int step) {
return column.canMoveLeft(step);
}
public Position moveLeft() {
return moveLeft(1);
}
public Position moveLeft(final int step) {
return new Position(row, column.moveLeft(step));
}
public boolean canMoveRight() {
return canMoveRight(1);
}
public boolean canMoveRight(final int step) {
return column.canMoveRight(step);
}
public Position moveRight() {
return moveRight(1);
}
public Position moveRight(final int step) {
return new Position(row, column.moveRight(step));
}
public boolean canMoveLeftUp() {
return canMoveLeft() && canMoveUp();
}
public Position moveLeftUp() {
return moveLeft().moveUp();
}
public boolean canMoveLeftDown() {
return canMoveLeft() && canMoveDown();
}
public Position moveLeftDown() {
return moveLeft().moveDown();
}
public boolean canMoveRightUp() {
return canMoveUp() && canMoveRight();
}
public Position moveRightUp() {
return moveRight().moveUp();
}
public boolean canMoveRightDown() {
return canMoveRight() && canMoveDown();
}
public Position moveRightDown() {
return moveRight().moveDown();
}
public boolean isTop() {
return row.isTop();
}
public boolean isBottom() {
return row.isBottom();
}
public boolean isFarLeft() {
return column.isFarLeft();
}
public boolean isFarRight() {
return column.isFarRight();
}
public boolean canMove(final Movement movement) {
return canMoveVertical(movement.y()) && canMoveHorizontal(movement.x());
}
public boolean canMoveVertical(final int step) {
if (step > 0) {
return canMoveUp(step);
}
if (step < 0) {
return canMoveDown(-step);
}
return true;
}
public boolean canMoveHorizontal(final int step) {
if (step > 0) {
return canMoveRight(step);
}
if (step < 0) {
return canMoveLeft(-step);
}
return true;
}
public Position move(final Movement movement) {
return moveVertical(movement.y()).moveHorizontal(movement.x());
}
public Position move(final int x, final int y) {
return moveVertical(x).moveHorizontal(y);
}
public Position moveVertical(final int step) {
if (step > 0) {
return moveUp(step);
}
if (step < 0) {
return moveDown(-step);
}
return this;
}
public Position moveHorizontal(final int step) {
if (step > 0) {
return moveRight(step);
}
if (step < 0) {
return moveLeft(-step);
}
return this;
}
/////
public boolean isRowEquals(Position targetPosition) {
return this.row == targetPosition.row;
}
public boolean isColumnEquals(Position targetPosition) {
return this.column == targetPosition.column;
}
public int calculateRowGap(Position targetPosition) {
return this.row.ordinal() - targetPosition.row.ordinal();
}
public int calculateColumnGap(Position targetPosition) {
return this.column.ordinal() - targetPosition.column.ordinal();
}
public int getRow() {
return row.ordinal();
}
public int getColumn() {
return column.ordinal();
}
}