Prepare the board

Do the Set up Go step of the quickstart if you have not done it. Use the Go binding and native library from the Mentat checkout containing the streaming event calls. From the repository root, run task web:test-checkers to build the library and verify the tutorial. In the Odin development shell, create your module:

sh390 B
export MENTAT_ROOT="$PWD"
export CGO_LDFLAGS="-L$MENTAT_ROOT/implementations/odin/build"
export LD_LIBRARY_PATH="$MENTAT_ROOT/implementations/odin/build"
mkdir checkers && cd checkers
mkdir -p seed setup move subscribe jump crown definitions/checkers
go mod init example.com/checkers
go mod edit -require=stardust@v0.0.0 \
  -replace=stardust="$MENTAT_ROOT/implementations/odin/bindings/go"

Run the programs in this tutorial one at a time. Only one process can have checkers.db open at a time. Each program closes the database before the next program starts.

The squares

Checkers uses the 32 dark squares of an 8 by 8 board. As in checkers notation, this tutorial gives each dark square a number from 1 to 32. Rows go from 0 at the top to 7 at the bottom, and columns go from 0 at the left to 7 at the right:

text331 B
         col 0  1  2  3  4  5  6  7
row 0         .  1  .  2  .  3  .  4
row 1         5  .  6  .  7  .  8  .
row 2         .  9  . 10  . 11  . 12
row 3        13  . 14  . 15  . 16  .
row 4         . 17  . 18  . 19  . 20
row 5        21  . 22  . 23  . 24  .
row 6         . 25  . 26  . 27  . 28
row 7        29  . 30  . 31  . 32  .

Red moves down the board, to higher rows. Black moves up the board, to lower rows. The game starts from a position in the middle of a game. Red pieces are on 11 and 18, Black pieces are on 27 and 32, and Red has the turn.

The board definitions

Create definitions/checkers/boards.dust with these forms. The path below definitions names the checkers/boards namespace. Subsequent steps add forms to this file.

