Reversi
Place discs to bracket and flip your opponent's lines; pass when you can't place.
- Players
- 2 players
- Origin
- Traditional, 19th century
- Rules file
- 100 lines · BGDL 0.4 draft, runs on 1.0 · no hooks
- Perfect information
- Grid board
- Passing
The game
A disc must be placed so that it and another disc of the mover's color bracket one or more straight lines of opposing discs, in any of the eight directions; every bracketed disc flips. A player with no legal placement passes. When neither side can place, the side with more discs wins. Black moves first.
How it's built in BGDL
- One function finds the discs bracketed in a direction; another collects every disc a placement would flip.
- Passing is a real move that's only legal when there's no placement, and the game ends when neither side can place.
Tested: Move generation matches the published move counts from the starting position (perft).
The rules file
Its top-level sections:dsl, game, boards, components, data, zones, consts, functions, setup, flow, actions, triggers, end.
reversi.rules.yaml
# Reversi, a conformance game (issue #32): passing, where a seat with no legal placement passes and the game
# goes on, ending only when neither seat can place; and flipping whole lines. Move counts from the start position are
# published (perft), so move generation can be checked against them.
# Rules: a disc must be placed so that it and another disc of the mover's color bracket one or more straight lines of
# the opponent's discs, in any of the eight directions; every bracketed disc flips. Black moves first. When neither side
# can place, the side with more discs wins.
dsl: "0.4"
game:
id: reversi
name: Reversi
players: { min: 2, max: 2 }
seats:
topology: ring
names: [black, white] # seat 0 = black, who moves first
boards:
board:
kind: grid
size: [8, 8]
labels: { x: "abcdefgh", y: "12345678" }
components:
disc:
props:
owner: seat
data:
startPosition:
of: disc
rows:
- { owner: white, at: [d4, e5] }
- { owner: black, at: [d5, e4] }
zones:
square: { kind: cell, board: board, of: disc, capacity: 1, visibility: public }
consts:
directions: "[(x: 1, y: 0), (x: -1, y: 0), (x: 0, y: 1), (x: 0, y: -1), (x: 1, y: 1), (x: 1, y: -1), (x: -1, y: 1), (x: -1, y: -1)]"
functions:
# The opponent's discs bracketed in one direction from a square, or none if the line isn't closed by a disc of s.
bracketed:
params: { at: square, d: "(x: int, y: int)", s: seat, run: "list<square>" }
returns: "list<square>"
expr: >
let next = offset(at, d.x, d.y);
next == null || pieceAt(next) == null ? []
: pieceAt(next).owner == s ? run
: bracketed(next, d, s, run.concat([next]))
# Every disc a placement on a square would flip.
flips:
params: { at: square, s: seat }
returns: "list<square>"
expr: "directions.selectMany(d => bracketed(at, d, s, []))"
placements:
params: { s: seat }
returns: "list<square>"
expr: "board.cells().where(sq => pieceAt(sq) == null && flips(sq, s).any())"
discs:
params: { s: seat }
returns: int
expr: "pieces(s).count()"
setup:
- createFrom: { table: startPosition, into: square, positionColumn: at }
flow:
- phase: play
turns:
order: { from: "seat(black)", direction: clockwise }
budget: 1
actions: [place, pass]
actions:
place:
choose:
- at: { from: "placements(self)" }
effect:
- let: { name: flipped, value: "flips(at, self)" }
- create: { type: disc, props: { owner: self }, into: at }
- forEach: { in: flipped, as: sq, do: [ { set: { target: pieceAt(sq).owner, value: self } } ] }
# A seat with no placement must pass; it can't pass otherwise.
pass:
require: "placements(self).isEmpty()"
effect: []
triggers:
# The game ends when neither side can place.
- event: afterAction
when: "placements(seat(black)).isEmpty() && placements(seat(white)).isEmpty()"
do:
- if:
cond: "discs(seat(black)) == discs(seat(white))"
then: [ { endGame: { draw: true, reason: equal discs } } ]
else: [ { endGame: { winner: "discs(seat(black)) > discs(seat(white)) ? seat(black) : seat(white)", reason: more discs } } ]
end:
scoring: { win: 1, draw: 0.5, loss: 0 }
rating: eloWrite your own game in BGDL
Start free and describe your game's rules, or book a demo and we'll walk through your rulebook with you.