我正在開發我的第一個合適的Clojure程序 - 一個象棋遊戲。我有以下幾點:Clojure的(讀線)返回零;不提示
(defn human-move [board]
(board-utils/print-board board)
(print "Enter your move, like this: 'E7 E5' ...")
(loop [raw-move (terminal-input)] ;;(read-line)]
(println "I just received" raw-move)
(if (re-matches #"[A-H][1-8]\s[A-H][1-8]" raw-move)
(parse-move raw-move)
(do
(println "Invalid format! There should be a letter, number, space, letter, and final number.")
(print "Try again: ")
(recur (read-line))))))
記下read-line
被註釋掉和terminal-input
替代的地方。 read-line
是給我一個NullPointerException,因此用於診斷目的:
(defn terminal-input []
(println "input")
(let [whatnot (read-line)]
(println "received" whatnot)
whatnot))
然後,當我打電話human-move
。
...
+---+---+---+---+---+---+---+---+
| P | P | P | P | P | P | P | P |
+---+---+---+---+---+---+---+---+
| R | N | B | Q | K | B | N | R |
+---+---+---+---+---+---+---+---+
Enter your move, like this: 'E7 E5' ...input
received nil
I just received nil
我從來沒有機會鍵入輸入內容。如果是Java,我會開始與垃圾收集器(例如Scanner.next()
)一起玩小遊戲,但在Clojure中,除了將(flush)
放在那裏之外,我不知道該做什麼。
這是SLIME的價值。
我也試過有terminal-input
提供虛擬數據,並得知我顯然使用loop
/recur
不正確。儘管如此,我還沒有調查過它,因爲我一直在分析read-line
問題。
在此先感謝。
順便說一句,我也添加了從minibuffer和y-or-n-p的支持。 – 2012-01-11 17:32:41
這真的很棒。我期待着有一個理由嘗試這個。 – amalloy 2012-01-11 18:41:42