edn7.7 kB
[
  #invoke [
      "defmutation"
      #local "setup"
      []
      {:patch {:_game    {:game true
                          :turn "red"
                          :next "black"}
               :_1       {:square true
                          :empty  true
                          :number 1
                          :row    0
                          :col    1}
               :_2       {:square true
                          :empty  true
                          :number 2
                          :row    0
                          :col    3}
               :_3       {:square true
                          :empty  true
                          :number 3
                          :row    0
                          :col    5}
               :_4       {:square true
                          :empty  true
                          :number 4
                          :row    0
                          :col    7}
               :_5       {:square true
                          :empty  true
                          :number 5
                          :row    1
                          :col    0}
               :_6       {:square true
                          :empty  true
                          :number 6
                          :row    1
                          :col    2}
               :_7       {:square true
                          :empty  true
                          :number 7
                          :row    1
                          :col    4}
               :_8       {:square true
                          :empty  true
                          :number 8
                          :row    1
                          :col    6}
               :_9       {:square true
                          :empty  true
                          :number 9
                          :row    2
                          :col    1}
               :_10      {:square true
                          :empty  true
                          :number 10
                          :row    2
                          :col    3}
               :_11      {:square true
                          :number 11
                          :row    2
                          :col    5}
               :_12      {:square true
                          :empty  true
                          :number 12
                          :row    2
                          :col    7}
               :_13      {:square true
                          :empty  true
                          :number 13
                          :row    3
                          :col    0}
               :_14      {:square true
                          :empty  true
                          :number 14
                          :row    3
                          :col    2}
               :_15      {:square true
                          :empty  true
                          :number 15
                          :row    3
                          :col    4}
               :_16      {:square true
                          :empty  true
                          :number 16
                          :row    3
                          :col    6}
               :_17      {:square true
                          :empty  true
                          :number 17
                          :row    4
                          :col    1}
               :_18      {:square true
                          :number 18
                          :row    4
                          :col    3}
               :_19      {:square true
                          :empty  true
                          :number 19
                          :row    4
                          :col    5}
               :_20      {:square true
                          :empty  true
                          :number 20
                          :row    4
                          :col    7}
               :_21      {:square true
                          :empty  true
                          :number 21
                          :row    5
                          :col    0}
               :_22      {:square true
                          :empty  true
                          :number 22
                          :row    5
                          :col    2}
               :_23      {:square true
                          :empty  true
                          :number 23
                          :row    5
                          :col    4}
               :_24      {:square true
                          :empty  true
                          :number 24
                          :row    5
                          :col    6}
               :_25      {:square true
                          :empty  true
                          :number 25
                          :row    6
                          :col    1}
               :_26      {:square true
                          :empty  true
                          :number 26
                          :row    6
                          :col    3}
               :_27      {:square true
                          :number 27
                          :row    6
                          :col    5}
               :_28      {:square true
                          :empty  true
                          :number 28
                          :row    6
                          :col    7}
               :_29      {:square true
                          :empty  true
                          :number 29
                          :row    7
                          :col    0}
               :_30      {:square true
                          :empty  true
                          :number 30
                          :row    7
                          :col    2}
               :_31      {:square true
                          :empty  true
                          :number 31
                          :row    7
                          :col    4}
               :_32      {:square true
                          :number 32
                          :row    7
                          :col    6}
               :_red-1   {:piece    true
                          :color    "red"
                          :rank     "uncrowned"
                          :forward  1
                          :king-row 7
                          :square   #local "_11"}
               :_red-2   {:piece    true
                          :color    "red"
                          :rank     "uncrowned"
                          :forward  1
                          :king-row 7
                          :square   #local "_18"}
               :_black-1 {:piece    true
                          :color    "black"
                          :rank     "uncrowned"
                          :forward  -1
                          :king-row 0
                          :square   #local "_27"}
               :_black-2 {:piece    true
                          :color    "black"
                          :rank     "uncrowned"
                          :forward  -1
                          :king-row 0
                          :square   #local "_32"}}}]
  #invoke [
      "defquery"
      #local "board"
      []
      [
        #local "=>"
        [
          #local "..."
          {:number #local "int?"
           :color  #local "string?"
           :rank   #local "string?"}]]
      {:find     [#local ?number #local ?color #local ?rank]
       :where    [
         [#local ?piece #local "square" #local ?square]
         [#local ?square #local "number" #local ?number]
         [#local ?piece #local "color" #local ?color]
         [#local ?piece #local "rank" #local ?rank]]
       :order-by [#local ?number]}]
  #invoke ["export" #local "setup" #local "board"]]

The setup mutation

setup has an empty parameter vector and a patch that makes all the entities of the game. Each key that starts with _ is a temporary name for a new entity. The _game entity has the <game> tag and two fields: turn is the color that moves now, and next is the color that moves after it.

Each square entity has the <square> tag and its number, row and col. An empty square also has the <empty> tag, so <square empty> is one tag group with two tags. The destination of a move must have this tag.

Each piece entity has the <piece> tag, a color and a rank. A new piece starts with the rank "uncrowned". forward is the row step of a forward move: 1 for Red and -1 for Black. king-row is the row where the piece becomes a king. The value _11 in square _11 is the temporary name of square 11, so the square field stores a link to that square.

The board query

The vector [=> [... {number int? color string? rank string?}]] after the empty parameter vector is a result contract. Nothing is before => because the query has no parameters. After =>, the contract gives the result: a list of maps with these three fields. The SDK reads the contract and gives each generated row the fields Number int64, Color string and Rank string.

The where clauses select each piece that has a square link, the number of that square, and the color and rank of the piece. A captured piece has no square, so the query does not select it. The find member gives one row for each selected piece, and order-by puts the rows in the sequence of the square numbers.

The export form

export names the definitions that the SDK makes available. Here the SDK generates the Setup and Board methods.

Save the definitions

Create seed/main.go with this program. It is the seed program of the quickstart with the database path checkers.db. It saves the files in definitions in one transaction, and the SDK plugin generates the SDK after the definitions commit.

go630 B
package main

import (
	"fmt"
	"log"

	"stardust"
)

func main() {
	db, err := stardust.OpenConfig(stardust.Config{
		Plugins: &stardust.Plugins{
			Stdlib:   true,
			Database: &stardust.DatabaseSettings{Path: "checkers.db"},
			SDKs: &stardust.SDKSettings{
				Go: &stardust.GoSDKSettings{Directory: "./sdk"},
			},
		},
	})
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		if err := db.Close(); err != nil {
			log.Print(err)
		}
	}()

	response, err := db.UpdateDefinitionsFromDir("./definitions")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Updated %d namespace(s); generated ./sdk\n", len(response.Namespaces))
}

Run go run ./seed. It creates checkers.db and the sdk directory.

Place the pieces

Create setup/main.go with this program. It calls Setup once and then prints each row of Board.

go671 B
package main

import (
	"fmt"
	"log"

	"example.com/checkers/sdk"
)

func main() {
	db, err := sdk.Open(sdk.Config{Plugins: &sdk.Plugins{
		Stdlib:   true,
		Database: &sdk.DatabaseSettings{Path: "checkers.db"},
	}})
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		if err := db.Close(); err != nil {
			log.Print(err)
		}
	}()

	result, err := db.CheckersBoards.Setup()
	if err != nil {
		log.Fatal(err)
	}
	if result.Transaction == nil {
		log.Fatal("setup wrote nothing")
	}
	pieces, err := db.CheckersBoards.Board()
	if err != nil {
		log.Fatal(err)
	}
	for i := range pieces {
		fmt.Printf("%d: %s %s\n", pieces[i].Number, pieces[i].Color, pieces[i].Rank)
	}
}

Run go run ./setup one time. It prints:

text75 B
11: red uncrowned
18: red uncrowned
27: black uncrowned
32: black uncrowned

Each run of setup makes another board. To start again, remove checkers.db and the sdk directory. Then run go run ./seed and go run ./setup.