Placing number tiles in Settlers of Catan for iOS


I'm coding Settlers of Catan for iOS and currently have my data tiles in a matix. Now, I'm trying to attach the numbers to each tile. But I'm stuck as to the correct way to do this algorithmically. There are specific rules for putting down the number tiles on the board. These are:

1) They must go in the pre-defined sequence shown in the image.

2) You must start in a corner of the board.

3) You must work your way around the board counter-clockwise, spiraling toward the center.

4) You must skip the desert tile.

Catan board

Since there are only 6 places on the board where you can actually start putting tiles down, I realize that it wouldn't be difficult to hand-code the 6 solutions into my data grid. But there is a variation of the game in which the board gets larger, so I figured it would be worth exploring an algorithm.

Any thoughts how this can be achieved?


Answers:


What you want to do will involve the geometric relationships between tiles (entries in your matrix)

You need a way of storing and querying that information.

There are several methods people have developed for assigning coordinates to hexagons:

One way to do what you want, off the top of my head, would be for each hex to have 6 'pointers' to its neighbors. These pointers could really be indices in your array, of course.

You could detect a 'corner' hex by noting that it will have 3 'null' neighbors. Then you can traverse counter-clockwise from there, remembering which you've already visited.


Update (in response to comment)

Let's suppose that for a given hex, we store the 6 neighbors as I described.

For this discussion, we'll name those neighbors A-F.

We assign these counter-clockwise, because that's convenient for us.

Graphically:

          A
        _____
   B   /     \   F
      /       \
     (         )
      \       /
   C   \_____/   E

          D

For a 'corner' hex, we might have:

          A
        _____
   B   /     \   NULL
      /       \
     (         )
      \       /
   C   \_____/   NULL

          NULL

So if we are looking at this hex, and we list our adjacency information (wrapping around), we have:

A, B, C, NULL, NULL, NULL, A, B, C, ...

A soon as we find 3 NULLs in a row, we know that the next spot should "point" in our starting direction. In this case we should start off in the direction of 'A'.

Another example:

          NULL
        _____
 NULL  /     \   NULL
      /       \
     (         )
      \       /
   C   \_____/   E

          D

Just as before, we make a list, starting from the top.

NULL, NULL, C, D, E, NULL, NULL, NULL, C

As soon as we find 3 NULLs, the next one is our starting direction - 'C' in this case.

Spiraling onward in the same direction from there should be fairly straightforward (well not literally - har har).

PS: thanks to http://ascii.co.uk/art/hexagon for quick ascii art (: