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.

dust907 B
(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}}})

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:

dust26 B
(export setup board 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.

go927 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)
		}
	}()

	// 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:

text104 B
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.