From a9ffd4b0b96082d0620b91d64b1047510f980938 Mon Sep 17 00:00:00 2001 From: djmil Date: Wed, 27 Sep 2023 17:25:30 +0200 Subject: [PATCH] Checkers move rules improved --- .../djmil/cordacheckers/CheckersTest.java | 90 ++++++++++--------- 1 file changed, 48 insertions(+), 42 deletions(-) diff --git a/backend/src/test/java/djmil/cordacheckers/CheckersTest.java b/backend/src/test/java/djmil/cordacheckers/CheckersTest.java index 4f69d3a..617f332 100644 --- a/backend/src/test/java/djmil/cordacheckers/CheckersTest.java +++ b/backend/src/test/java/djmil/cordacheckers/CheckersTest.java @@ -58,10 +58,19 @@ public class CheckersTest { this.jump = jump; this.step = step; } - Jump(Set jumps, Set steps) { + static Set intersect(Set jumps, Set steps) { + Set res = new HashSet(); + for (Integer jump :jumps) { + var jumpSteps = getKingSteps(jump); + jumpSteps.retainAll(steps); + + if (jumpSteps.size() == 1) + res.add(new Jump(jump, jumpSteps.iterator().next())); + } + return res; } - + @Override public String toString() { return "[jump=" + jump + ", step=" + step + "]"; @@ -92,40 +101,31 @@ public class CheckersTest { } - public Set getBlackSteps(Integer idx) { + public static Set getBlackSteps(Integer idx) { return adjacentCell.get(idx).stream() .filter(cur -> cur > idx && cur - idx <= 5) .collect(Collectors.toSet()); } - public Set getWhiteSteps(Integer idx) { + public static Set getWhiteSteps(Integer idx) { return adjacentCell.get(idx).stream() .filter(cur -> cur < idx && idx - cur <= 5) .collect(Collectors.toSet()); } - public Set getKingSteps(Integer idx) { + public static Set getKingSteps(Integer idx) { return adjacentCell.get(idx).stream() .filter(cur -> abs(idx - cur) <= 5) .collect(Collectors.toSet()); } - public Set getBlackJumps(Integer idx) { + public static Set getBlackJumps(Integer idx) { final var allSteps = getBlackSteps(idx); final var allJumps = adjacentCell.get(idx).stream() .filter(cur -> cur > idx && cur - idx > 5) .collect(Collectors.toSet()); - Set jumps = new HashSet(); - for (Integer jump :allJumps) { - var steps = getWhiteSteps(jump); - steps.retainAll(allSteps); - - if (steps.size() == 1) - jumps.add(new Jump(jump, steps.iterator().next())); - } - - return jumps; + return Jump.intersect(allJumps, allSteps); } public Set getWhiteJumps(Integer idx) { @@ -134,16 +134,7 @@ public class CheckersTest { .filter(cur -> idx > cur && idx - cur > 5) .collect(Collectors.toSet()); - Set jumps = new HashSet(); - for (Integer jump :allJumps) { - var steps = getBlackSteps(jump); - steps.retainAll(allSteps); - - if (steps.size() == 1) - jumps.add(new Jump(jump, steps.iterator().next())); - } - - return jumps; + return Jump.intersect(allJumps, allSteps); } public Set getKingJumps(Integer idx) { @@ -152,16 +143,7 @@ public class CheckersTest { .filter(cur -> abs(idx - cur) > 5) .collect(Collectors.toSet()); - Set jumps = new HashSet(); - for (Integer jump :allJumps) { - var steps = getKingSteps(jump); - steps.retainAll(allSteps); - - if (steps.size() == 1) - jumps.add(new Jump(jump, steps.iterator().next())); - } - - return jumps; + return Jump.intersect(allJumps, allSteps); } @Test @@ -192,18 +174,42 @@ public class CheckersTest { @Test void blackJumpTest() { - assertThat(getBlackJumps(4)).containsAll(Arrays.asList(new Jump(11, 8))); - assertThat(getBlackJumps(16)).containsAll(Arrays.asList(new Jump(23, 19))); - assertThat(getBlackJumps(15)).containsAll(Arrays.asList(new Jump(22, 18), new Jump(24, 19))); + assertThat(getBlackJumps(4)).containsAll( + Arrays.asList(new Jump(11, 8))); + + assertThat(getBlackJumps(16)).containsAll( + Arrays.asList(new Jump(23, 19))); + + assertThat(getBlackJumps(15)).containsAll( + Arrays.asList(new Jump(22, 18), new Jump(24, 19))); + assertThat(getBlackJumps(28)).isEmpty(); } @Test void whiteJumpTest() { - assertThat(getWhiteJumps(30)).containsAll(Arrays.asList(new Jump(21, 25), new Jump(23, 26))); - assertThat(getWhiteJumps(17)).containsAll(Arrays.asList(new Jump(10, 14))); + assertThat(getWhiteJumps(30)).containsAll( + Arrays.asList(new Jump(21, 25), new Jump(23, 26))); + + assertThat(getWhiteJumps(17)).containsAll( + Arrays.asList(new Jump(10, 14))); + assertThat(getWhiteJumps(7)).isEmpty(); - assertThat(getWhiteJumps(9)).containsAll(Arrays.asList(new Jump(2, 6))); + + assertThat(getWhiteJumps(9)).containsAll( + Arrays.asList(new Jump(2, 6))); + } + + @Test + void kingJumpTest() { + assertThat(getKingJumps(11)).containsAll( + Arrays.asList(new Jump(2, 7), new Jump(4, 8), new Jump(18, 15),new Jump(20, 16))); + + assertThat(getKingJumps(17)).containsAll( + Arrays.asList(new Jump(10, 14), new Jump(26, 22))); + + assertThat(getKingJumps(32)).containsAll( + Arrays.asList(new Jump(23, 27))); } }