Skip to content
Settlers Journal
Go back

Refactoring Graph Data Structures and Loading Board Layout from Files

Edit page

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

Challenges

Learnings


Edit page
Share this post on:

Previous Post
Introducing ResourceBundle for Player Resources and Expanding Unit Test Coverage
Next Post
Full-Stack Road Placement and Builder Pattern for the FSM