2015-02-01 4 views
1

포커 게임을 시뮬레이트하는 LISP 프로그램을 만들고 있습니다.목록의 요소가 위 또는 아래에 있는지 확인하십시오.

현재이 I로 지정된 '핸드'를 순서 :

(defconstant order #(2 3 4 5 6 7 8 9 10 J Q K A)) 

(defun sort< (el1 el2) 
    (< (position el1 order) 
    (position el2 order))) 

I는 그것이 무엇 손 계산 시작 기능의 '핸드'를 전달한다.

(poker '((3 H)(2 H)(J H)(8 H)(5 H))) 

(3 수이고 H는 소송입니다 : 손이 형식으로 제공됩니다

(defun poker (hand) 
    (defparameter sortedhand (sort hand 'sort< :key 'first)) 
    (if (and (equal (second(first sortedhand))(second(second sortedhand))) 
      (equal (second(first sortedhand))(second(third sortedhand))) 
      (equal (second(first sortedhand))(second(fourth sortedhand))) 
      (equal (second(first sortedhand))(second(fifth sortedhand)))) 
     (print 't) 
     (print 'f)) 
     (print sortedhand)) 

(그들은 같은 무늬의 경우 여기에 내가 확인하고) 마음).

스트레이트 같은 카드를 계산하려면 카드가 연속 값인지 확인해야합니다.

(poker '(8 H)(Q H)(9 H)(10 H)(J H)) 

예를 들어 스트레이트 플러시입니다.

제대로 처리 할 수있는 방법이 있습니까? 이것을 달성하기 위해 상수 주문을 사용할 수있는 방법이 있습니까?

+5

DEFPARAMETER는 전역 변수 용입니다. DEFUN 내부에서 사용하지 마십시오. LET을 사용하십시오. –

답변

2

"직선"및 "플러시"조건부로 구분합니다.

(defconstant +card-values+ 
    #(2 3 4 5 6 7 8 9 10 J Q K A)) 

(defun card-value (card) 
    (position (first card) +card-values+)) 

(defun card-diff (card0 card1) 
    (- (card-value card0) (card-value card1))) 

(defun card< (&rest cards) 
    (apply #'< (mapcar #'card-value cards))) 

(defun suite= (&rest cards) 
    (if (endp (rest cards)) 
     t 
     (let ((card-suites (mapcar #'second cards))) 
     (every #'eql card-suites (rest card-suites))))) 

(테스트되지 않은) :

(defun straightp (hand) 
    (let ((sorted-hand (mapcar #'first (sort (copy-tree hand) #'card<)))) 
    (every (lambda (card0 card1) 
      (= 1 (card-diff card1 card0))) 
      sorted-hand 
      (rest sorted-hand)))) 

(defun flushp (hand) 
    (apply #'suite= hand)) 

(defun straight-flush-p (hand) 
    (and (straightp hand) 
     (flushp hand))) 

Card<, card-diffsuite= 오히려 명백한 의미를 가지고있다.

관련 문제