-
Notifications
You must be signed in to change notification settings - Fork 12
/
originalSticks.CLP
313 lines (257 loc) · 7.79 KB
/
originalSticks.CLP
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
;;;======================================================
;;; Sticks Program
;;;
;;; This program was introduced in Chapter 9.
;;;
;;; CLIPS Version 6.0 Example
;;;
;;; To execute, merely load, reset and run.
;;;======================================================
; **************
; FACT TEMPLATES
; **************
; The phase fact indicates the current action to be
; undertaken before the game can begin
; (phase
; <action>) ; Either choose-player or
; select-pile-size
; The player-select fact contains the human's response to
; the "Who moves first?" question.
; (player-select
; <choice>) ; Valid responses are c (for computer)
; and h (for human).
; The pile-select fact contains the human's response to
; the "How many sticks in the pile?" question.
; (pile-select
; <choice>) ; Valid responses are integers
; greater than zero.
; The pile-size fact contains the current number
; of sticks in the stack.
; (pile-size
; <sticks>) ; An integer greater than zero.
; The player-move fact indicates whose turn it is.
; (player-move
; <player) ; Either c (for computer)
; or h (for human).
; The human-takes fact contains the human's response to
; the "How many sticks do you wish to take?" question.
; (human-takes
; <choice>) ; Valid responses are 1, 2, and 3.
; The take-sticks facts indicate how many sticks the
; computer should take based on the remainder when the
; stack size is divided by 4.
(deftemplate take-sticks
(slot how-many) ; Number of sticks to take.
(slot for-remainder)) ; Remainder when stack is
; divided by 4.
; ********
; DEFFACTS
; ********
(deffacts initial-phase
(phase choose-player))
(deffacts take-sticks-information
(take-sticks (how-many 1) (for-remainder 1))
(take-sticks (how-many 1) (for-remainder 2))
(take-sticks (how-many 2) (for-remainder 3))
(take-sticks (how-many 3) (for-remainder 0)))
; *****
; RULES
; *****
; RULE player-select
; IF
; The phase is to choose the first player
; THEN
; Ask who should move first, and
; Get the human's response
(defrule player-select
(phase choose-player)
=>
(printout t "Who moves first (Computer: c "
"Human: h)? ")
(assert (player-select (read))))
; RULE good-player-choice
; IF
; The phase is to choose the first player, and
; The human has given a valid response
; THEN
; Remove unneeded information, and
; Indicate whose turn it is, and
; Indicate that the pile size should be chosen
(defrule good-player-choice
?phase <- (phase choose-player)
?choice <- (player-select ?player&c | h)
=>
(retract ?phase ?choice)
(assert (player-move ?player))
(assert (phase select-pile-size)))
; RULE bad-player-choice
; IF
; The phase is to choose the first player, and
; The human has given a invalid response
; THEN
; Remove unneeded information, and
; Indicate that the first player should be chosen again,
; and Print the valid choices
(defrule bad-player-choice
?phase <- (phase choose-player)
?choice <- (player-select ?player&~c&~h)
=>
(retract ?phase ?choice)
(assert (phase choose-player))
(printout t "Choose c or h." crlf))
; RULE pile-select
; IF
; The phase is to choose the pile size
; THEN
; Ask what the pile size should be, and
; Get the human's response
(defrule pile-select
(phase select-pile-size)
=>
(printout t "How many sticks in the pile? ")
(assert (pile-select (read))))
; RULE good-pile-choice
; IF
; The phase is to choose the pile size, and
; The human has given a valid response
; THEN
; Remove unneeded information, and
; Store the pile size
(defrule good-pile-choice
?phase <- (phase select-pile-size)
?choice <- (pile-select ?size&:(integerp ?size)
&:(> ?size 0))
=>
(retract ?phase ?choice)
(assert (pile-size ?size)))
; RULE bad-pile-choice
; IF
; The phase is to choose the pile size, and
; The human has given a invalid response
; THEN
; Remove unneeded information, and
; Indicate that the pile size should be chosen again,
; and Print the valid choices
(defrule bad-pile-choice
?phase <- (phase select-pile-size)
?choice <- (pile-select ?size&~:(integerp ?size)
|:(<= ?size 0))
=>
(retract ?phase ?choice)
(assert (phase select-pile-size))
(printout t "Choose an integer greater than zero."
crlf))
; RULE computer-loses
; IF
; The pile size is 1, and
; It is the computer's move
; THEN
; Print that the computer has lost the game
(defrule computer-loses
(pile-size 1)
(player-move c)
=>
(printout t "Computer must take the last stick!" crlf)
(printout t "I lose!" crlf))
; RULE human-loses
; IF
; The pile size is 1, and
; It is the human's move
; THEN
; Print that the human has lost the game
(defrule human-loses
(pile-size 1)
(player-move h)
=>
(printout t "You must take the last stick!" crlf)
(printout t "You lose!" crlf))
; RULE get-human-move
; IF
; The pile size is greater than 1, and
; It is the human's move
; THEN
; Ask how many sticks to take, and
; Get the human's response
(defrule get-human-move
(pile-size ?size&:(> ?size 1))
(player-move h)
=>
(printout t "How many sticks do you wish to take? ")
(assert (human-takes =(read))))
; RULE good-human-move
; IF
; There is a pile of sticks, and
; The human has chosen how many sticks to take, and
; It is the human's move, and
; The human's choice is valid
; THEN
; Remove unneeded information, and
; Compute the new pile size, and
; Update the pile size, and
; Print the number of sticks left in the stack, and
; Trigger the computer player's turn
(defrule good-human-move
?pile <- (pile-size ?size)
?move <- (human-takes ?choice)
?whose-turn <- (player-move h)
(test (and (integerp ?choice)
(>= ?choice 1)
(<= ?choice 3)
(< ?choice ?size)))
=>
(retract ?pile ?move ?whose-turn)
(bind ?new-size (- ?size ?choice))
(assert (pile-size ?new-size))
(printout t ?new-size " stick(s) left in the pile."
crlf)
(assert (player-move c)))
; RULE bad-human-move
; IF
; There is a pile of sticks, and
; The human has chosen how many sticks to take, and
; It is the human's move, and
; The human's choice is invalid
; THEN
; Print the valid choices, and
; Remove unneeded information, and
; Retrigger the human player's move
(defrule bad-human-move
(pile-size ?size)
?move <- (human-takes ?choice)
?whose-turn <- (player-move h)
(test (or (not (integerp ?choice))
(< ?choice 1)
(> ?choice 3)
(>= ?choice ?size)))
=>
(printout t "Number of sticks must be between 1 and 3,"
crlf
"and you must be forced to take the last "
"stick." crlf)
(retract ?move ?whose-turn)
(assert (player-move h)))
; RULE computer-move
; IF
; It is the computers's move, and
; The pile size is greater than 1, and
; The computer's response is available
; THEN
; Remove unneeded information, and
; Compute the new pile size, and
; Print the number of sticks left in the stack, and
; Update the pile size, and
; Trigger the human players move
(defrule computer-move
?whose-turn <- (player-move c)
?pile <- (pile-size ?size&:(> ?size 1))
(take-sticks (how-many ?number)
(for-remainder =(mod ?size 4)))
=>
(retract ?whose-turn ?pile)
(bind ?new-size (- ?size ?number))
(printout t "Computer takes " ?number " stick(s)."
crlf)
(printout t ?new-size " stick(s) left in the pile."
crlf)
(assert (pile-size ?new-size))
(assert (player-move h)))