Checkers move rules improved

This commit is contained in:
djmil 2023-09-27 17:25:30 +02:00
parent 9cbeaceca9
commit a9ffd4b0b9

View File

@ -58,8 +58,17 @@ public class CheckersTest {
this.jump = jump;
this.step = step;
}
Jump(Set<Integer> jumps, Set<Integer> steps) {
static Set<Jump> intersect(Set<Integer> jumps, Set<Integer> steps) {
Set<Jump> res = new HashSet<Jump>();
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
@ -92,40 +101,31 @@ public class CheckersTest {
}
public Set<Integer> getBlackSteps(Integer idx) {
public static Set<Integer> getBlackSteps(Integer idx) {
return adjacentCell.get(idx).stream()
.filter(cur -> cur > idx && cur - idx <= 5)
.collect(Collectors.toSet());
}
public Set<Integer> getWhiteSteps(Integer idx) {
public static Set<Integer> getWhiteSteps(Integer idx) {
return adjacentCell.get(idx).stream()
.filter(cur -> cur < idx && idx - cur <= 5)
.collect(Collectors.toSet());
}
public Set<Integer> getKingSteps(Integer idx) {
public static Set<Integer> getKingSteps(Integer idx) {
return adjacentCell.get(idx).stream()
.filter(cur -> abs(idx - cur) <= 5)
.collect(Collectors.toSet());
}
public Set<Jump> getBlackJumps(Integer idx) {
public static Set<Jump> 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<Jump> jumps = new HashSet<Jump>();
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<Jump> getWhiteJumps(Integer idx) {
@ -134,16 +134,7 @@ public class CheckersTest {
.filter(cur -> idx > cur && idx - cur > 5)
.collect(Collectors.toSet());
Set<Jump> jumps = new HashSet<Jump>();
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<Jump> getKingJumps(Integer idx) {
@ -152,16 +143,7 @@ public class CheckersTest {
.filter(cur -> abs(idx - cur) > 5)
.collect(Collectors.toSet());
Set<Jump> jumps = new HashSet<Jump>();
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)));
}
}