Checkers move rules improved
This commit is contained in:
parent
9cbeaceca9
commit
a9ffd4b0b9
@ -58,10 +58,19 @@ public class CheckersTest {
|
|||||||
this.jump = jump;
|
this.jump = jump;
|
||||||
this.step = step;
|
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
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "[jump=" + jump + ", step=" + step + "]";
|
return "[jump=" + jump + ", step=" + step + "]";
|
||||||
@ -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()
|
return adjacentCell.get(idx).stream()
|
||||||
.filter(cur -> cur > idx && cur - idx <= 5)
|
.filter(cur -> cur > idx && cur - idx <= 5)
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Integer> getWhiteSteps(Integer idx) {
|
public static Set<Integer> getWhiteSteps(Integer idx) {
|
||||||
return adjacentCell.get(idx).stream()
|
return adjacentCell.get(idx).stream()
|
||||||
.filter(cur -> cur < idx && idx - cur <= 5)
|
.filter(cur -> cur < idx && idx - cur <= 5)
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Integer> getKingSteps(Integer idx) {
|
public static Set<Integer> getKingSteps(Integer idx) {
|
||||||
return adjacentCell.get(idx).stream()
|
return adjacentCell.get(idx).stream()
|
||||||
.filter(cur -> abs(idx - cur) <= 5)
|
.filter(cur -> abs(idx - cur) <= 5)
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Jump> getBlackJumps(Integer idx) {
|
public static Set<Jump> getBlackJumps(Integer idx) {
|
||||||
final var allSteps = getBlackSteps(idx);
|
final var allSteps = getBlackSteps(idx);
|
||||||
final var allJumps = adjacentCell.get(idx).stream()
|
final var allJumps = adjacentCell.get(idx).stream()
|
||||||
.filter(cur -> cur > idx && cur - idx > 5)
|
.filter(cur -> cur > idx && cur - idx > 5)
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
|
|
||||||
Set<Jump> jumps = new HashSet<Jump>();
|
return Jump.intersect(allJumps, allSteps);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Jump> getWhiteJumps(Integer idx) {
|
public Set<Jump> getWhiteJumps(Integer idx) {
|
||||||
@ -134,16 +134,7 @@ public class CheckersTest {
|
|||||||
.filter(cur -> idx > cur && idx - cur > 5)
|
.filter(cur -> idx > cur && idx - cur > 5)
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
|
|
||||||
Set<Jump> jumps = new HashSet<Jump>();
|
return Jump.intersect(allJumps, allSteps);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Jump> getKingJumps(Integer idx) {
|
public Set<Jump> getKingJumps(Integer idx) {
|
||||||
@ -152,16 +143,7 @@ public class CheckersTest {
|
|||||||
.filter(cur -> abs(idx - cur) > 5)
|
.filter(cur -> abs(idx - cur) > 5)
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
|
|
||||||
Set<Jump> jumps = new HashSet<Jump>();
|
return Jump.intersect(allJumps, allSteps);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -192,18 +174,42 @@ public class CheckersTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void blackJumpTest() {
|
void blackJumpTest() {
|
||||||
assertThat(getBlackJumps(4)).containsAll(Arrays.asList(new Jump(11, 8)));
|
assertThat(getBlackJumps(4)).containsAll(
|
||||||
assertThat(getBlackJumps(16)).containsAll(Arrays.asList(new Jump(23, 19)));
|
Arrays.asList(new Jump(11, 8)));
|
||||||
assertThat(getBlackJumps(15)).containsAll(Arrays.asList(new Jump(22, 18), new Jump(24, 19)));
|
|
||||||
|
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();
|
assertThat(getBlackJumps(28)).isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void whiteJumpTest() {
|
void whiteJumpTest() {
|
||||||
assertThat(getWhiteJumps(30)).containsAll(Arrays.asList(new Jump(21, 25), new Jump(23, 26)));
|
assertThat(getWhiteJumps(30)).containsAll(
|
||||||
assertThat(getWhiteJumps(17)).containsAll(Arrays.asList(new Jump(10, 14)));
|
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(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)));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user