From 127d804eb231a6f0176ba6a541e33bdcd0248c18 Mon Sep 17 00:00:00 2001 From: ueno Date: Thu, 20 May 2004 09:02:21 +0000 Subject: [PATCH] * riece-hangman.el (riece-hangman-player-context-alist): Renamed from riece-hangman-players. (riece-hangman-make-context): Add doc. (riece-hangman-context-word): Add doc. (riece-hangman-context-guessed): Add doc. (riece-hangman-context-missed-count): Add doc. (riece-hangman-context-set-guessed): Add doc. (riece-hangman-context-set-missed-count): Add doc. (riece-hangman-word): Add doc. (riece-hangman-reply-with-context): Prefix user name. --- lisp/ChangeLog | 13 +++++ lisp/riece-hangman.el | 149 ++++++++++++++++++++++++++----------------------- 2 files changed, 92 insertions(+), 70 deletions(-) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 1d176cd..8a82aed 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,5 +1,18 @@ 2004-05-20 Daiki Ueno + * riece-hangman.el (riece-hangman-player-context-alist): Renamed + from riece-hangman-players. + (riece-hangman-make-context): Add doc. + (riece-hangman-context-word): Add doc. + (riece-hangman-context-guessed): Add doc. + (riece-hangman-context-missed-count): Add doc. + (riece-hangman-context-set-guessed): Add doc. + (riece-hangman-context-set-missed-count): Add doc. + (riece-hangman-word): Add doc. + (riece-hangman-reply-with-context): Prefix user name. + +2004-05-20 Daiki Ueno + * riece-doctor.el (riece-doctor-after-privmsg-hook): Prefix user name to reply. diff --git a/lisp/riece-hangman.el b/lisp/riece-hangman.el index 8811e09..72e8658 100644 --- a/lisp/riece-hangman.el +++ b/lisp/riece-hangman.el @@ -53,31 +53,42 @@ :type 'file :group 'riece-hangman) -(defvar riece-hangman-players nil) +(defvar riece-hangman-player-context-alist nil) (defvar riece-hangman-words-buffer nil) (defun riece-hangman-make-context (word) + "Make an instance of player context object. +This function is for internal use only." (vector word nil 0)) (defun riece-hangman-context-word (context) + "Return the correct word of CONTEXT. +This function is for internal use only." (aref context 0)) (defun riece-hangman-context-guessed (context) + "Return the guessed letters in this CONTEXT. +This function is for internal use only." (aref context 1)) (defun riece-hangman-context-missed-count (context) + "Return the count of missed guesses in this CONTEXT. +This function is for internal use only." (aref context 2)) -(defun riece-hangman-context-set-word (context word) - (aset context 0 word)) - (defun riece-hangman-context-set-guessed (context guessed) + "Set the GUESSED letters in this CONTEXT. +This function is for internal use only." (aset context 1 guessed)) (defun riece-hangman-context-set-missed-count (context missed-count) + "Set the count of MISSED guesses in this CONTEXT. +This function is for internal use only." (aset context 2 missed-count)) (defun riece-hangman-word () + "Return random word. +The wordlist is read from `riece-hangman-words-file'." (unless riece-hangman-words-buffer (setq riece-hangman-words-buffer (generate-new-buffer " *riece-hangman*")) (save-excursion @@ -102,104 +113,102 @@ string 'notice t)) (riece-send-string (format "NOTICE %s :%s\r\n" target string))) -(defun riece-hangman-reply-with-context (target context) - (let* ((word (riece-hangman-context-word context)) - (masked-word (make-string (length word) ?-)) - (guessed (riece-hangman-context-guessed context)) - (index 0)) - (while (< index (length word)) - (if (memq (aref word index) guessed) - (aset masked-word index (aref word index))) +(defun riece-hangman-reply-with-context (user target context) + (let ((masked-word (make-string + (length (riece-hangman-context-word context)) + ?-)) + (guessed (copy-sequence (riece-hangman-context-guessed context))) + (index 0)) + (while (< index (length (riece-hangman-context-word context))) + (if (memq (aref (riece-hangman-context-word context) index) guessed) + (aset masked-word index + (aref (riece-hangman-context-word context) index))) (setq index (1+ index))) (riece-hangman-reply target - (format "Word: %s, Guessed: %s" - masked-word - (apply #'string (sort (copy-sequence guessed) #'<)))))) + (format "%s: Word: %s, Guessed: %s" + user masked-word + (if guessed + (apply #'string (sort guessed #'<)) + ""))))) (defun riece-hangman-after-privmsg-hook (prefix string) - (let* ((user (riece-make-identity (riece-prefix-nickname prefix) - riece-server-name)) + (let* ((user (riece-prefix-nickname prefix)) (parameters (riece-split-parameters string)) (targets (split-string (car parameters) ",")) (message (nth 1 parameters)) - pointer) + case-fold-search + pointer word guessed) (if (string-match riece-hangman-hello-regexp message) - (if (riece-identity-assoc user riece-hangman-players) + (if (riece-identity-assoc user riece-hangman-player-context-alist t) (riece-hangman-reply (car targets) - (format "%s: You are already playing the game." - (riece-format-identity user t))) + (format "%s: You are already playing the game." user)) (let ((context (riece-hangman-make-context (riece-hangman-word)))) - (setq riece-hangman-players (cons (cons user context) - riece-hangman-players)) - (riece-hangman-reply-with-context (car targets) context))) + (setq riece-hangman-player-context-alist + (cons (cons user context) + riece-hangman-player-context-alist)) + (riece-hangman-reply-with-context user (car targets) context))) (if (string-match riece-hangman-bye-regexp message) - (when (setq pointer (riece-identity-assoc user - riece-hangman-players)) - (setq riece-hangman-players (delq pointer riece-hangman-players)) + (when (setq pointer (riece-identity-assoc + user riece-hangman-player-context-alist t)) + (setq riece-hangman-player-context-alist + (delq pointer riece-hangman-player-context-alist)) (riece-hangman-reply (car targets) (format "%s: Sorry, the word was \"%s\"" - (riece-format-identity user t) + user (riece-hangman-context-word (cdr pointer))))) - (if (setq pointer (riece-identity-assoc user riece-hangman-players)) + (if (setq pointer (riece-identity-assoc + user riece-hangman-player-context-alist t)) (if (or (/= (length message) 1) (not (string-match "[a-z]" message))) (riece-hangman-reply (car targets) - (format "%s: Not a valid guess: %s" - (riece-format-identity user t) - message)) + (format "%s: Not a valid guess: %s" user message)) (if (memq (aref message 0) (riece-hangman-context-guessed (cdr pointer))) (riece-hangman-reply (car targets) (format "%s: Already guessed '%c'" - (riece-format-identity user t) - (aref message 0))) - (riece-hangman-context-set-guessed - (cdr pointer) - (cons (aref message 0) - (riece-hangman-context-guessed (cdr pointer)))) - (let ((word (riece-hangman-context-word (cdr pointer))) - (index 0) - (char (aref message 0))) - (unless (catch 'found - (while (< index (length word)) - (if (eq (aref word index) char) - (throw 'found t)) - (setq index (1+ index)))) - (riece-hangman-context-set-missed-count - (cdr pointer) - (1+ (riece-hangman-context-missed-count - (cdr pointer)))))) + user (aref message 0))) + (setq guessed (riece-hangman-context-set-guessed + (cdr pointer) + (cons (aref message 0) + (riece-hangman-context-guessed + (cdr pointer)))) + word (riece-hangman-context-word (cdr pointer))) + (unless (catch 'found + (setq index 0) + (while (< index (length word)) + (if (eq (aref word index) (aref message 0)) + (throw 'found t)) + (setq index (1+ index)))) + (riece-hangman-context-set-missed-count + (cdr pointer) + (1+ (riece-hangman-context-missed-count (cdr pointer))))) (if (>= (riece-hangman-context-missed-count (cdr pointer)) 7) (progn (riece-hangman-reply (car targets) (format "%s: Sorry, the word was \"%s\"" - (riece-format-identity user t) + user (riece-hangman-context-word (cdr pointer)))) - (setq riece-hangman-players + (setq riece-hangman-player-context-alist (delq pointer - riece-hangman-players))) - (let ((word (riece-hangman-context-word (cdr pointer))) - (guessed (riece-hangman-context-guessed (cdr pointer))) - (index 0) - (char (aref message 0))) - (if (catch 'missing - (while (< index (length word)) - (unless (memq (aref word index) guessed) - (throw 'missing t)) - (setq index (1+ index)))) - (riece-hangman-reply-with-context - (car targets) (cdr pointer)) - (riece-hangman-reply - (car targets) - (format "%s: You got it!" - (riece-format-identity user t))) - (setq riece-hangman-players - (delq pointer riece-hangman-players)))))))))))) + riece-hangman-player-context-alist))) + (if (catch 'missing + (setq index 0) + (while (< index (length word)) + (unless (memq (aref word index) guessed) + (throw 'missing t)) + (setq index (1+ index)))) + (riece-hangman-reply-with-context user (car targets) + (cdr pointer)) + (riece-hangman-reply (car targets) + (format "%s: You got it!" user)) + (setq riece-hangman-player-context-alist + (delq pointer + riece-hangman-player-context-alist))))))))))) (defun riece-hangman-insinuate () (add-hook 'riece-after-privmsg-hook 'riece-hangman-after-privmsg-hook)) -- 1.7.10.4