Reversi is one of those games you can explain in a sentence and still lose to for years. Place a disc so that it traps a straight line of your opponent's discs between it and another of yours, and every trapped disc flips to your colour. Whoever owns the most discs when nobody can move wins.
On mympgames, my free arcade of 60+ multiplayer browser games, the whole Reversi rule engine is a single 124-line server module. This post walks through it: how the board starts, how a move is checked, the rule about passing that trips up a lot of implementations, and how the client gets just enough information to draw hint dots without doing any rule work itself.
The contract every game follows
Every game on the site exports the same four things: meta, createState, onAction and publicState. The room server owns the sockets and the turn clock; the game module owns the rules. Reversi's meta is tiny:
const meta = { id: 'reversi', name: 'Reversi', blurb: 'Flank and flip — own the majority.', players: 2, tags: ['turn-based', 'strategy'], }; Players are just indexes, 0 and 1. An empty square is null. That choice keeps the rest of the code very small, because "the opponent" is always 1 - player.
The starting position
Reversi starts with four discs in the middle, placed diagonally:
function createState() { const board = Array.from({ length: SIZE }, () => Array(SIZE).fill(null)); board[3][3] = 1; board[3][4] = 0; board[4][3] = 0; board[4][4] = 1; return { board, turn: 0, winner: null, scores: [2, 2] }; } One small trap here: Array(SIZE).fill([]) style shortcuts share a single row object across the whole board. Building each row inside Array.from gives eight independent rows, so flipping one square never mysteriously flips a whole column.
One function does all the rule work: flipsFor
The heart of the game is a function that answers one question: if this player put a disc here, which discs would flip? If the answer is "none", the move is illegal. That means legality checking and move execution share exactly the same code, so they can never disagree.
const DIRS = [ [-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1], ]; function flipsFor(board, r, c, player) { if (board[r][c] != null) return []; const opp = 1 - player; const flips = []; for (const [dr, dc] of DIRS) { let rr = r + dr; let cc = c + dc; const line = []; while (inBounds(rr, cc) && board[rr][cc] === opp) { line.push([rr, cc]); rr += dr; cc += dc; } if (line.length && inBounds(rr, cc) && board[rr][cc] === player) { flips.push(...line); } } return flips; } For each of the eight directions it walks outward collecting opponent discs. The line only counts if two things are true when the walk stops: it collected at least one disc, and the square it stopped on is still on the board and belongs to the mover. Ran into an empty square or the edge? That direction contributes nothing.
The line.length check matters more than it looks. Without it, placing a disc right next to one of your own would count as a "capture" of zero discs in that direction, and a buggy version could accept a move that flips nothing at all.
Listing legal moves is then just "every square where flipsFor returns something":
function legalMoves(board, player) { const moves = []; for (let r = 0; r < SIZE; r++) for (let c = 0; c < SIZE; c++) if (flipsFor(board, r, c, player).length) moves.push([r, c]); return moves; } That's 64 squares times up to 8 rays, at most a few hundred cheap array reads. I thought about caching or using bitboards, and decided it wasn't worth it: on an 8×8 board, the simple scan runs in well under a millisecond, and readable code is easier to trust in a multiplayer game where the server is the referee.
Validating a move on the server
Clients never tell the server "I flipped these discs". They send { type: 'place', r, c } and the server works out the rest:
function onAction(state, playerIndex, action) { if (state.winner != null) return { ok: false, error: 'Game over' }; if (playerIndex !== state.turn) return { ok: false, error: 'Not your turn' }; if (action.type !== 'place') return { ok: false, error: 'Invalid action' }; const { r, c } = action; if (!inBounds(r, c)) return { ok: false, error: 'OOB' }; const flips = flipsFor(state.board, r, c, playerIndex); if (!flips.length) return { ok: false, error: 'Illegal' }; state.board[r][c] = playerIndex; for (const [rr, cc] of flips) state.board[rr][cc] = playerIndex; state.scores = count(state.board); // ...turn handling below } The order of the guards is deliberate: cheap checks first (game over, wrong turn, wrong action type, out of bounds), the actual rule check last. A modified client can send whatever it likes; the worst it can get back is an error string.
The rule people forget: passing
Here is where a lot of quick Reversi clones go wrong. If the next player has no legal move, they don't lose and the game doesn't end. They pass, and the same player moves again. The game only ends when neither player can move, which happens on a full board but can also happen earlier, for example when one colour has been wiped out.
let next = 1 - playerIndex; if (!legalMoves(state.board, next).length) { if (!legalMoves(state.board, playerIndex).length) { finish(state); return { ok: true }; } next = playerIndex; // opponent passes, mover goes again } state.turn = next; return { ok: true }; I like that passing isn't an action at all here. There's no "Pass" button for a player to press at the wrong moment and no way to pass when you actually have a move. The server simply notices that a player is stuck and skips them. The tradeoff is that the skip can surprise people: the turn indicator just stays on your opponent. A small toast in the UI explaining "no legal moves, turn passes" would help, and it's on my list.
Scoring at the end is a plain count, with -1 standing for a draw:
function finish(state) { state.scores = count(state.board); if (state.scores[0] > state.scores[1]) state.winner = 0; else if (state.scores[1] > state.scores[0]) state.winner = 1; else state.winner = -1; } What the client gets to see
Reversi has no hidden information, so publicState is mostly a copy of the state. The interesting addition is legal: the server sends the list of legal squares for whoever's turn it is.
function publicState(state) { return { board: state.board.map((r) => [...r]), turn: state.turn, winner: state.winner, scores: [...state.scores], size: SIZE, legal: legalMoves(state.board, state.turn), }; } The Vue component turns that into a Set of "r,c" keys. It uses it to disable buttons on illegal squares and, when the room has hints turned on, to draw a small dot on each playable square. The client never runs flipsFor. One rules implementation, on the server, means the hint dots and the referee always agree.
Copying the rows (map((r) => [...r])) instead of handing out the live board is a habit from the other games: whatever the transport layer does with the object, it can't mutate the real game state.
The bot: honest about being simple
When you start a room alone, a bot can sit in the other seat. Reversi's bot is deliberately basic right now: it takes the legal list from the public state (or recomputes it from the board if it's missing) and picks a random legal move.
function reversi(state, bot) { if (state.turn !== bot) return null; const moves = state.legal?.length ? state.legal : legalReversi(state.board, bot); const m = pick(moves); return m ? { type: 'place', r: m[0], c: m[1] } : null; } That's simplified from the real file, but the behaviour is the same. Because the bot sends its move through the same onAction as a human, it can't cheat even by accident. A random Reversi bot is easy to beat once you learn that corners can never be flipped back, which is honestly not a bad way for a beginner to discover that rule. A stronger bot that weights corners and edges and avoids the squares next to empty corners would be a natural next step.
What I'd take away
- Write one function that answers "what does this move change?" and derive legality from it.
- Make passing automatic instead of a player action, so it can't be abused or misclicked.
- Let the server compute legal moves and ship them to the client, so hints and validation can't drift apart.
- Keep the board representation boring (
null,0,1) and the code will stay short.
If you want to see it in action, you can play Reversi against a friend or the bot at mympgames.com/play/reversi, and browse the other 60+ free multiplayer games at mympgames.com. No downloads, no sign-up wall.