这是一个用Common Lisp编写的用于Hangman类型游戏的控制台程序。第一个玩家输入要由第二个玩家猜测的字符串。我的输入函数如下-不幸的是,第一个玩家输入的字符仍然可见。
使用JavaScript很简单,只需使用密码文本输入框。在VB中,使用同样的工具很简单。有没有办法使用原生的Common Lisp函数来做到这一点?
谢谢,CC。
(defun get-answer ()
(format t "Enter the word or phrase to be guessed: ~%")
(coerce (string-upcase (read-line)) 'list))
(defun start-hangman ()
(setf tries 6)
(greeting)
(setf answer (get-answer))
(setf obscure (get-obscure answer))
(game-loop answer obscure))发布于 2016-10-01 02:41:08
每个实现都以不同的方式支持这一点。
如果您希望在不同实现之上使用可移植性层,则可能需要使用iolib.termios或cl-charms (libcurses接口)之类的辅助库。
SBCL
我找到了一个关于SBCL的discussion thread,下面是来自Richard M.Kreuter的实现代码:
(require :sb-posix)
(defun echo-off ()
(let ((tm (sb-posix:tcgetattr sb-sys:*tty*)))
(setf (sb-posix:termios-lflag tm)
(logandc2 (sb-posix:termios-lflag tm) sb-posix:echo))
(sb-posix:tcsetattr sb-sys:*tty* sb-posix:tcsanow tm)))
(defun echo-on ()
(let ((tm (sb-posix:tcgetattr sb-sys:*tty*)))
(setf (sb-posix:termios-lflag tm)
(logior (sb-posix:termios-lflag tm) sb-posix:echo))
(sb-posix:tcsetattr sb-sys:*tty* sb-posix:tcsanow tm)))所以,现在终于有机会来谈谈PROG2了
(defun read-silently ()
(prog2
(echo-off)
(read-line sb-sys:*tty*)
(echo-on)))但是,您可能希望确保在展开堆栈时始终重置回显,并在输入内容之前清除输入:
(defun read-silently ()
(echo-off)
(unwind-protect
(progn
(clear-input sb-sys:*tty*)
(read-line sb-sys:*tty*))
(echo-on)))CL-CHARMS
这里有一个使用libcurse的替代方案。下面的代码足以完成一个简单的测试。
(defun read-silently ()
(let (input)
(charms:with-curses ()
(charms:disable-echoing)
(charms:enable-raw-input)
(clear-input *terminal-io*)
(setf input (read-line *terminal-io*))
(charms:disable-raw-input)
(charms:enable-echoing))
input))此外,使用libcurse可能会帮助您实现一个漂亮的hangman控制台游戏。
发布于 2016-10-01 02:11:39
您是否要打印到控制台?这是标准控制台的固有限制。
你需要打印一大堆换行符才能将文本从屏幕上移出。
许多游戏机不具备选择性擦除部分屏幕的功能。
https://stackoverflow.com/questions/39797560
复制相似问题