Ok, I've read that what I want to do is a no no. But this is the sort of
thing I did in Scheme about 20 years ago (and because of that I'm probably
misremembering ;-)).
Basically I'm learning clojure and thought I'd write a tic tac toe game.
But not any tic tac toe, I want to write one where I can have multiple
games going simultaneously. Something like:
(def g1 (new-game))
(def g2 (new-game))
(g1 :x 0)
(g1 :print)
(g2 :x 5)
(g2 :print)
So the schemer in me (and probably the imperative programmer as well)
thought I could return a clojure that encapsulates the board value,
something like this:
(defn new-game []
(let [board (into [] (repeat 9 nil))]
(fn [n i]
(cond
(= n :x)(set! board (assoc board i 'x))
(= n :o)(set! board (assoc board i 'o))
(= n :print) (println board)))))
Of course I get an error saying I can't bind to the non-mutable board.
I'm really new to Clojure, so apologies if this is really basic for this
list. Can I do what I want or can someone point me in the right direction?
I've seen some other tic tac toe implementations on github, but they use
recur to track state and I was hoping there was a cleaner idiomatic way
than that.
Thanks!
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en