Building on top of Simon's excellent answer answer, why stop at enum
s for Direction
s when you can do one to represent Border
s too?
This solution can't claim to be the speediest, and I still think Simon's answer provides the ideal balance between performance and an 'OO'-approach. JS1's approach JS1's approach is the bit mask approach you ask for... Consider this if it turns out you do have a case to determine where your Border
s are, such that the comparison logic between your Direction
s and said Border
s can be abstracted away.
Building on top of Simon's excellent answer, why stop at enum
s for Direction
s when you can do one to represent Border
s too?
This solution can't claim to be the speediest, and I still think Simon's answer provides the ideal balance between performance and an 'OO'-approach. JS1's approach is the bit mask approach you ask for... Consider this if it turns out you do have a case to determine where your Border
s are, such that the comparison logic between your Direction
s and said Border
s can be abstracted away.
Building on top of Simon's excellent answer, why stop at enum
s for Direction
s when you can do one to represent Border
s too?
This solution can't claim to be the speediest, and I still think Simon's answer provides the ideal balance between performance and an 'OO'-approach. JS1's approach is the bit mask approach you ask for... Consider this if it turns out you do have a case to determine where your Border
s are, such that the comparison logic between your Direction
s and said Border
s can be abstracted away.
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
public class Game {
public static void main(String[] args) {
final int limit = 10;
final Set<Border> leftBorder = Border.getBorders(1, 0, limit, limit);
final Set<Border> rightBorder = Border.getBorders(0, 1, limit, limit);
final Set<Border> topBorder = Border.getBorders(limit, 1, limit, limit);
final Set<Border> bottomBorder = Border.getBorders(1, limit, limit, limit);
final Set<Border> topLeftCorner = Border.getBorders(limit, 0, limit, limit);
final Set<Border> topRightCorner = Border.getBorders(limit, limit, limit, limit);
final Set<Border> bottomRightCorner = Border.getBorders(0, limit, limit, limit);
final Set<Border> bottomLeftCorner = Border.getBorders(0, 0, limit, limit);
final Set<Border> free = Border.getBorders(1, 1, limit, limit);
final List<Set<Border>> combinations = Arrays.asList(leftBorder, rightBorder, topBorder, bottomBorder,
topLeftCorner, topRightCorner, bottomRightCorner, bottomLeftCorner, free);
Stream.of(Direction.values())
.flatMap(d -> combinations.stream()
.filter(b -> !b.stream().map(Object::toString).anyMatchnoneMatch(v -> v.name().equals(d.opposite().name())))
.peek(b -> System.out.println("Generating moves for " + d + " given borders " + b))
.map(b -> d.getValidMoves(b)))
.forEach(System.out::println);
}
}
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
public class Game {
public static void main(String[] args) {
final int limit = 10;
final Set<Border> leftBorder = Border.getBorders(1, 0, limit, limit);
final Set<Border> rightBorder = Border.getBorders(0, 1, limit, limit);
final Set<Border> topBorder = Border.getBorders(limit, 1, limit, limit);
final Set<Border> bottomBorder = Border.getBorders(1, limit, limit, limit);
final Set<Border> topLeftCorner = Border.getBorders(limit, 0, limit, limit);
final Set<Border> topRightCorner = Border.getBorders(limit, limit, limit, limit);
final Set<Border> bottomRightCorner = Border.getBorders(0, limit, limit, limit);
final Set<Border> bottomLeftCorner = Border.getBorders(0, 0, limit, limit);
final Set<Border> free = Border.getBorders(1, 1, limit, limit);
final List<Set<Border>> combinations = Arrays.asList(leftBorder, rightBorder, topBorder, bottomBorder,
topLeftCorner, topRightCorner, bottomRightCorner, bottomLeftCorner, free);
Stream.of(Direction.values())
.flatMap(d -> combinations.stream()
.filter(b -> !b.stream().map(Object::toString).anyMatch(v -> v.equals(d.opposite().name())))
.peek(b -> System.out.println("Generating moves for " + d + " given borders " + b))
.map(b -> d.getValidMoves(b)))
.forEach(System.out::println);
}
}
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
public class Game {
public static void main(String[] args) {
final int limit = 10;
final Set<Border> leftBorder = Border.getBorders(1, 0, limit, limit);
final Set<Border> rightBorder = Border.getBorders(0, 1, limit, limit);
final Set<Border> topBorder = Border.getBorders(limit, 1, limit, limit);
final Set<Border> bottomBorder = Border.getBorders(1, limit, limit, limit);
final Set<Border> topLeftCorner = Border.getBorders(limit, 0, limit, limit);
final Set<Border> topRightCorner = Border.getBorders(limit, limit, limit, limit);
final Set<Border> bottomRightCorner = Border.getBorders(0, limit, limit, limit);
final Set<Border> bottomLeftCorner = Border.getBorders(0, 0, limit, limit);
final Set<Border> free = Border.getBorders(1, 1, limit, limit);
final List<Set<Border>> combinations = Arrays.asList(leftBorder, rightBorder, topBorder, bottomBorder,
topLeftCorner, topRightCorner, bottomRightCorner, bottomLeftCorner, free);
Stream.of(Direction.values())
.flatMap(d -> combinations.stream()
.filter(b -> b.stream().noneMatch(v -> v.name().equals(d.opposite().name())))
.peek(b -> System.out.println("Generating moves for " + d + " given borders " + b))
.map(b -> d.getValidMoves(b)))
.forEach(System.out::println);
}
}
Caveats
This solution can't claim to be the speediest, and I still think Simon's answer provides the ideal balance between performance and an 'OO'-approach. JS1's approach is the bit mask approach you ask for... Consider this if it turns out you do have a case to determine where your Border
s are, such that the comparison logic between your Direction
s and said Border
s can be abstracted away.
Caveats
This solution can't claim to be the speediest, and I still think Simon's answer provides the ideal balance between performance and an 'OO'-approach. JS1's approach is the bit mask approach you ask for... Consider this if it turns out you do have a case to determine where your Border
s are, such that the comparison logic between your Direction
s and said Border
s can be abstracted away.