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.
;; A piece that reaches its king row becomes a king.
#invoke [
"defreactor"
#local "crown"
{:query {:find [#local ?piece]
:where [
[#local ?piece #local "rank" "uncrowned"]
[#local ?piece #local "king-row" #local ?row]
[#local ?piece #local "square" #local ?square]
[#local ?square #local "row" #local ?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.
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:
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.
[
#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 [
"defmutation"
#local "move"
[#local "from" #local "to"]
[#local "int?" #local "int?" #local "=>" #local "map?"]
{:query {:find [
#local ?game
#local ?piece
#local ?from
#local ?to
#local ?color
#local ?next]
:where [
[#local ?game {:game true}]
[#local ?game #local "turn" #local ?color]
[#local ?game #local "next" #local ?next]
[#local ?from #local "number" #local "from"]
[#local ?from #local "row" #local ?row]
[#local ?from #local "col" #local ?col]
[#local ?piece #local "square" #local ?from]
[#local ?piece #local "color" #local ?color]
[#local ?piece #local "rank" #local ?rank]
[#local ?piece #local "forward" #local ?forward]
[#local ?to #local "number" #local "to"]
[#local ?to {:empty true}]
[#local ?to #local "row" #local ?to-row]
[#local ?to #local "col" #local ?to-col]
#invoke ["=" #invoke ["math/abs" #invoke ["-" #local ?to-col #local ?col]] 1]
#invoke ["=" #invoke ["math/abs" #invoke ["-" #local ?to-row #local ?row]] 1]
#invoke [
"or"
#invoke ["=" #local ?rank "king"]
#invoke ["=" #invoke ["-" #local ?to-row #local ?row] #local ?forward]]]}
:patch {:?piece {:square #link #local ?to}
:?from {:empty true}
:?to {:empty false}
:?game {:turn #local ?next
:next #local ?color}}}]
#invoke [
"defmutation"
#local "jump"
[#local "from" #local "to"]
[#local "int?" #local "int?" #local "=>" #local "map?"]
{:query {:find [
#local ?game
#local ?piece
#local ?captured
#local ?from
#local ?over
#local ?to
#local ?color
#local ?next]
:where [
[#local ?game {:game true}]
[#local ?game #local "turn" #local ?color]
[#local ?game #local "next" #local ?next]
[#local ?from #local "number" #local "from"]
[#local ?from #local "row" #local ?row]
[#local ?from #local "col" #local ?col]
[#local ?piece #local "square" #local ?from]
[#local ?piece #local "color" #local ?color]
[#local ?piece #local "rank" #local ?rank]
[#local ?piece #local "forward" #local ?forward]
[#local ?to #local "number" #local "to"]
[#local ?to {:empty true}]
[#local ?to #local "row" #local ?to-row]
[#local ?to #local "col" #local ?to-col]
[#local ?captured #local "color" #local ?next]
[#local ?captured #local "square" #local ?over]
[#local ?over #local "row" #local ?over-row]
[#local ?over #local "col" #local ?over-col]
#invoke ["=" #invoke ["math/abs" #invoke ["-" #local ?over-col #local ?col]] 1]
#invoke ["=" #invoke ["math/abs" #invoke ["-" #local ?over-row #local ?row]] 1]
#invoke [
"or"
#invoke ["=" #local ?rank "king"]
#invoke ["=" #invoke ["-" #local ?over-row #local ?row] #local ?forward]]
#invoke [
"="
#invoke ["-" #local ?to-col #local ?over-col]
#invoke ["-" #local ?over-col #local ?col]]
#invoke [
"="
#invoke ["-" #local ?to-row #local ?over-row]
#invoke ["-" #local ?over-row #local ?row]]]}
:patch {:?piece {:square #link #local ?to}
:?captured {:square nil}
:?from {:empty true}
:?over {:empty true}
:?to {:empty false}
:?game {:turn #local ?next
:next #local ?color}}}]
;; A piece that reaches its king row becomes a king.
#invoke [
"defreactor"
#local "crown"
{:query {:find [#local ?piece]
:where [
[#local ?piece #local "rank" "uncrowned"]
[#local ?piece #local "king-row" #local ?row]
[#local ?piece #local "square" #local ?square]
[#local ?square #local "row" #local ?row]]}
:patch {:?piece {:rank "king"}}}]
#invoke ["export" #local "setup" #local "board" #local "move" #local "jump"]]