TLDR
Rafactoring graph data structures and helper methods. Reading board layout details from resource files rather than hardcoding. Adding Hexagons into larger graph structure so we can more easily find adjacent Hexagons for a Settlement.
Reading board layout from a file.
// read in the json file has a map of graphNodeIndex to list of neighboring graphNodeIndices
// would throw during initialization, so fine to ignore
@SneakyThrows
private static Map<GraphNodeIndex, List<GraphNodeIndex>> readAdjacentsFromFile(String fileName) {
String filePath = Objects.requireNonNull(
ResourceLoader.class.getClassLoader().getResource(fileName)).getFile();
return new ObjectMapper()
.readValue(new File(filePath), Adjacents.class)
.adjacents()
.stream()
.collect(
Collectors
.toMap(
adjacency -> rawIndexToGraphNodeIndex(adjacency.node()),
adjacency -> adjacency.neighbors()
.stream()
.map(CatanGraph::rawIndexToGraphNodeIndex)
.toList()
)
);
}
Main work
- Updating graph
HashMapto use the entireGraphNodeobjects, rather thanStringkeys. - Removing unneeded fields in
GraphNodes, adding composition / field forIGraphNodePiece. - Updating helper functions to use the new
GraphNodestructure.
Challenges
- Java is verbose, even reading a file into a particular data structure involves a lot of
StreamAPI use. - Thinking about inheritence / composition / design changes as the final application structure becomes more apparent.
Learnings
Records in Java have default hash implementations that are useful, but can silently be wrong based on referenced objects.- For example, if a
Recordreferences aList<T>, even ifTalso has a useful hash, theList<T>reference is what gets hashed. - E.g., Two equivalent
List<T>would not result in identicalRecordhashes.
- For example, if a