HomeLearnHow-toBuilding a Collaborative iOS Minesweeper Game with Realm

Building a Collaborative iOS Minesweeper Game with Realm

Updated: Mar 18, 2022 |

Published: Mar 14, 2022

  • Realm
  • Swift
  • Swift UI
  • ...

By Andrew Morgan

Rate this article

#Introduction

I wanted to build an app that we could use at events to demonstrate Realm Sync. It needed to be fun to interact with, and so a multiplayer game made sense. Tic-tac-toe is too simple to get excited about. I'm not a game developer and so Call Of Duty wasn't an option. Then I remembered Microsoft's Minesweeper.

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?!").

Family sat around a table, all playing the Realm-Sweeper game on their iPhones

You can download the back end and iOS apps from the Realm-Sweeper repo, and get it up and running in a few minutes if you want to play with it.

This article steps you through some of the key aspects of setting up the backend Realm app, as well as the iOS code. Hopefully, you'll see how simple it is and try building something for yourself. If anyone's looking for ideas, then Sokoban could be interesting.

#Prerequisites

#The Minesweeper game

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.

#What Realm-Sweeper adds

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.

Animation of two iPhones. As a user taps a tile on one device, the change appears almost instantly on the other

You also get to configure the size of the grid and how many mines you'd like to hide.

#The data model

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):

Data model for the Game Class

This means that even a modestly sized grid (20x20 tiles) results in a Game document/object with more than 2,000 attributes.

Every time you tap on a tile, the Game object 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!

#The backend Realm app

If you don't want to set this up yourself, simply follow the instructions from the repo to import the app.

If you opt to build the backend app yourself, there are only two things to configure once you create the empty Realm app:

  1. Enable email/password authentication. I kept it simple by opting to auto-confirm new users and sticking with the default password-reset function (which does nothing).
  2. Enable partitioned Realm sync. Set the partition key to partition and enable developer mode (so that the schema will be created automatically when the iOS app syncs for the first time).

The partition field 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:

1{
2 "%%true": {
3 "%function": {
4 "arguments": [
5 "%%partition"
6 ],
7 "name": "canAccessPartition"
8 }
9 }
10}

The canAccessPartition function is:

1exports = function(partition) {
2 const user = context.user.data.email;
3 return partition === user;
4};

#The iOS app

I'd suggest starting by downloading, configuring, and running the app—just follow the instructions from the repo. That way, you can get a feel for how it works.

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…

#Model

There's a single top-level Realm Object—Game:

1class Game: Object, ObjectKeyIdentifiable {
2 @Persisted(primaryKey: true) var _id: ObjectId
3 @Persisted var numRows = 0
4 @Persisted var numCols = 0
5 @Persisted var score = 0
6 @Persisted var startTime: Date? = Date()
7 @Persisted var latestMoveTime: Date?
8 @Persisted var secondsTakenToComplete: Int?
9 @Persisted var board: Board?
10 @Persisted var gameStatus = GameStatus.notStarted
11 @Persisted var winningTimeInSeconds: Int?
12
13}

Most of the fields are pretty obvious.The most interesting is board, which contains the grid of tiles:

1class Board: EmbeddedObject, ObjectKeyIdentifiable {
2 @Persisted var rows = List<Row>()
3 @Persisted var startingNumberOfMines = 0
4 ...
5}

row is a list of Cells:

1class Row: EmbeddedObject, ObjectKeyIdentifiable {
2 @Persisted var cells = List<Cell>()
3 ...
4}
5
6class Cell: EmbeddedObject, ObjectKeyIdentifiable {
7 @Persisted var isMine = false
8 @Persisted var numMineNeigbours = 0
9 @Persisted var isExposed = false
10 @Persisted var isFlagged = false
11 @Persisted var hasExploded = false
12 ...
13}

The model is also where the business game logic is implemented. This means that the views can focus on the UI. For example, Game includes a computed variable to check whether the game has been solved:

1var hasWon: Bool {
2 guard let board = board else { return false }
3 if board.remainingMines != 0 { return false }
4
5 var result = true
6
7 board.rows.forEach() { row in
8 row.cells.forEach() { cell in
9 if !cell.isExposed && !cell.isFlagged {
10 result = false
11 return
12 }
13 }
14 if !result { return }
15 }
16 return result
17}

#Views

As with any SwiftUI app, the UI is built up of a hierarchy of many views.

Screen capture from Xcode showing the hierarchy of views making up the RealmSweeper UI

Here's a quick summary of the views that make up Real-Sweeper:

