0

我正在学习 Clojure 并尝试实现一个简单的井字游戏(或 morpion)。但我正在努力避免任何 ref/atom/agent ...

我知道我可以在控制台应用程序中轻松地做到这一点,因为它不是事件驱动的,所以我会确切地知道何时传递一个新的板值。我是说

  • 我的板将是向量的向量([[:circle 0 :cross][:any :circle :empty][:cross :none :none]] 其中只有 :circle 和 :cross 值很重要)
  • 我会有一个简单的基于文本的方法,它需要一个板并返回一个字符串

但是,每当我想实现一个图形面板时,我想知道我怎样才能做同样的事情。例如 :

  • 我正在创建 javax.swing.JPanel 的一个实例(请不要关心这里使用的其他方法):

    (defn make-board-panel
    "Builds the board JPanel, whose state is given by board argument,
    and turn-piece argument (must be either :circle or :cross, I mean the next
    value to set in the board)."
    [board turn-piece]
    {:pre ['morpion.core/is-board? board, 'morpion.core/is-a-player-piece? turn-piece]}
      (proxy [JPanel MouseListener] []
        (paintComponent [g]
            (proxy-super paintComponent g)
            (paint-board-lines g)
            (paint-board board g)
        )
        (mouseClicked [e]
            (if (and (abs-coord-in-cell? (.getX e)) (abs-coord-in-cell? (.getY e)))
                (let [cell-x (abs-coord-to-rel (.getX e))
                        cell-y (abs-coord-to-rel (.getY e))]
                    ;; Here I compute the new board value
                )  
                nil ;; Here I wish I could return the new board value to the caller of make-board-panel, but this seems impossible !
            ) 
        )
        (mouseEntered [e])
        (mouseExited [e])
        (mousePressed [e])
        (mouseReleased [e])
       )
    )
    
  • 但似乎我无法从 Panel 的 mouseClicked 事件中获取板的新值:除非我引入状态变量。

那么有没有解决方法:

我的完整项目的来源:

(由于 Igrapenthin 的评论,我试图改进,但我仍然失败了。)

4

1 回答 1

2
(defn make-board-panel
 [board turn-piece output-fn]
 (proxy [JPanel MouseListener] []
   ;; ...
   (mouseClicked [e]
     (when (and (abs-coord-in-cell? (.getX e)) 
           (abs-coord-in-cell? (.getY e)))
       (let [cell-x (abs-coord-to-rel (.getX e))
             cell-y (abs-coord-to-rel (.getY e))]
          ;; Here I compute the new board value
          (output-fn new-board-value))))
    ;; ...
    ))
于 2013-08-27T16:08:21.840 回答