Skip to content
Settlers Journal
Go back

Revisiting Catan With a Fresh Perspective

Edit page

TLDR

Exploring a new React proof of concept that redesigns the UI. The responsive layout is now handled using SVG elements that scale naturally. Using new AI coding workflows that change my approach to this project. Reflecting on how large the scope of building a professional-grade Catan web application really is.

alt text

New SVG-based hexagonal grid / board. Note that this is much cleaner and scales naturally when devices are resized.

Main work

SVG Math

The Catan board is essentially a hexagonal grid with different types of items placed on it:

To create the hexagonal grid, there is a bit of math involved (mostly geometry). There are a lot of great references on the math behind hexagons, so I will only cover the main idea.

Review the reference grid below that introduces a new coordinate system to effectively work with hexagonal grids. We refer to the coordinates as (q, r) tuples, where q is the column and r is the row.

alt text

Credit to Milo Trujillo for this graphic showing the hexagonal grid coordinates.

The most awkward thing about this grid is that tiles with the same q value (column) do not necessarily exist in the same vertical slice. In other words, the r value is also needed to determine the horizontal positioning of a grid element.

The final position of every SVG element is derived from these coordinates. The SVG elements have to be defined in terms of traditional (x, y) coordinates, so we need to be able to translate back and forth between the coordinate systems.

We can determine the “traditional” center coordinate of these hexagonal coordinates with a simple linear transformation. We want to take a (q, r) pair and convert it to an (x, y) pair. Note that the matrix reflects our earlier observation that x is dependent on both q and r.

| x |              | √3   √3/2 | | q |
| y | = HEX_SIZE * |  0    3/2 | | r |

For example, with (q=2, r=2) and HEX_SIZE = 40:

x = 40 * (√3 * 2  +  (√3/2) * 2)
  = 40 * (2√3 + √3)
  = 40 * 3√3
 40 * 5.196
 207.85

y = 40 * (3/2) * 2
  = 40 * 3
  = 120.00

(x, y) = (207.85, 120.00)

From here, we can use the traditional Cartesian coordinates and trigonometry to find the six vertices of the hexagon.

If you’re curious, you can see the raw SVG elements that are generated in the final output. Note that the center position of the (q=2, r=2) hexagon never appears, since each hexagon is a polygon element. polygon elements are entirely defined by their vertices.

Challenges

Learnings


Edit page
Share this post on:

Previous Post
Hosting the Settlers Journal On a Static Website
Next Post
Computing Available Actions for the `TURN_SPEND_RESOURCES` State