[Cs254f11] Help me fix this loop
Maxwell William Fair Levit
mwl10 at hampshire.edu
Tue Oct 11 19:13:56 EDT 2011
Hey all, I'm writing code that takes turns placing black and white (1 and 2)
pieces on a Go board. I'm running into trouble with the loop that is supposed
to actually place a bunch of pieces.
(def board
(atom
(zipmap
(reduce concat
(for [y (range 19)]
(map vector (repeat 19 y )
(for [x (range 19)] x))))
(vec (repeat 361 0)))))
;; defines a 19x19 board with all 361 entires set to 0.
(defn place-peice
[x y z]
(swap! board update-in [[x y]] (fn [j] z) ))
;;Sets the value of the space with coordinates [x y] to z.
(loop [c 1 x (rand-int 19) y (rand-int 19)]
(if (and (< c 3) (zero? (get @board x y)))
(do (place-peice x y (if (even? x) 1 2))
(recur (inc c) (rand-int 19) (rand-int 19)))
(recur c (rand-int 19) (rand-int 19))))
This just crashes clooj, presumably because its going infinitely, since I set
the number of iterations low enough that it shouldn't just be an efficiency
issue.
The other setup I had which gave me a "Must recur from tail position" error. was
this.
(loop [c 1 x (rand-int 19) y (rand-int 19)]
(if (and (< c 3) (zero? (get @board x y)))
(place-peice x y (if (even? x) 1 2))
(recur c (rand-int 19) (rand-int 19)))
(recur (inc c) (rand-int 19) (rand-int 19)))
Can someone tell me why these don't work and/or how to fix them?
Thanks,
-Max
More information about the Cs254f11
mailing list