move: mandatory capture
This commit is contained in:
parent
7d26dca752
commit
8affa353da
@ -21,7 +21,7 @@ public class CheckersMoveTest {
|
|||||||
Map.entry(2, Arrays.asList(6, 7, 9, 11)),
|
Map.entry(2, Arrays.asList(6, 7, 9, 11)),
|
||||||
Map.entry(3, Arrays.asList(7, 8, 10, 12)),
|
Map.entry(3, Arrays.asList(7, 8, 10, 12)),
|
||||||
Map.entry(4, Arrays.asList(8, 11)),
|
Map.entry(4, Arrays.asList(8, 11)),
|
||||||
Map.entry(5, Arrays.asList(1, 9, 14, 9, 11)),
|
Map.entry(5, Arrays.asList(1, 9, 14)),
|
||||||
Map.entry(6, Arrays.asList(1, 2, 9, 10, 13, 15)),
|
Map.entry(6, Arrays.asList(1, 2, 9, 10, 13, 15)),
|
||||||
Map.entry(7, Arrays.asList(2, 3, 10, 11, 14, 16)),
|
Map.entry(7, Arrays.asList(2, 3, 10, 11, 14, 16)),
|
||||||
Map.entry(8, Arrays.asList(3, 4, 11, 12, 15)),
|
Map.entry(8, Arrays.asList(3, 4, 11, 12, 15)),
|
||||||
|
@ -110,6 +110,22 @@ public class GameBoardTests {
|
|||||||
assertThat(m2.board().get(15)).isEqualTo(BLACK_MAN);
|
assertThat(m2.board().get(15)).isEqualTo(BLACK_MAN);
|
||||||
assertThat(m2.moveNumber() == 2);
|
assertThat(m2.moveNumber() == 2);
|
||||||
assertThat(m2.status()).isEqualByComparingTo(Status.GAME_BOARD_WAIT_FOR_OPPONENT);
|
assertThat(m2.status()).isEqualByComparingTo(Status.GAME_BOARD_WAIT_FOR_OPPONENT);
|
||||||
|
|
||||||
|
assertThatThrownBy(() -> {
|
||||||
|
cordaClient.gameBoardMove(hiWhite, game.uuid(), move(24, 19),
|
||||||
|
"An attempt to ignore mandatory capture");
|
||||||
|
});
|
||||||
|
|
||||||
|
final var m3 = cordaClient.gameBoardMove(hiWhite, game.uuid(), move(18, 11), null);
|
||||||
|
assertThat(m2.board().get(18)).isEqualTo(WHITE_MAN);
|
||||||
|
assertThat(m2.board().get(11)).isNull();
|
||||||
|
assertThat(m2.board().get(15)).isEqualTo(BLACK_MAN);
|
||||||
|
assertThat(m3.board().get(11)).isEqualTo(WHITE_MAN);
|
||||||
|
assertThat(m3.board().get(18)).isNull();
|
||||||
|
assertThat(m3.board().get(15)).isNull();
|
||||||
|
assertThat(m3.moveNumber() == 3);
|
||||||
|
assertThat(m3.status()).isEqualByComparingTo(Status.GAME_BOARD_WAIT_FOR_OPPONENT);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ArrayList<Integer> move(int from, int to) {
|
ArrayList<Integer> move(int from, int to) {
|
||||||
|
@ -42,7 +42,9 @@ public class Move {
|
|||||||
if (!allMoves.contains(this))
|
if (!allMoves.contains(this))
|
||||||
throw new Exception("Prohibited move");
|
throw new Exception("Prohibited move");
|
||||||
|
|
||||||
// TODO: check for mandatory captures
|
final Set<Move> mandatoryJumps = getMandatoryJumps(board, expectedMoveColor);
|
||||||
|
if (mandatoryJumps.size() != 0 && !mandatoryJumps.contains(this))
|
||||||
|
throw new Exception("A mandatory opponent's stone capture must be performed");
|
||||||
|
|
||||||
movingStone = movingStone.promoteIfPossible(to);
|
movingStone = movingStone.promoteIfPossible(to);
|
||||||
if (board.put(to, movingStone) != null)
|
if (board.put(to, movingStone) != null)
|
||||||
@ -66,7 +68,7 @@ public class Move {
|
|||||||
|
|
||||||
intersection.retainAll(b);
|
intersection.retainAll(b);
|
||||||
if (intersection.size() != 1)
|
if (intersection.size() != 1)
|
||||||
// A legit move is characterized by single intersection point
|
// A legit move is characterized by a single intersection point
|
||||||
throw new Exception("Prohibited move");
|
throw new Exception("Prohibited move");
|
||||||
|
|
||||||
return intersection.iterator().next();
|
return intersection.iterator().next();
|
||||||
@ -113,7 +115,7 @@ public class Move {
|
|||||||
Map.entry(2, Arrays.asList(6, 7, 9, 11)),
|
Map.entry(2, Arrays.asList(6, 7, 9, 11)),
|
||||||
Map.entry(3, Arrays.asList(7, 8, 10, 12)),
|
Map.entry(3, Arrays.asList(7, 8, 10, 12)),
|
||||||
Map.entry(4, Arrays.asList(8, 11)),
|
Map.entry(4, Arrays.asList(8, 11)),
|
||||||
Map.entry(5, Arrays.asList(1, 9, 14, 9, 11)),
|
Map.entry(5, Arrays.asList(1, 9, 14)),
|
||||||
Map.entry(6, Arrays.asList(1, 2, 9, 10, 13, 15)),
|
Map.entry(6, Arrays.asList(1, 2, 9, 10, 13, 15)),
|
||||||
Map.entry(7, Arrays.asList(2, 3, 10, 11, 14, 16)),
|
Map.entry(7, Arrays.asList(2, 3, 10, 11, 14, 16)),
|
||||||
Map.entry(8, Arrays.asList(3, 4, 11, 12, 15)),
|
Map.entry(8, Arrays.asList(3, 4, 11, 12, 15)),
|
||||||
@ -149,6 +151,31 @@ public class Move {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Set<Move> getMandatoryJumps(Map<Integer, Stone> board, Stone.Color color) {
|
||||||
|
Set<Move> res = new HashSet<Move>();
|
||||||
|
|
||||||
|
for (Map.Entry<Integer, Stone> entry : board.entrySet()) {
|
||||||
|
final Integer from = entry.getKey();
|
||||||
|
final Stone stone = entry.getValue();
|
||||||
|
if (stone.getColor() != color)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
final Stone.Color opponentsColor = color.opposite();
|
||||||
|
final Set<Move> allJumps = stone.getJumps(from);
|
||||||
|
|
||||||
|
allJumps.removeIf( j -> {
|
||||||
|
if (board.get(j.to) != null)
|
||||||
|
return true; // must be an empty jumpTo tile
|
||||||
|
final Stone jumpOverStone = board.get(j.jumpOver);
|
||||||
|
return jumpOverStone == null || jumpOverStone.getColor() != opponentsColor;
|
||||||
|
});
|
||||||
|
|
||||||
|
res.addAll(allJumps);
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
final int prime = 31;
|
final int prime = 31;
|
||||||
|
Loading…
Reference in New Issue
Block a user