(mod X (include *standard-cl-23*) (include truncate.clinc) (include partition.clinc) (include append.clinc) (defun find_hand_indices ((@ numbered-cards ((number . card) . rest))) (if numbered-cards (logior (lsh 1 number) (find_hand_indices rest)) 0 ) ) (defun number_cards (num cards) (if cards (c (c num (f cards)) (number_cards (+ num 1) (r cards))) () ) ) ;; Sorts the cards by group size according to hand. ;; Hand has pairs of count and rank. We pull out cards based on their rank ;; until each bucket is empty in hand and then give the remaining cards. (defun bucket_cards_by_frequency_groups (hand cards) (if hand (assign (hand_freq . want_rank) (f hand) (cards_with_rank . cards_without_rank) (partition (lambda ((& want_rank) (num . (rank . suit))) (= rank want_rank)) cards) ;; We have the cards with this rank... go to the next wanted rank. (append cards_with_rank (bucket_cards_by_frequency_groups (r hand) cards_without_rank)) ) cards ) ) (defun handcalc (cards sorted_ranks hand firstcount firstrank secondcount secondrank flush_suit) (if ;; Full house (logand (= firstcount 3) (= secondcount 2)) (find_hand_indices (truncate 5 (bucket_cards_by_frequency_groups hand (number_cards 0 cards)))) ;; Flush (if flush_suit (find_hand_indices (truncate 5 (bucket_cards_by_frequency_groups hand (number_cards 0 cards)))) ;; Else (find_hand_indices (truncate 5 (bucket_cards_by_frequency_groups hand (number_cards 0 cards)))) ) ) ) (handcalc (q (14 . 1) (12 . 1) (11 . 1) (10 . 1) (9 . 1)) (list 14 12 11 10 9) (q (14 . 1) (12 . 1) (11 . 1) (10 . 1) (9 . 1)) 1 14 1 12 1) )