Capture with a jump
A jump takes a piece over an adjacent piece of the other color to the empty
square behind it. The jumped piece is captured. Add this mutation to
definitions/checkers/boards.dust, before the export form.
(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}}})
The query starts with the same clauses as move. It then finds the captured
piece and the square between the source and the destination:
[?captured color ?next]selects a piece of the color that moves next, which is the color of the other player.[?captured square ?over]binds?overto the square of that piece.- The first three predicates make sure that
?overis one step from the source, in a direction that the piece can move. - The last two predicates make sure that the destination is one more step in the same direction.
The patch moves the piece, removes the captured piece from the board and
changes the turn. ?captured {square null} retracts the square field, so
the board query does not select the captured piece. The captured piece
stays in the database without a square. The two squares ?from and ?over
get the <empty> tag.
Replace the export form with this form, so that the SDK can call jump:
(export setup board move jump)
Run go run ./seed again. It generates the SDK again with the Jump method.
Create jump/main.go with this program. The Red piece on 18 jumps over the Black
piece on 23 and moves to 27. In checkers notation, an x marks a capture.
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 jumps from 18 over the black piece on 23 and lands on 27.
result, err := db.CheckersBoards.Jump(18, 27)
if err != nil {
log.Fatal(err)
}
if result.Transaction == nil {
log.Fatal("18x27: rejected")
}
fmt.Println("18x27: captured")
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 ./jump. It prints:
18x27: captured 15: red uncrowned 27: red uncrowned 32: black uncrowned
The Black piece on 23 is not on the board, and the Red piece is on 27.