A shared checkers board

Note

Prerequisite: Complete the Go quickstart before starting this tutorial.

Rosie and Bob play checkers on a shared board. Rosie plays Red and Bob plays Black. Their application shows the board, accepts moves from each player, and shows the new positions. Each move must obey the rules of the game. The player must have the turn, an uncrowned piece moves one square forward on a diagonal, and the destination square must be empty.

To capture, jump over an opponent’s piece and land on the empty square just beyond it. When a piece reaches the far row, it becomes a king and can also move backward. These rules apply to each move from each program. When Rosie's program has an error, the board must still reject an incorrect move.

In this tutorial, you write Go programs that play part of a game on that board. You save the game rules as DUST definitions and generate a Go SDK.

Only one process can have the database file open at a time. Run the example programs one at a time. The subscription program makes and observes a move in that process, so one program shows both sides of the game.

Without Stardust

A usual design keeps the rules in the application. Each program examines a move before it sends it, or a server process examines each move for all programs. When each program has its own copy of the rules, two releases of a program can give different results for one move. A program with an error can write an incorrect move to the database.

A server process that examines each move must read the board, examine the move, and then write it. When it reads and writes in different transactions, two moves can pass the examination before the database records either move. The process must lock the board or try the write again. To show the moves of the other player, each program reads the board again at an interval or reads a different message channel.

With Stardust

Stardust keeps the rules in the database with the board. A move is a mutation whose query selects the piece, its square and the destination. The rules are clauses of that query. A move that does not obey the rules selects no rows, so the mutation writes nothing. The new position of the piece and the change of turn commit in one transaction, and all writes use one writer.

A reactor crowns a piece in the same write that moves it to the far row. A subscription to the board query keeps the other player's board current. The Go programs call the generated SDK and contain no rules.

After crowning a king, you add multiplayer events to the same game. Preview legal jumps, submit both captures in one turn, and reopen the saved board after a missed notification.