ContentView is the top-level view. When the app first runs, it will show the LoginView. Once the user has logged in, it shows GameListView instead. It's here that we set the Realm Sync partition (to be the username of the user that's just logged in):

1GameListView()
2 .environment(\.realmConfiguration,
3 realmApp.currentUser!.configuration(partitionValue: username))

ContentView also includes the LogoutButton view.

LoginView allows the user to provide a username and password:

Screen capture of the login view. Fields to enter username and password. Checkbox to indicate that you're registering a new user. Button to login,

Those credentials are then used to register or log into the backend Realm app:

1func userAction() {
2 Task {
3 do {
4 if newUser {
5 try await realmApp.emailPasswordAuth.registerUser(
6 email: email, password: password)
7 }
8 let _ = try await realmApp.login(
9 credentials: .emailPassword(email: email, password: password))
10 username = email
11 } catch {
12 errorMessage = error.localizedDescription
13 }
14 }
15}

GameListView reads the list of this user's existing games.

1@ObservedResults(Game.self,
2 sortDescriptor: SortDescriptor(keyPath: "startTime", ascending: false)) var games

It displays each of the games within a GameSummaryView. If you tap one of the games, then you jump to a GameView for that game:

1NavigationLink(destination: GameView(game: game)) {
2 GameSummaryView(game: game)
3}

GameListView, Screen capture of a list of games, together with buttons to logout, set settings, or create a new game

Tap the settings button and you're sent to SettingsView.

Tap the "New Game" button and a new Game object is created and then stored in Realm by appending it to the games live query:

1private func createGame() {
2 numMines = min(numMines, numRows * numColumns)
3 game = Game(rows: numRows, cols: numColumns, mines: numMines)
4 if let game = game {
5 $games.append(game)
6 }
7 startGame = true
8}

SettingsView lets the user choose the number of tiles and mines to use:

SettingsView. Steppers to set the number of rows, columns, and mines

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 UserDefaults to locally persist the settings rather than storing them in a synced realm:

1@AppStorage("numRows") var numRows = 10
2@AppStorage("numColumns") var numColumns = 10
3@AppStorage("numMines") var numMines = 15

GameSummaryView displays a summary of one of the user's current or past games.

GameSummaryView. Screen capture of view containing the start and completion times + emoji for the status of the game

GameView shows the latest stats for the current game at the top of the screen:

GameStatusView. Screen capture showing remaining mines, status of the game (smiling emoji) and elapsed time

It uses the LEDCounter and StatusButton views for the summary.

Below the summary, it displays the BoardView for the game.

LEDCounter displays the provided number as three digits using a retro LED font:

CounterView – 3 red LED numbers

StatusButton uses a ZStack to display the symbol for the game's status on top of a tile image:

StatusButton. Smiling emoji in front of a gray tile

The view uses SwiftUI's GeometryReader function to discover how much space is available so that it can select an appropriate font size for the symbol:

1GeometryReader { geo in
2 Text(status)
3 .font(.system(size: geo.size.height * 0.7))
4}

BoardView displays the game's grid of tiles:

BoardView. A grid of tiles. Some tiles have been removed, revealing colored numbers. One tile contains a red flag

Each of the tiles is represented by a CellView view.

When a tile is tapped, this view exposes its contents:

1.onTapGesture() {
2 expose(row: row, col: col)
3}

On a tap-and-hold, a flag is dropped:

1.onLongPressGesture(minimumDuration: 0.1) {
2 flag(row: row, col: col)
3}

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:

1func hapticFeedback(_ isSuccess: Bool) {
2 let generator = UINotificationFeedbackGenerator()
3 generator.notificationOccurred(isSuccess ? .success : .error)
4}

You now feel a buzz when the flag has been dropped.

CellView displays an individual tile:

CellView. Tile containing a crossed out red flag

What's displayed depends on the contents of the Cell and the state of the game. It uses four further views to display different types of tile: FlagView, MineCountView, MineView, and TileView.

FlagView

FlagView. 2 Tiles, both containing a flag, one shows the  flag crossed out

MineCountView

MineCountView, 6 gray tiles. One is empty the others containing numbers 1 through 5, each in a different color

MineView

MineView. Two tiles containing mines, one with a gray background, one with a red background

TileView

TileView. A single gray tile

#Conclusion

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 Game object that needs to be synced.

GIF showing changes in one game board appearing in near-realtime in the game board on a different device

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 repo 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.)

Got questions? Ask them in our Community forum.

Rate this article
MongoDB logo
© 2021 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.