Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: e9f8e99b76
Fetching contributors…

Cannot retrieve contributors at this time

59 lines (44 sloc) 1.577 kB
(ns joy.refs
"Examples for Ref in section 11.2"
(use [joy.mutation :only (dothreads!)]
[joy.a :only (neighbors)]))
(def initial-board
[[:- :k :-]
[:- :- :-]
[:- :K :-]])
(defn board-map [f bd]
(vec (map #(vec (for [s %] (f s))) bd)))
(defn reset!
"Resets the board state. Generally these types of functions are a
bad idea, but matters of page count force our hand."
[]
(def board (board-map ref initial-board))
(def to-move (ref [[:K [2 1]] [:k [0 1]]]))
(def num-moves (ref 0)))
(def king-moves (partial neighbors [[-1 -1] [-1 0] [-1 1] [0 -1] [0 1] [1 -1] [1 0] [1 1]] 3))
(defn good-move? [to enemy-sq] (when (not= to enemy-sq) to))
(defn choose-move [[[mover mpos][_ enemy-pos]]]
[mover (some #(good-move? % enemy-pos)
(shuffle (king-moves mpos)))])
(defn place [from to] to)
(defn move-piece [[piece dest] [[_ src] _]]
(alter (get-in board dest) place piece)
(alter (get-in board src ) place :-)
(alter num-moves inc))
(defn update-to-move [move]
(alter to-move #(vector (second %) move)))
(defn make-move []
(dosync
(let [move (choose-move @to-move)]
(move-piece move @to-move)
(update-to-move move))))
(defn go [move-fn threads times]
(dothreads! move-fn :threads threads :times times))
(defn bad-make-move []
(let [move (choose-move @to-move)]
(dosync (move-piece move @to-move))
(dosync (update-to-move move))))
(defn move-piece [[piece dest] [[_ src] _]]
(commute (get-in board dest) place piece)
(commute (get-in board src ) place :-)
(commute num-moves inc))
Jump to Line
Something went wrong with that request. Please try again.