Comment detail

ポーカーの役判定 (Nested Flatten)
簡潔になるようにしたつもりが、混沌としたものになってしまいました…。
実行結果:
(format t "~A => ~A~%" x (hand x "SQSJSASKST"))
;==>
;SQSJSASKST => Royal straight flash 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
(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")))))))))

すいません、修正前のをコピペしてました。実行例も不要な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

Index

Feed

Other

Link

Pathtraq

loading...