1. 安装 Common Lisp 实现
这里以 SBCL 为例,在 Armbian 中,你可以通过包管理器来安装 SBCL。打开终端,执行以下命令:
sudo apt update
sudo apt install sbcl
这个命令会先更新软件包列表,然后安装 SBCL。
demo.lisp
;; 生成指定范围内的随机不重复数字列表
(defun generate-random-numbers (min max count)
(let ((numbers (loop for i from min to max collect i))
result)
(dotimes (i count)
(let ((index (random (length numbers))))
(push (nth index numbers) result)
(setf numbers (remove (nth index numbers) numbers))))
(sort result #'<)))
;; 生成双色球中奖号码
(defun generate-winning-numbers ()
(let ((red-balls (generate-random-numbers 1 33 6))
(blue-ball (generate-random-numbers 1 16 1)))
(list red-balls blue-ball)))
;; 让用户输入红球号码
(defun input-red-balls ()
(format t "请输入 6 个不重复的红球号码(范围 01 - 33),用空格分隔:")
(let ((input (read-line)))
(let ((numbers (mapcar #'parse-integer (cl-ppcre:split " " input))))
(when (and (= (length numbers) 6)
(every (lambda (n) (<= 1 n 33)) numbers)
(equal (sort (copy-list numbers) #'<) (remove-duplicates numbers :test #'=)))
numbers))))
;; 让用户输入蓝球号码
(defun input-blue-ball ()
(format t "请输入 1 个蓝球号码(范围 01 - 16):")
(let ((input (read-line)))
(let ((number (parse-integer input)))
(when (<= 1 number 16)
(list number)))))
;; 判断中奖情况
(defun check-winning (user-numbers winning-numbers)
(let* ((user-red (car user-numbers))
(user-blue (cadr user-numbers))
(winning-red (car winning-numbers))
(winning-blue (cadr winning-numbers))
(red-matches (count-if (lambda (n) (member n winning-red)) user-red))
(blue-match (equal user-blue winning-blue)))
(cond ((and (= red-matches 6) blue-match) "一等奖")
((= red-matches 6) "二等奖")
((and (= red-matches 5) blue-match) "三等奖")
((or (and (= red-matches 5) (not blue-match))
(and (= red-matches 4) blue-match)) "四等奖")
((or (and (= red-matches 4) (not blue-match))
(and (= red-matches 3) blue-match)) "五等奖")
((or (and (<= red-matches 2) blue-match)) "六等奖")
(t "未中奖"))))
;; 主游戏函数
(defun play-double-color-ball ()
(let ((winning-numbers (generate-winning-numbers))
user-numbers)
(format t "欢迎参加双色球游戏!~%")
(let ((red (input-red-balls)))
(when red
(let ((blue (input-blue-ball)))
(when blue
(setf user-numbers (list red blue))
(format t "本期中奖红球号码:~{~2,'0d ~},蓝球号码:~2,'0d~%" (car winning-numbers) (car (cadr winning-numbers)))
(format t "你选择的红球号码:~{~2,'0d ~},蓝球号码:~2,'0d~%" (car user-numbers) (car (cadr user-numbers)))
(format t "你中了:~a~%" (check-winning user-numbers winning-numbers))))))))
;; 运行游戏
(play-double-color-ball)
运行
sbcl --load demo.lisp