lisp中getword输入默认_常见的Lisp,如何屏蔽键盘输入

This is a console program in Common Lisp for a Hangman type game. The first player enters a string to be guessed by the second player. My input function is below --- unfortunately the characters typed by the first player remain visible.

With JavaScript it's simple, just use a password text entry box. With VB it's simple using the same sort of facility. Is there any way to do this using a native Common Lisp function?

Thanks, 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))

解决方案

Each implementation supports this differently.

You might want to use an auxiliary library like iolib.termios or cl-charms (interface to libcurses) if you want a portability layer above different implementations.

SBCL

I found a discussion thread about it for SBCL, and here is the code for that implementation, from 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)))

And so, here is finally an opportunity to talk about PROG2:

(defun read-silently ()

(prog2

(echo-off)

(read-line sb-sys:*tty*)

(echo-on)))

However, you might want to ensure that the echo is always reset when unwinding the stack, and clear the input before inputting things:

(defun read-silently ()

(echo-off)

(unwind-protect

(progn

(clear-input sb-sys:*tty*)

(read-line sb-sys:*tty*))

(echo-on)))

CL-CHARMS

Here is an alternative using libcurse. The following is sufficient to make a simple test work.

(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))

Besides, using libcurse might help you implement a nice-looking hangman console game.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值