basic model
This commit is contained in:
46
app/src/main/java/chess/model/Direction.java
Normal file
46
app/src/main/java/chess/model/Direction.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package chess.model;
|
||||
|
||||
public enum Direction {
|
||||
|
||||
Unset(65),
|
||||
Front(8), Back(-8), Left(-1), Right(1),
|
||||
FrontLeft(7), FrontRight(9), BackLeft(-9), BackRight(-7);
|
||||
|
||||
private final int indexOffset;
|
||||
|
||||
Direction(int indexOffset) {
|
||||
this.indexOffset = indexOffset;
|
||||
}
|
||||
|
||||
public int getIndexOffset() {
|
||||
return indexOffset;
|
||||
}
|
||||
|
||||
public static Direction findDirection(Move move) {
|
||||
assert move.isValid() : "Move is invalid!";
|
||||
int diffX = move.getFinish().getX() - move.getStart().getX();
|
||||
int diffY = move.getFinish().getY() - move.getStart().getY();
|
||||
|
||||
if (diffX == 0 && diffY < 0)
|
||||
return Direction.Back;
|
||||
if (diffX == 0 && diffY > 0)
|
||||
return Direction.Front;
|
||||
|
||||
if (diffX < 0 && diffY == 0)
|
||||
return Direction.Left;
|
||||
if (diffX > 0 && diffY == 0)
|
||||
return Direction.Right;
|
||||
|
||||
if (diffX < 0 && -diffX == diffY)
|
||||
return Direction.FrontLeft;
|
||||
if (diffX > 0 && diffX == diffY)
|
||||
return Direction.FrontRight;
|
||||
|
||||
if (diffY < 0 && diffX == diffY)
|
||||
return Direction.BackLeft;
|
||||
if (diffY > 0 && diffX == -diffY)
|
||||
return Direction.BackRight;
|
||||
|
||||
return Direction.Unset;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user