pretty cool

This commit is contained in:
2025-03-31 22:28:08 +02:00
parent 5598d4f5eb
commit 2c6b64fa7d
23 changed files with 363 additions and 24 deletions

View File

@@ -3,8 +3,8 @@ package chess.model;
public enum Direction {
Unset(65),
Front(8), Back(-8), Left(-1), Right(1),
FrontLeft(7), FrontRight(9), BackLeft(-9), BackRight(-7);
Front(-8), Back(8), Left(-1), Right(1),
FrontLeft(-9), FrontRight(-7), BackLeft(7), BackRight(9);
private final int indexOffset;
@@ -16,15 +16,23 @@ public enum Direction {
return indexOffset;
}
public static Direction fromInt(int direction) {
for (Direction dir : Direction.values()) {
if (dir.getIndexOffset() == direction)
return dir;
}
return null;
}
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.Back;
if (diffX < 0 && diffY == 0)
return Direction.Left;
@@ -32,14 +40,14 @@ public enum Direction {
return Direction.Right;
if (diffX < 0 && -diffX == diffY)
return Direction.FrontLeft;
return Direction.BackLeft;
if (diffX > 0 && diffX == diffY)
return Direction.FrontRight;
return Direction.BackRight;
if (diffY < 0 && diffX == diffY)
return Direction.BackLeft;
if (diffY > 0 && diffX == -diffY)
return Direction.BackRight;
return Direction.FrontLeft;
if (diffX > 0 && diffX == -diffY)
return Direction.FrontRight;
return Direction.Unset;
}