Building a Collaborative iOS Minesweeper Game with Realm
Rate this tutorial
Minesweeper was a Windows fixture from 1990 until Windows 8 relegated it to the app store in 2012. It was a single-player game, but it struck me as something that could be a lot of fun to play with others. Some family beta-testing of my first version while waiting for a ferry proved that it did get people to interact with each other (even if most interactions involved shouting, "Which of you muppets clicked on that mine?!").
The gameplay for Minesweeper is very simple.
You're presented with a grid of gray tiles. You tap on a tile to expose what's beneath. If you expose a mine, game over. If there isn't a mine, then you'll be rewarded with a hint as to how many mines are adjacent to that tile. If you deduce (or guess) that a tile is covering a mine, then you can plant a flag to record that.
You win the game when you correctly flag every mine and expose what's behind every non-mined tile.
Minesweeper wasn't designed for touchscreen devices; you had to use a physical mouse. Realm-Sweeper brings the game into the 21st century by adding touch controls. Tap a tile to reveal what's beneath; tap and hold to plant a flag.
Minesweeper was a single-player game. All people who sign into Realm-Sweeper with the same user ID get to collaborate on the same game in real time.
You also get to configure the size of the grid and how many mines you'd like to hide.
I decided to go for a simple data model that would put Realm sync to the test.
Each game is a single document/object that contains meta data (score, number of rows/columns, etc.) together with the grid of tiles (the board):
This means that even a modestly sized grid (20x20 tiles) results in a
Gamedocument/object with more than 2,000 attributes.
Every time you tap on a tile, the
Gameobject has to be synced with all other players. Those players are also tapping on tiles, and those changes have to be synced too. If you tap on a tile which isn't adjacent to any mines, then the app will recursively ripple through exposing similar, connected tiles. That's a lot of near-simultaneous changes being made to the same object from different devices—a great test of Realm's automatic conflict resolution!
If you opt to build the backend app yourself, there are only two things to configure once you create the empty Realm app:
partitionfield will be set to the username—allowing anyone who connects as that user to sync all of their games.
You can also add sync rules to ensure that a user can only sync their own games (in case someone hacks the mobile app). I always prefer using Realm functions for permissions. You can add this for both the read and write rules:
This isn't intended to be a full tutorial covering every line of code in the app. Instead, I'll point out some key components.
As always with Realm and MongoDB, it all starts with the data…
There's a single top-level Realm Object—
Most of the fields are pretty obvious.The most interesting is
board, which contains the grid of tiles:
rowis a list of
The model is also where the game logic is implemented. This means that the views can focus on the UI. For example,
Gameincludes a computed variable to check whether the game has been solved:
As with any SwiftUI app, the UI is built up of a hierarchy of many views.
ContentViewalso includes the
Those credentials are then used to register or log into the backend Realm app:
It displays each of the games within a
GameSummaryView. If you tap one of the games, then you jump to a
GameViewfor that game:
Tap the settings button and you're sent to
Tap the "New Game" button and a new
Gameobject is created and then stored in Realm by appending it to the
If the user uses multiple devices to play the game (e.g., an iPhone and an iPad), then they may want different-sized boards (taking advantage of the extra screen space on the iPad). Because of that, the view uses the device's
UserDefaultsto locally persist the settings rather than storing them in a synced realm:
It uses the
StatusButtonviews for the summary.
Below the summary, it displays the
BoardViewfor the game.
Each of the tiles is represented by a
When a tile is tapped, this view exposes its contents:
On a tap-and-hold, a flag is dropped:
When my family tested the first version of the app, they were frustrated that they couldn't tell whether they'd held long enough for the flag to be dropped. This was an easy mistake to make as their finger was hiding the tile at the time—an example of where testing with a mouse and simulator wasn't a substitute for using real devices. It was especially frustrating as getting it wrong meant that you revealed a mine and immediately lost the game. Fortunately, this is easy to fix using iOS's haptic feedback:
You now feel a buzz when the flag has been dropped.
What's displayed depends on the contents of the
Celland the state of the game. It uses four further views to display different types of tile:
Realm-Sweeper gives a real feel for how quickly Realm is able to synchronize data over the internet.
I intentionally avoided optimizing how I updated the game data in Realm. When you see a single click exposing dozens of tiles, each cell change is an update to the
Gameobject that needs to be synced.
Note that both instances of the game are running in iPhone simulators on an overworked Macbook in England. The Realm backend app is running in the US—that's a 12,000 km/7,500 mile round trip for each sync.
I took this approach as I wanted to demonstrate the performance of Realm synchronization. If an app like this became super-popular with millions of users, then it would put a lot of extra strain on the backend Realm app.
An obvious optimization would be to condense all of the tile changes from a single tap into a single write to the Realm object. If you're interested in trying that out, just fork the and make the changes. If you do implement the optimization, then please create a pull request. (I'd probably add it as an option within the settings so that the "slow" mode is still an option.)
Continuously Building and Hosting our Swift DocC Documentation using Github Actions and Netlify
May 10, 2022