Crown a king

A piece that moves to its king row becomes a king. The move and jump mutations can each take a piece there, and a subsequent mutation can also do it. Thus the crown rule is a reactor. The database runs a reactor in each write that changes a field that the reactor reads or writes. Add this reactor to definitions/checkers/boards.dust, before the export form.

dust291 B
;; A piece that reaches its king row becomes a king.
(defreactor crown
  {query {find [?piece]
          where [[?piece rank "uncrowned"]
                 [?piece king-row ?row]
                 [?piece square ?square]
                 [?square row ?row]]}
   patch {?piece {rank "king"}}})

defreactor has a name and a body map, and no parameters. The ;; line above it is a comment. The query selects each uncrowned piece whose square is on the row in its king-row field. The clause [?square row ?row] uses the same ?row as the king-row clause, so the two values must be equal. The patch sets the rank of each selected piece to "king".

The reactor reads rank, king-row, square and row. Each move and each jump writes square, so the database runs the reactor in each of these writes. The change of the reactor commits in the same transaction as the move. The export form does not name the reactor, so the SDK has no method for it, and the export form stays the same.

Run go run ./seed again to save the reactor. The database does not run a new reactor on the pieces that are in the database at that time. The reactor runs only in subsequent writes.

Create crown/main.go with this program. Black moves from 32 to 28. The Red piece on 27 moves to 32, on its king row. Black moves from 28 to 24, and then the Red king moves back from 32 to 27. An uncrowned piece cannot move back, so the last move shows that the piece is a king. The program then prints the board and the stats that db.Reactors() returns for each saved reactor.

go1.1 kB
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)
		}
	}()

	// Red reaches its king row on 32, and then the king moves back to 27.
	moves := [][2]int64{{32, 28}, {27, 32}, {28, 24}, {32, 27}}
	for i := range moves {
		move := moves[i]
		result, err := db.CheckersBoards.Move(move[0], move[1])
		if err != nil {
			log.Fatal(err)
		}
		if result.Transaction == nil {
			log.Fatalf("%d-%d: rejected", move[0], move[1])
		}
		fmt.Printf("%d-%d: moved\n", move[0], move[1])
	}

	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)
	}
	reactors, err := db.Reactors()
	if err != nil {
		log.Fatal(err)
	}
	for i := range reactors {
		fmt.Printf("%s: %d runs, %d changes\n", reactors[i].Name, reactors[i].Runs, reactors[i].Changes)
	}
}

Run go run ./crown. It prints:

text127 B
32-28: moved
27-32: moved
28-24: moved
32-27: moved
15: red uncrowned
24: black uncrowned
27: red king
crown: 5 runs, 1 changes

The program made four writes. The move to 32 ran the reactor in two rounds. The first round changed the rank of the Red piece, and the second round changed nothing. Each of the other three writes ran the reactor in one round that changed nothing.

Complete definitions

After this step, definitions/checkers/boards.dust contains these forms.

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

(defquery board [] [=> [... {number int? color string? rank string?}]]
  {find [?number ?color ?rank]
   where [[?piece square ?square]
          [?square number ?number]
          [?piece color ?color]
          [?piece rank ?rank]]
   order-by [?number]})

(defmutation move [from to] [int? int? => map?]
  {query {find [?game ?piece ?from ?to ?color ?next]
          where [[?game <game>]
                 [?game turn ?color]
                 [?game next ?next]
                 [?from number from]
                 [?from row ?row]
                 [?from col ?col]
                 [?piece square ?from]
                 [?piece color ?color]
                 [?piece rank ?rank]
                 [?piece forward ?forward]
                 [?to number to]
                 [?to <empty>]
                 [?to row ?to-row]
                 [?to col ?to-col]
                 (= (math/abs (- ?to-col ?col)) 1)
                 (= (math/abs (- ?to-row ?row)) 1)
                 (or (= ?rank "king") (= (- ?to-row ?row) ?forward))]}
   patch {?piece {square #link ?to}
          ?from {<empty>}
          ?to {<!empty>}
          ?game {turn ?next next ?color}}})

(defmutation jump [from to] [int? int? => map?]
  {query {find [?game ?piece ?captured ?from ?over ?to ?color ?next]
          where [[?game <game>]
                 [?game turn ?color]
                 [?game next ?next]
                 [?from number from]
                 [?from row ?row]
                 [?from col ?col]
                 [?piece square ?from]
                 [?piece color ?color]
                 [?piece rank ?rank]
                 [?piece forward ?forward]
                 [?to number to]
                 [?to <empty>]
                 [?to row ?to-row]
                 [?to col ?to-col]
                 [?captured color ?next]
                 [?captured square ?over]
                 [?over row ?over-row]
                 [?over col ?over-col]
                 (= (math/abs (- ?over-col ?col)) 1)
                 (= (math/abs (- ?over-row ?row)) 1)
                 (or (= ?rank "king") (= (- ?over-row ?row) ?forward))
                 (= (- ?to-col ?over-col) (- ?over-col ?col))
                 (= (- ?to-row ?over-row) (- ?over-row ?row))]}
   patch {?piece {square #link ?to}
          ?captured {square null}
          ?from {<empty>}
          ?over {<empty>}
          ?to {<!empty>}
          ?game {turn ?next next ?color}}})

;; A piece that reaches its king row becomes a king.
(defreactor crown
  {query {find [?piece]
          where [[?piece rank "uncrowned"]
                 [?piece king-row ?row]
                 [?piece square ?square]
                 [?square row ?row]]}
   patch {?piece {rank "king"}}})

(export setup board move jump)