Move a piece
A move takes a piece from one square to an adjacent diagonal square. This
step adds a move mutation whose query contains the rules of a move. Add this
mutation to definitions/checkers/boards.dust, before the export form.
#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}}}]The parameters and the contract
[from to] is the parameter vector, and [int? int? => map?] is the
contract. The two parameters are the numbers of the source and destination
squares, and the result is a map. The SDK reads the contract, so the generated
Move method takes two int64 values.
The rules in the query
Each clause of where must match, or the query selects no row. The first
three clauses read the turn and next colors of the game. The next clauses
find the square with the number from, the piece on that square, and the
piece's color, rank and forward step. The clause [?piece color ?color]
uses the same ?color as the turn clause, so the piece must have the color
that has the turn.
The clause [?to <empty>] requires the destination square to have the
<empty> tag. The three predicates at the end compare numbers. The column and
the row must each change by 1. The row must change by the forward step of
the piece, or the piece must be a king.
The patch
The patch writes four entities for the selected row. square #link ?to links
the piece to the destination square. Without #link, the field would store
the entity ID of the square as an integer. ?from {<empty>} adds the tag to
the source square, and ?to {<!empty>} removes it from the destination. A
tag that starts with ! retracts the tag. ?game {turn ?next next ?color}
interchanges the two colors, so the other player has the turn.
A move that does not obey the rules selects no row, so the patch writes
nothing. The mutation then publishes no transaction, and the Transaction
field of the Go result is nil.
Make the move
Replace the export form with this form, so that the SDK can call move:
#invoke ["export" #local "setup" #local "board" #local "move"]Run go run ./seed again. It saves the changed namespace and generates the SDK
again with the Move method.
Create move/main.go with this program. Red moves from 11 to 15. Then Red
tries to move from 15 to 19 before Black moves. The program prints the result
of each move and then the board.
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 moves from 11 to 15, then tries to move again before Black.
moves := [][2]int64{{11, 15}, {15, 19}}
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 {
fmt.Printf("%d-%d: rejected\n", move[0], move[1])
continue
}
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)
}
}Run go run ./move. It prints:
11-15: moved 15-19: rejected 15: red uncrowned 18: red uncrowned 27: black uncrowned 32: black uncrowned
The second move did not obey the turn rule, so the database did not change. The board shows the Red piece on 15 and the other pieces on their start squares.