Design an Online Multiplayer Battleship Game
Match players by rating for two-player Battleship games where each move can take up to two days and players have many games at once. Fleets stay hidden, abuse is blocked, and players come from three regions.
Problem statement
Design a ranked online Battleship game for two players. Players are matched by similar rating and take turns firing at each other's hidden ships, with up to two days per move and many games at once. A player must never learn where the opponent's ships are, except the squares they have hit.
What the system must do. - Ranked matchmaking: match each player with an opponent of similar rating (a number that estimates a player's skill). Ranked games change both players' ratings when they end. - Game management: create each game, keep track of its state, and let players see all their games. - Turn-based play: players take turns. The two players do not need to be online at the same time. A player who is offline is told when it is their turn. - A player has up to two days for each move. - A player can be in many games at the same time. - Stop automated abuse, such as scripts and bots, and stop players from sending too many moves. - Serve players in several regions of the world.
For this exercise, assume: - Standard rules. Each player has a 10 × 10 grid and places five ships on it before play starts: lengths 5, 4, 3, 3 and 2, each placed straight across or straight down, with no two ships on the same square.
On each turn, the player whose turn it is fires one shot at a square of the opponent's grid that they have not fired at before. The server answers miss, hit, or hit and sunk (naming the ship). Turns alternate after every shot, hit or miss. The first player to sink all five of the opponent's ships wins.
- Both players place their ships in any order. The server picks who shoots first, and the first turn starts when both fleets are placed. - Ratings use the Elo system (a common formula that raises the winner's rating and lowers the loser's by an amount based on the gap between them). New players start at 1,200.
Each finished game changes each player's rating exactly once. - The two-day limit applies to every step the game waits for: placing ships and each shot. A player who misses it loses the game on time. If neither player places their ships in time, the game is cancelled with no rating change.
A player can also resign at any time, which counts as a loss. - A player can have up to 5 waiting matchmaking requests and up to 50 active (unfinished) games. Each request makes one new game. A player is never matched with themselves.
- A player sees their own ships, the shots they have fired with their results, and the opponent's shots on their own grid. The opponent's ships are shown only as the squares the player has hit. Both full fleets are shown to both players only after the game ends.
- Players live in three regions: North America, Europe and Asia-Pacific. The two players in a game can be from different regions. - Real players fire at most about 10 shots per minute across all their games. Abusive scripts may send thousands of requests per second from many accounts and IP addresses.
Out of scope: unranked games with friends, chat, spectators, tournaments, payments, and catching a human player who uses an outside tool to choose shots.
What to build
- A player can ask for a ranked game and is matched with a waiting opponent of similar rating. Each request ends in exactly one game or is cancelled, and both ratings change once when the game ends.
- Players place their fleets and take turns firing. The server checks every shot, answers miss, hit or sunk, accepts exactly one shot per turn, and never shows a player the opponent's ships beyond the squares they have hit.
- Players do not need to be online together: each player is told when it is their turn, has up to two days per move before losing on time, and can follow up to 50 active games at once and see which ones are waiting for them.
- The system rejects floods of moves and slows or blocks scripts and bots, without affecting players who play at a normal pace.
At scale
- For this exercise, assume 20 million registered players, 3 million of them active each day, and about 10 million games in progress at any time.
- For this exercise, assume about 60 million shots per day (about 3,000 per second at peak), up to 100 new matchmaking requests per second, and about 30,000 game and game-list reads per second at peak.
- For this exercise, assume a player gets the result of an accepted shot within 500 ms at p95 (95% of shots are at least this fast) from any of the three regions, and the opponent's your-turn notification goes out within 10 seconds at p95.
- For this exercise, assume an accepted shot is never lost. If a whole region fails, the games stored there may pause for up to 15 minutes, and no player may lose a game on time because of the outage.