在循环中调用函数(Common Lisp) [英] Call a function while in a loop (Common Lisp)

查看:87
本文介绍了在循环中调用函数(Common Lisp)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个Lisp控制台生存游戏,我试图添加一个函数,直到a = b,显示".每一秒.然后,当a = b时,将"hurt"变量设置为true,如果/true,则将"health"减1,直到用户和"hurt"变量调用"use-medkit"功能设置为false并且退出两个循环.

I am making a console Lisp survival game and I am trying to add a function where until a = b, show "." every second. Then, when a = b, set a "hurt" variable to true, and if/when that variable is true, subtract "health" by 1 until the "use-medkit" function is invoked by the user and the "hurt" variable is set false and you exit both loops.

我遇到的问题是,当系统提示我使用"use-medkit"函数并键入该函数时,它不评估我输入的任何内容,并始终从健康"中减去1.循环运行时如何调用用户输入的函数?

The problem I am having is when I am prompted to use the "use-medkit" function and I type it in, it doesn't evaluate anything that I input and keeps subtracting 1 from "health". How can I call a user-inputted function while a loop is running?

这是我的代码:

(setq a (random 11)) ; Random from 0 - 10
(setq b (random 11)) ; ^^^^^^^^^^^^^^^^^^
(setq hurt 0)
(setq repair 0)
(setq health 999)

(defun use-medkit () 
    (setq repair 1))

(defun get-hurt ()
    (loop
        (progn 
            (setq a (random 11))
            (setq b (random 11))
            (progn  
                (princ ".")
                (sleep 1)))

        (if (eq a b) (progn     
                        (setq hurt 1)
                        (when (eq hurt 1) (progn        
                                            (format t "~%You are hurt!~%You will lose 1 hp every 10 seconds~%~%Type use-medkit to stop the bleeding~%")
                                            (loop
                                                (progn 
                                                    (- 1 health)
                                                    (sleep 10))
                                                    ;(format t "health: ~A~%" health)
                                                (when (eq repair 1) (progn 
                                                                 (return "You stopped the bleeding") (setq hurt 0) (setq repair 0))))))))))

推荐答案

因此,程序无法一次执行两项操作.特别是,如果您正在忙于打印点,休眠并从999中减去1,那么您就不会停下来看看是否还有另一个命令.

So a program can’t do two things at once. In particular if you’re busy printing dots, sleeping and subtracting 1 from 999 then you won’t pause to see if there’s another command coming.

不幸的是,解决这个问题很困难.终端中最好的解决方案可能会使用ncurses之类的东西.此外,没有控制输入缓冲的标准方法.取而代之的是,这是一种可以进行一些并发和提示的简单方法.您可能想使用适当的异步库.

Unfortunately solving this problem is hard. The nicest solution in a terminal would probably use something like ncurses. Additionally there is no standard way to control input buffering. In lieu of that, here is a simple way you can do a bit of concurrency and some prompts. You might want to use a proper async library instead.

(defun maybe-read (input-stream recording-stream)
  (when (listen input-stream)
    (let ((char (read-char input-stream)))
      (if (char= char #\Newline)
         t
         (progn (write-char char recording-stream) (maybe-read))))))

(defun make-partial-reader (input-stream)
  (list input-stream (make-string-output-stream)))
(defun partial-read (reader)
  (when (apply #'maybe-read reader)
     (get-output-stream-string (second reader))))

(defun how-long-until (time)
  (let ((gap
          (/ (- time (get-internal-run-time)) internal-time-units-per-second)))
    (cond ((< gap 0) (values 0 :late))
          ((<= gap 0.001) (values 0 :now))
          (T (values (- gap 0.001) :soon)))))
(defun sleep-until (time)
  (multiple-value-bind (span type)
        (how-long-until time)
    (when (> span 60) (warn "long wait!")
    (case type
      (:late nil)
      (:now t)
      (:soon
        (sleep span)
        (unless (sleep-until time) (warn "poor timekeeping"))
        t))))
(defmacro with-prompt-and-scheduler ((schedule) (line &optional (input *standard-input*)) &body handle-line-input)
  (let ((reader (gensym)) (insched (gensym)))
    `(let ((,reader (make-partial-reader ,input) (,insched)))
        (flet ((,schedule (in fun &aux (at (+ (get-internal-run-time) (* in internal-time-units-per-second))))
                 (if (null ,insched) (push (cons at fun) schedule)
                    (loop for s on ,insched
                          for ((at2) . y) = s
                      if (< at at2)
                       do (psetf (car s) (cons at fun)
                                 (cdr s) (cons (car s) (cdr s)))
                          (finish-loop)
                      unless y do (setf (cdr s) (acons at fun nil)) (finish-loop)))))
         (loop
           (if ,insched
               (let ((,insched (pop ,insched)))
                 (when (sleep-until (car ,insched))
                   (let ((,line (partial-read ,reader)))
                     (when ,line ,@handle-line-input)))
                 (funcall (cdr ,insched)))
               (let ((,line (concatenate 'string (get-output-stream-string (second ,reader)) (read-line (first ,reader)))))
                 ,@handle-line))))))))

然后您可以像使用它一样

And then you could use it like:

(let ((count 0))
  (with-prompt-and-scheduler (schedule) (line)
    (let ((n (read-from-string line)))
      (when (realp n)
        (schedule n (let ((x (incf count))) (lambda () (format t "Ding ~a ~a~%" x count) (finish-output))))))))

在运行输入10之后,然后在下一行5.如果快速执行此操作,则会得到:

And after running that input 10, then on the next line 5. If you do that quickly you’ll get:

Ding 2 2
Ding 1 2

第一行显示在5秒后,第二行显示在10秒后.如果速度慢,您将得到:

With the first line appearing after 5 seconds and the second after 10. If you are slow you should get:

Ding 1 1
Ding 2 2

第一行输入10后10秒,第二行输入5后5秒.

With the first line coming 10 seconds after you enter 10 and the second line coming 5 seconds after you enter 5.

希望这能使您了解如何使一个程序似乎一次完成两件事.

Hopefully this can give you an idea of how to make a program seem to do two things at once.

这篇关于在循环中调用函数(Common Lisp)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