Conmon lisp Demo

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
相关推荐
嘴对嘴编程25 分钟前
oracle数据泵操作
数据库·oracle
这儿有一堆花41 分钟前
Kali Linux 2025.1a:主题焕新与树莓派支持的深度解析
linux·运维·服务器
wanhengidc1 小时前
算力服务器和普通服务器之间的不同之处
运维·服务器
lilye663 小时前
程序化广告行业(55/89):DMP与DSP对接及数据统计原理剖析
java·服务器·前端
SKYDROID云卓小助手4 小时前
三轴云台之相机技术篇
运维·服务器·网络·数码相机·音视频
·薯条大王6 小时前
MySQL联合查询
数据库·mysql
morris1318 小时前
【redis】redis实现分布式锁
数据库·redis·缓存·分布式锁
hycccccch9 小时前
Canal+RabbitMQ实现MySQL数据增量同步
java·数据库·后端·rabbitmq
wirepuller_king9 小时前
创建Linux虚拟环境并远程连接,finalshell自定义壁纸
linux·运维·服务器
这个懒人9 小时前
深入解析Translog机制:Elasticsearch的数据守护者
数据库·elasticsearch·nosql·translog