好的,最后一个问题,我将用 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)
随机行动?
您需要在程序开始时播种随机状态。
(setf *random-state* (make-random-state t))
;; # this initializes the global random state by
;; "some means" (e.g. current time.)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)