【发布时间】:2011-05-01 08:20:22
【问题描述】:
好的,最后一个问题,我将用 Common Lisp 完成我的猜数字游戏! :D 每当游戏开始时(或在第一场比赛后开始新的比赛),都会调用以下函数。
;;; Play the game
(defun play ()
;; If it's their first time playing this session,
;; make sure to greet the user.
(unless (> *number-of-guesses* 0)
(welcome-user))
;; Reset their remaining guesses
(setq *number-of-guesses* 0)
;; Set the target value
(setq *target*
;; Random can return float values,
;; so we must round the result to get
;; an integer value.
(round
;; Add one to the result, because
;; (random 100) yields a number between
;; 0 and 99, whereas we want a number
;; from 1 to 100 inclusive.
(+ (random 100) 1)))
(if (eql (prompt-for-guess) t)
(play)
(quit)))
所以按理说,每次玩家开始游戏时,*target* 应该被设置为 1-100 之间的新随机整数。但是,*target* 每次都默认为 82。如何让(random) 随机执行...?
【问题讨论】:
标签: random lisp common-lisp sbcl