[Cs254f11] Fwd: Help me fix this loop
Jack Laxson
jrl11 at hampshire.edu
Tue Oct 11 20:02:17 EDT 2011
---------- Forwarded message ----------
From: Jack L. <jackjrabbit at gmail.com>
Date: Tue, Oct 11, 2011 at 7:58 PM
Subject: Re: [Cs254f11] Help me fix this loop
To: Maxwell William Fair Levit <mwl10 at hampshire.edu>
Running the second loop without the last bit works, e.g. ...
(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)))
)
Lists all the positions but they're all 0. :)
Jack
On Tue, Oct 11, 2011 at 7:13 PM, Maxwell William Fair Levit <
mwl10 at hampshire.edu> wrote:
>
>
> 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
> _______________________________________________
> Cs254f11 mailing list
> Cs254f11 at lists.hampshire.edu
> https://lists.hampshire.edu/mailman/listinfo/cs254f11
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.hampshire.edu/pipermail/cs254f11/attachments/20111011/9bbd1928/attachment.htm>
More information about the Cs254f11
mailing list