On Fri, Jan 2, 2009 at 4:04 PM, Chouser <chou...@gmail.com> wrote:
>
> n Fri, Jan 2, 2009 at 4:47 PM, Mark Volkmann <r.mark.volkm...@gmail.com> 
> wrote:
>>
>> On Fri, Jan 2, 2009 at 3:09 PM, Chouser <chou...@gmail.com> wrote:
>>>
>>> On Fri, Jan 2, 2009 at 4:05 PM, Tom Ayerst <tom.aye...@gmail.com> wrote:
>>>> That def inside a function doesn't look right but I'm a noob at this too.  
>>>> I
>>>> managed to run the snake off the board which suggests the concurrency is 
>>>> not
>>>> quite right.
>>>
>>> Calling 'def' to like this is much worse than a lack of comments,
>>> especially in code meant to teach.
>>
>> Thanks for the feedback! I'll gladly change it to use atoms or agents
>> or refs. I'm just not sure which one is most appropriate. What do you
>> think I should use?
>
> I don't feel I have much authority in the realm of designing
> concurrent programs, but here are a couple thoughts:
>
> It seems to me that 'apple' and 'snake' together describe the state of
> the game, and therefore need their changes coordinated.

I'm thinking their changes don't need to be coordinated. Here's why.
The only state for an apple is it's color and cell. The color never
changes. The cell only changes when the snake eats it. That is
determined in the actionPerformed method which is also responsible for
triggering movement of the snake, thus changing its state.
actionPerformed is called on the Swing event dispatch thread. That
means there can never multiple threads running actionPerformed
concurrently. I think this is why my code works using def, despite it
being bad form.

> This suggests they should both be refs.
>
> I think it might also be very natural to use a single agent with
> Thread/sleep and send-off to itself instead of the Timer.  This would
> detangle the actionPerformed method from the panel, and allow you to
> give the action function a meaningful name instead.

It would also invalidate what I said above about knowing that the
actionPerformed code was always being executed from a single thread.
Do you think it's a good idea to do that anyway?

-- 
R. Mark Volkmann
Object Computing, Inc.

--~--~---------~--~----~------------~-------~--~----~
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
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to