Checkers move rules
This commit is contained in:
parent
9144683de3
commit
9cbeaceca9
209
backend/src/test/java/djmil/cordacheckers/CheckersTest.java
Normal file
209
backend/src/test/java/djmil/cordacheckers/CheckersTest.java
Normal file
@ -0,0 +1,209 @@
|
|||||||
|
package djmil.cordacheckers;
|
||||||
|
|
||||||
|
import static java.lang.Math.abs;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.platform.commons.annotation.Testable;
|
||||||
|
|
||||||
|
@Testable
|
||||||
|
public class CheckersTest {
|
||||||
|
|
||||||
|
public final static Map<Integer, List<Integer>> adjacentCell = Map.ofEntries(
|
||||||
|
Map.entry(1, Arrays.asList(5, 6, 10)),
|
||||||
|
Map.entry(2, Arrays.asList(6, 7, 9, 11)),
|
||||||
|
Map.entry(3, Arrays.asList(7, 8, 10, 12)),
|
||||||
|
Map.entry(4, Arrays.asList(8, 11)),
|
||||||
|
Map.entry(5, Arrays.asList(1, 9, 14, 9, 11)),
|
||||||
|
Map.entry(6, Arrays.asList(1, 2, 9, 10, 13, 15)),
|
||||||
|
Map.entry(7, Arrays.asList(2, 3, 10, 11, 14, 16)),
|
||||||
|
Map.entry(8, Arrays.asList(3, 4, 11, 12, 15)),
|
||||||
|
Map.entry(9, Arrays.asList(2, 5, 6, 13, 14, 18)),
|
||||||
|
Map.entry(10, Arrays.asList(1, 3, 6, 7, 14, 15, 17, 19)),
|
||||||
|
Map.entry(11, Arrays.asList(2, 4, 7, 8, 15, 16, 18, 20)),
|
||||||
|
Map.entry(12, Arrays.asList(3, 8, 16, 19)),
|
||||||
|
Map.entry(13, Arrays.asList(6, 9, 17, 22)),
|
||||||
|
Map.entry(14, Arrays.asList(5, 7, 9, 10, 17, 18, 21, 23)),
|
||||||
|
Map.entry(15, Arrays.asList(6, 8, 10, 11, 18, 19, 22, 24)),
|
||||||
|
Map.entry(16, Arrays.asList(7, 11, 12, 19, 20, 23)),
|
||||||
|
Map.entry(17, Arrays.asList(10, 13, 14, 21, 22, 26)),
|
||||||
|
Map.entry(18, Arrays.asList(9, 11, 14, 15, 22, 23, 25, 27)),
|
||||||
|
Map.entry(19, Arrays.asList(10, 12, 15, 16, 23, 24)),
|
||||||
|
Map.entry(20, Arrays.asList(11, 16, 24, 27)),
|
||||||
|
Map.entry(21, Arrays.asList(14, 17, 25, 30)),
|
||||||
|
Map.entry(22, Arrays.asList(13, 15, 17, 18, 25, 26, 29, 31)),
|
||||||
|
Map.entry(23, Arrays.asList(14, 16, 18, 19, 26, 27, 30, 32)),
|
||||||
|
Map.entry(24, Arrays.asList(15, 19, 20, 27, 28, 31)),
|
||||||
|
Map.entry(25, Arrays.asList(18, 21, 22, 29, 30)),
|
||||||
|
Map.entry(26, Arrays.asList(17, 19, 22, 23, 30, 31)),
|
||||||
|
Map.entry(27, Arrays.asList(18, 20, 23, 24, 31, 32)),
|
||||||
|
Map.entry(28, Arrays.asList(19, 24, 32)),
|
||||||
|
Map.entry(29, Arrays.asList(22, 25)),
|
||||||
|
Map.entry(30, Arrays.asList(21, 23, 25, 26)),
|
||||||
|
Map.entry(31, Arrays.asList(22, 24, 26, 27)),
|
||||||
|
Map.entry(32, Arrays.asList(23, 27, 28))
|
||||||
|
);
|
||||||
|
|
||||||
|
public static class Jump {
|
||||||
|
final public int jump;
|
||||||
|
final public int step;
|
||||||
|
Jump(int jump, int step) {
|
||||||
|
this.jump = jump;
|
||||||
|
this.step = step;
|
||||||
|
}
|
||||||
|
Jump(Set<Integer> jumps, Set<Integer> steps) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "[jump=" + jump + ", step=" + step + "]";
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
final int prime = 31;
|
||||||
|
int result = 1;
|
||||||
|
result = prime * result + jump;
|
||||||
|
result = prime * result + step;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null)
|
||||||
|
return false;
|
||||||
|
if (getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
Jump other = (Jump) obj;
|
||||||
|
if (jump != other.jump)
|
||||||
|
return false;
|
||||||
|
if (step != other.step)
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public 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) {
|
||||||
|
return adjacentCell.get(idx).stream()
|
||||||
|
.filter(cur -> cur < idx && idx - cur <= 5)
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
|
||||||
|
public 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) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Jump> getWhiteJumps(Integer idx) {
|
||||||
|
final var allSteps = getWhiteSteps(idx);
|
||||||
|
final var allJumps = adjacentCell.get(idx).stream()
|
||||||
|
.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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Jump> getKingJumps(Integer idx) {
|
||||||
|
final var allSteps = getKingSteps(idx);
|
||||||
|
final var allJumps = adjacentCell.get(idx).stream()
|
||||||
|
.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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void blackStepTest() {
|
||||||
|
assertThat(getBlackSteps(1)).containsAll(Arrays.asList(5, 6));
|
||||||
|
assertThat(getBlackSteps(4)).containsAll(Arrays.asList(8));
|
||||||
|
assertThat(getBlackSteps(18)).containsAll(Arrays.asList(22, 23));
|
||||||
|
assertThat(getBlackSteps(21)).containsAll(Arrays.asList(25));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whiteStepTest() {
|
||||||
|
assertThat(getWhiteSteps(29)).containsAll(Arrays.asList(25));
|
||||||
|
assertThat(getWhiteSteps(28)).containsAll(Arrays.asList(24));
|
||||||
|
assertThat(getWhiteSteps(14)).containsAll(Arrays.asList(9, 10));
|
||||||
|
assertThat(getWhiteSteps(8)).containsAll(Arrays.asList(3, 4));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void kingStepTest() {
|
||||||
|
assertThat(getKingSteps(29)).containsAll(Arrays.asList(25));
|
||||||
|
assertThat(getKingSteps(31)).containsAll(Arrays.asList(26, 27));
|
||||||
|
assertThat(getKingSteps(20)).containsAll(Arrays.asList(16, 24));
|
||||||
|
assertThat(getKingSteps(13)).containsAll(Arrays.asList(9, 17));
|
||||||
|
assertThat(getKingSteps(2)).containsAll(Arrays.asList(6, 7));
|
||||||
|
assertThat(getKingSteps(15)).containsAll(Arrays.asList(10, 11, 18, 19));
|
||||||
|
}
|
||||||
|
|
||||||
|
@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(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(7)).isEmpty();
|
||||||
|
assertThat(getWhiteJumps(9)).containsAll(Arrays.asList(new Jump(2, 6)));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user