Hi again,
It turns out that reduce is exactly what I need...I didn't know this but
there is a handy 'reduced' fn that makes it easy to terminate from
within a reduce at any given time. At least this is what i understand
from the docs...so the final thing looks like this:
(defn tournament
"Starts a tournament between the 2 players (-1 1). If there is no
winner, returns the entire history (vector) of
the tournament after 100 moves. If there is a winner, a map will be
returned containing the :winner and the :history."
[sb depth]
(reduce
(fn [history dir]
(let [cb (peek history)
win-dir (jit-referee cb)] ;;just-in-time referee looks for
missing kings
(if win-dir (reduced {:winner win-dir :history history})
;;terminate with this map
(conj history (-> (chess-best-move dir cb depth)
(:move)
(try-move))))))
[sb] (take 100 (cycle '(-1 1))))) ;;50 moves each should be enough
Ok, it's certainly not a one-liner but it's pretty clear and readable so
I'm happy :-)
Jim
On 16/09/12 11:59, Jim - FooBar(); wrote:
Hi all,
I'm trying to come up with a way to create a 'tournament' fn that
basically alternates between players (in turns) and calls soem 'move'
fn on each. Now, obviously this can be done with loop/recur no
problem, however perhaps a combination of cycle & iterate is more
appropriate...so I'm, thinking something along these lines :
(defn tournament [board] ;;no need for players - directions are standard
(vec ;;return a vector of the entire history of this tournament
(iterate #(chess-best-move (cycle [-1 1]) ) board)))
;;chess-best-move returns new board
This is obviously not good enough...cycle is in the wrong place! I
usually use cycle in combination with map rather than iterate and now
I'm at a loss...I can't exactly use map in this case cos i need the
calls to 'move' to be chained (passing the new board to the next
one)...The truth is I can use reduce like this:
(defn tournament [b]
(reduce
(fn [history dir]
(conj history (chess-best-move dir (peek history))))
[b] (take 100 (cycle '(-1 1))))) ;;100 moves maximum
I just thought iterate is a better fit...can anyone think how this can
be done in a one-liner using iterate+cycle or am I at a good path with
reduce?
Jim
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en