Comment detail
ポーカーの役判定 (Nested Flatten)すいません、修正前のをコピペしてました。実行例も不要なxが混じってました。正確には、 (format t "~A => ~A~%" (hand "SQSJSASKST")) です。
1 2 3 4 | 40c40
< (destructuring-bind (ignore flashp kinds 3-2p) hand
---
> (destructuring-bind (ignore flashp straightp kinds 3-2p) hand
|





mc
#5170()
[
Common Lisp
]
Rating0/0=0.00
実行結果:
(format t "~A => ~A~%" x (hand x "SQSJSASKST"))
;==>
;SQSJSASKST => Royal straight flash
(defpackage :doukaku-121 (:use :cl :ppcre)) (in-package :doukaku-121) (defun break-part (str &optional (ace-val 1) &aux suit rank) (do-matches-as-strings (match "([SDHC][A2-9TJQK])" str) (push (schar match 0) suit) (push (schar match 1) rank)) (values (sublis `((A . ,ace-val) (T . 10) (J . 11) (Q . 12) (K . 13)) (mapcar (lambda (x)(read-from-string (string x))) (nreverse rank))) suit)) (defun flashp (str) (let ((s (nth-value 1 (break-part str)))) (and (every (lambda (x) (char= (car s) x)) s) 'flash))) (defun kinds (str) (let ((k (length (delete-duplicates (break-part str))))) (if (= k 5) nil k))) (defun straightp (str) (flet ((check (str ace) (let ((lst (sort (break-part str ace) #'<))) (mapc (lambda (x y) (unless (= 1 (- y x)) (return-from check nil))) lst (cdr lst)) (values 'straight (if (= 1 ace) nil 'royal))))) (or (check str 1) (check str 14)))) (defun 3-2p (str) (do ((l (break-part str) (cdr l))) ((null (cddr l)) nil) (case (count (car l) l) (3 (return t)) (4 (return nil))))) (defun hand (str &optional (out t)) (multiple-value-bind (straightp royalp) (straightp str) (let ((hand (list royalp straightp (flashp str) (kinds str) (3-2p str)))) (destructuring-bind (ignore flashp kinds 3-2p) hand (declare (ignore ignore)) (if 3-2p (case kinds (2 (format out "Full house")) (3 (format out "Three of a kind"))) (case kinds (2 (format out "Four of a kind")) (3 (format out "Two pair")) (4 (format out "One paire")) (otherwise (if (or straightp flashp) (format out "~@(~{~@[~A ~]~}~)" hand) (format out "No pair")))))))))Rating0/0=0.00-0+
1 reply [ reply ]