DEV Community

Max Miracolo
Max Miracolo

Posted on • Originally published at mcolo.hashnode.dev

The Logic Behind Tile Swaps: Lessons from Connections and Starcrossed

A screenshot of the New York Times puzzle Connections, the grid has 16 tiles, four of which are highlighted – MICE, OXEN, DICE, and LICE

Assuming you already know how Connections works, given the above scenario, which tiles from the top row will swap with which correctly selected tiles when the animation happens? There are a lot of options to pair them up. Will MICE swap with KINDER, SINGULAR or DIE? Will DICE move, or stay put? Think about it for a second and guess, read on to see the answer.


These are questions I found myself asking while playing connections one day, and it came in handy when I started building starcrossed. It’s a daily movie puzzle game inspired by games like Connections, and one aspect of Connections I set out to emulate was the animation of the tile swaps. Learning how they worked was a vital step, and it turned out to be way more intriguing than I had expected. On that note, back to the initial prompt. Here’s how the animation plays out.

An animated gif of the same Connections puzzle as before, the selected tiles OXEN, DICE, LICE, and MICE move to the top row in that order.

We end up with the following swaps: KINDEROXEN, SINGULARLICE, and DIEMICE, and DICE stayed put. Did you guess correctly? These pairs may seem arbitrary, but there is indeed a methodology for determining them.

Hierarchical

Connections employs what I would call a hierarchical approach. To illustrate this I’ve recreated the grid from above with the tiles labeled 1-16, and slowed down the animation.

An animated gif of a four by four grid of 16 tiles, numbered 1 through 16. The grid recreates the connections grid from before, and it animates the selected tiles (2,5,8,13) moving to the top row in the order of (5,2,8,13)

The four selected tiles 2 , 5, 8, and 13 need to end up in the first row. Since 2 is already in the first row, that leaves 1, 3, and 4 to swap. There are six different combinations for pairing these six tiles, and if the fourth tile also needs to be swapped then that number jumps to 24 combinations. Therefore a decision needs to be made, or a rule needs to be established to decide how the pairs are formed. In the case of Connections, the tiles are swapped based on their ordering, or hierarchy. Tiles higher up, and to the left, take precedence. Or referencing our new grid, tiles with lower numbers get paired up first. This hierarchy becomes even clearer when the tiles are arranged in a single row.

An animated gif of a a single row of 16 tiles, numbered 1 through 16. The animation sequentially highlights the pairs of tiles that will be swapped.

Simply move from left to right to determine the order for selecting pairs. First 15, then 38, and finally 413. Again, 2 stays put because it’s already one of the first four tiles.

While this approach certainly makes sense, it isn’t the only method to determine which tiles swap. One potential drawback of the hierarchical approach is that there may be more commotion, or a busier animation due to tiles moving farther than they need to. This brings us to the next approach.

Nearest Neighbor

The goal here is to have the tiles move as little distance as possible, and coincidentally, overlap as little as possible. As you can see, with the same selected tiles from the previous examples, there’s less movement in this animation.

An animated gif of a four by four grid of 16 tiles, numbered 1 through 16. The grid recreates the connections grid from before, and it animates the selected tiles (2,5,8,13) moving to the top row in the order of (5,2,13,8)

In this case, it happens to be quite close to the hierarchical approach, but instead of 38 swapping, 313 swap because 48 are closer together. However, in some cases the difference can be quite dramatic.

An animated gif of two four by four grids of 16 tiles, numbered 1 through 16. The grid on the left animates the selected tiles (8,11,13,14) moving to the top row in the order of (8,11,13,14). The grid on the right animates the selected tiles (8,11,13,14) moving to the top row in the order of (13,14,11,8)

The nearest neighbor is a more subtle approach and I’m even contemplating updating starcrossed to use it. For now, starcrossed will keep its current approach, which I will call user-driven.

User-Driven

If you think about it, an order has already been established before the swap occurs. We can follow the sequence in which the user selected the tiles, and then have that be the order for how the tiles should land in the top row. Below is a visualization of this approach, again using the same selected tiles as the previous examples, now assuming the user selected the tiles in a specific order: 21385.

An animated gif of a four by four grid of 16 tiles, numbered 1 through 16. The grid recreates the connections grid from before, and it animates the selected tiles (2,5,8,13) moving to the top row in the order of (2,13,8,5)

Where nearest neighbor dialed back the commotion from the hierarchical approach, this can result in more commotion, which may be desirable! This is the approach I chose for starcrossed, with a caveat: no tiles are swapped. The tiles in the first row remain in place and are covered by the user-selected tiles.

An animated gif of a four by four grid of 16 tiles, numbered 1 through 16. The grid recreates the connections grid from before, and it animates the selected tiles (2,5,8,13) moving to the top row in the order of (5,2,13,8). This time no tiles are swapped.

Here’s a look at this approach in action with starcrossed.

An animated gif of the puzzle game starcrossed. Four headshots are selected and move to the top row, and then the category is revealed.

In this case, the tiles were selected in the following order:

Hugh JackmanRussell CroweAnne HathawayAmanda Seyfried

One reason I chose this approach was to create consistency. The tiles selected by the user maintain this order as they move to the top row, and that order is maintained again when the names are listed in the revealed category (green box). I also found that by not swapping the tiles with the user-driven approach it was a happy medium in terms of animation busyness.

Embrace the Chaos

A fourth option would be for the animation to have as much commotion as possible. Having more or less commotion isn’t strictly a good or bad thing. It largely depends on the use case — perhaps more commotion is desired to evoke a feeling of high energy and excitement.

In my mind, there are essentially two ways to implement this more chaotic approach. One would be to find the opposite of the nearest neighbor (farthest neighbor), and the other would be to choose pairs at random each time the animation will run. Of course, choosing pairs at random could result in the same combination as nearest neighbor, but the randomness between each round of swaps could still evoke a feeling of novelty and excitement.


Each approach has it’s pros and cons and choosing one ultimately depends on the experience you want to create. Whether it’s the structured logic of the hierarchical method, the subtle efficiency of nearest neighbor, the personalized feel of user-driven swaps, or the lively unpredictability of a chaotic approach, each method brings something unique. For starcrossed, I opted for consistency and excitement, but experimenting with different styles has been a fascinating process. If nothing else, I hope this overview has given you a new appreciation for the small details that shape how puzzles come to life on screen.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay