aboutsummaryrefslogtreecommitdiff
path: root/blackjack.lisp
blob: 3d0318db517d108422891833e38a748701f36f64 (about) (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
(defconst *aces* '(A))
(defconst *faces* '(J Q K))

(defun acep (x)
  (declare (xargs :guard t))
  (and (member x *aces*) t))

(defun facep (x)
  (declare (xargs :guard t))
  (and (member x *faces*) t))

(defun rankp (x)
  (declare (xargs :guard t))
  (or (acep x)
      (facep x)
      (and (natp x)
           (<= 2 x)
           (<= x 10))))

(defun scorep (x)
  (declare (xargs :guard t))
  (case x
    ((BUST NATURAL-BLACKJACK) t)
    (t (and (natp x)
            (<= x 21)))))

(defun rank-list-p (x)
  (declare (xargs :guard t))
  (cond ((consp x)
         (and (rankp (car x))
              (rank-list-p (cdr x))))
        (t (null x))))

(defun filter-natp (x)
  (declare (xargs :guard (true-listp x)))
  (cond ((endp x) nil)
        ((natp (car x))
         (cons (car x) (filter-natp (cdr x))))
        (t (filter-natp (cdr x)))))

(defun nat-list-p (x)
  (declare (xargs :guard t))
  (cond ((consp x)
         (and (natp (car x))
              (nat-list-p (cdr x))))
        (t (null x))))

(defun sum (x)
  (declare (xargs :guard (nat-list-p x)))
  (cond ((endp x) 0)
        (t (+ (car x) (sum (cdr x))))))

(defun filter-facep (x)
  (declare (xargs :guard (true-listp x)))
  (cond ((endp x) nil)
        ((facep (car x))
         (cons (car x) (filter-facep (cdr x))))
        (t (filter-facep (cdr x)))))

(defun face-list-p (x)
  (declare (xargs :guard t))
  (cond ((consp x)
         (and (facep (car x))
              (face-list-p (cdr x))))
        (t (null x))))

(defun face-list-to-score (x)
  (declare (xargs :guard (face-list-p x)))
  (* 10 (length x)))

(defun score-without-aces (rl)
  (declare (xargs :guard (rank-list-p rl)))
  (+ (sum (filter-natp rl))
     (face-list-to-score (filter-facep rl))))

(defthm natp/score-without-aces
  (implies (rank-list-p x)
           (natp (score-without-aces x))))

(defun ace-count (x)
  (declare (xargs :guard (true-listp x)))
  (cond ((endp x) 0)
        ((acep (car x)) (+ 1 (ace-count (cdr x))))
        (t (ace-count (cdr x)))))

(defthm natp/ace-count
  (implies (rank-list-p x)
           (natp (ace-count x))))

(defun add-ace-scores (s ac)
  (declare (xargs :guard (and (natp s)
                              (natp ac))))
  (cond
   ((zerop ac) s)
   ((<= (+ s 11) 21) (+ s 11 (- ac 1)))
   (t (+ s 1 (- ac 1)))))

(defthm natp/add-ace-scores
  (implies (and (natp x) (natp y))
           (natp (add-ace-scores x y))))

(defun score* (rl)
  (declare (xargs :guard (rank-list-p rl)))
  (add-ace-scores (score-without-aces rl)
                  (ace-count rl)))

(defthm natp/score*-1
  (implies (rank-list-p rl)
           (natp (+ (sum (filter-natp rl))
                    (* 10 (len (filter-facep rl)))))))

(defthm natp/score*
  (implies (rank-list-p rl)
           (natp (score* rl))))

(defun score (rl)
  (declare (xargs :guard (rank-list-p rl)))
  (let ((s (score* rl)))
    (cond
     ((and (< 21 s)) 'BUST)
     ((and (= (len rl) 2) (= s 21))
      'NATURAL-BLACKJACK)
     (t s))))

(defthm scorep/score-1
  (implies (rank-list-p x)
           (natp (+ (sum (filter-natp x))
                    (* 10 (len (filter-facep x)))))))

(defthm scorep/score
  (implies (rank-list-p x)
           (scorep (score x)))
  :hints (("Goal" :hands-off (add-ace-scores))))

(defun judge/score (player dealer)
  (declare (xargs :guard (and (scorep player)
                              (scorep dealer))))
  (cond
   ((and (equal player 'NATURAL-BLACKJACK)
         (equal dealer 'NATURAL-BLACKJACK))
    'PUSH)
   ((equal player 'NATURAL-BLACKJACK) 'WIN)
   ((equal dealer 'NATURAL-BLACKJACK) 'LOSE)
   ((equal player 'BUST) 'LOSE)
   ((equal dealer 'BUST) 'WIN)
   ((< player dealer) 'LOSE)
   ((> player dealer) 'WIN)
   (t 'PUSH)))

(defun judge/ranks (player dealer)
  (declare (xargs :guard (and (rank-list-p player)
                              (rank-list-p dealer))
                  :guard-hints (("Goal"
                                 :hands-off (score)
                                 :use (scorep/score)))))
  (judge/score (score player)
               (score dealer)))

(defthm bust-and-lose
  (equal (judge/score 'BUST x) 'LOSE))

(defthm draw-a-face-and-blackjack
  (implies (facep r)
           (equal (score (list 'A r))
                  'NATURAL-BLACKJACK)))

(defthm bust-12
  (let ((rs '(K 2))
        (new 'J))
    (and (= (score* rs) 12)
         (equal (score (cons new rs)) 'BUST))))

(defthm safe-hit
  (implies (and (rankp r1)
                (rankp r2)
                (rankp new)
                (<= (score* (list r1 r2)) 11))
           (not (equal (score (list new r1 r2))
                       'BUST))))