-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow.lisp
143 lines (124 loc) · 5.06 KB
/
show.lisp
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
;; FILE: show.lisp
;; AUTHORS: Daniel Melody & Aidan Lavis
;; ==========================================
;; Contains the functions related to the SHOW segment of Cribbage
;; SHOW
;; ------------------------------------------
(defun show (c)
(let ((plr (cribbage-whose-turn? c)))
(format t "one for last: player ~A gets a point~%" (1+ plr))
(incf (svref (cribbage-score c) plr)))
(let ((cut (cribbage-cut c))
(hands (cribbage-backup-hands c)))
(labels ((hand-score (plr)
(let ((hand (svref hands plr)))
(format t "~%scoring player ~A hand: ~A~%"
(1+ plr) (mapcar #'card->string hand))
(incf (svref (cribbage-score c) plr)
(hand-value cut hand)))))
(hand-score *player-one*)
(hand-score *player-two*)
(format t "~%scoring crib Dealer= ~A~%" (1+ (cribbage-whose-dealer? c)))
(incf (svref (cribbage-score c) (cribbage-whose-dealer? c))
(crib-value cut (cribbage-crib c)))
(format t "~%")
(print-cribbage c t 1)
;; change dealer for next ROUND
(toggle-dealer! c)
)))
;; -------------------------------------------------------------------------- ;;
;; ------------------------- card-scoring functions ------------------------- ;;
;; -------------------------------------------------------------------------- ;;
(defun make-hand (c1 c2 c3)
(list c1 c2 c3))
(defun make-crib (c1 c2 c3 c4)
(list c1 c2 c3 c4))
(defun print-cards (cards)
(print-card (first cards))
(print-cards (rest cards)))
(defun hand-value (cut hand)
(let ((cards (cons cut hand)))
(+ (fifteen-score cards)
(tuple-score cards)
(run-score cards)
(flush-score-hand cut hand))
))
(defun crib-value (cut crib)
(cards-value (cons cut crib)))
(defun cards-value (cards)
(+ (fifteen-score cards)
(tuple-score cards)
(run-score cards)
(flush-score cards))
)
(defun power-set (listy)
(if (null listy)
'(())
(let ((rest-pset (power-set (rest listy))))
(append (mapcar (lambda (subby)
(cons (first listy) subby))
rest-pset)
rest-pset))))
(defun fifteen-score (cards)
(labels ((info (listy)
(format t " 2 points for fifteen: ~A~%" listy))
(count-fifteens (pset)
(cond ((null pset) 0)
((= (apply #'+ (first pset)) 15)
(info (first pset))
(+ 1 (count-fifteens (rest pset))))
(t (count-fifteens (rest pset))))))
(* 2 (count-fifteens (power-set (mapcar #'card-value cards))))))
(defun tuple-score (cards)
(let ((score-vec #('error 0 2 6 12)))
(labels ((info (s c) (when (> s 0)
(format t " ~A points for ~Aple~%" s c)))
(count-tuples
(ranks last-rank counter)
(let ((score (svref score-vec counter)))
(cond ((null ranks) (info score counter) score)
((= (first ranks) last-rank)
(count-tuples (rest ranks) (first ranks) (+ 1 counter)))
(t
(info score counter)
(+ score
(count-tuples (rest ranks) (first ranks) 1)))
))))
(count-tuples (sort (mapcar #'rank-of cards) #'<) -2 1))))
(defun run-score (cards)
(labels ((info (s rc) (when (> s 0)
(format t " ~A points for ~A run~%" s rc)))
(count-runs
(ranks last-rank run-count multi)
(let ((score (* (apply #'* multi)
(if (>= run-count 3) run-count 0))))
(cond ((null ranks) (info score run-count) score)
((= (first ranks) last-rank)
(count-runs (rest ranks) (first ranks)
run-count (cons (1+ (first multi))
(rest multi))))
((= (first ranks) (+ 1 last-rank))
(count-runs (rest ranks) (first ranks)
(+ run-count 1) (cons 1 multi)))
(t
(info score run-count)
(+ score
(count-runs (rest ranks) (first ranks) 1 '(1))))
))))
(count-runs (sort (mapcar #'rank-of cards) #'<) -2 1 '(1))))
(defun print-flush (result)
(when (> result 0)
(format t " ~A points for flush~%" result))
result)
(defun flush-score-hand (cut hand)
(print-flush (let ((suits (mapcar #'suit-of hand)))
(if (apply #'= suits)
(+ (length hand)
(if (= (suit-of cut) (first suits))
1
0))
0))))
(defun flush-score (cards)
(print-flush (if (apply #'= (mapcar #'suit-of cards))
(length cards)
0)))