This repository was archived by the owner on Jun 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbb-util.lisp
More file actions
255 lines (152 loc) · 5.53 KB
/
bb-util.lisp
File metadata and controls
255 lines (152 loc) · 5.53 KB
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
#|*****************************************************************
Description: Defines methods and functions to modify
and preform calculatoins on "bit boards" (64-bit bit-vector).
Notes: Need to decide if all of these functions/methods
should be keept as is or changed to a method/function
respectivly.
******************************************************************|#
#|--------------------------------------------
Modification
--------------------------------------------|#
(defmethod clear-bb ((bb bit-vector))
"Initilizes a 64-bit bit-vector to 0"
(loop for i from 0 to 63 do
(setf (bit bb i) 0)))
(defun blank-bb ()
"Returns a 64-bit bit-vector, of all 0"
(let ((temp (copy-seq #*0000000000000000000000000000000000000000000000000000000000000000)))
(clear-bb temp)
temp))
#|----------------------------------------------
Output
----------------------------------------------|#
(defun print-bb (bb)
"Print a 64-bit bit-vector as a board."
(terpri)
(let ((b (reverse bb)))
(loop for i from 0 to 7 do
(let ((x))
(loop for j from 0 to 7 do
(setf x (cons (aref b (+ j (* i 8))) x )))
(print x))))
(terpri))
(defun fill-bb (index)
"Returns a 64-bit bit-vector with the given indexes = 1, and the rest = 0"
(let ((temp (copy-seq (blank-bb))))
(loop
(if (and (listp index) (integerp (car index)))
(progn
(setf (bit temp (car index)) 1)
(setf index (cdr index)))
(progn
(if (not (eql (car index) nil))
(progn
(print 'FILL-BB__INVALID_INPUT)
(car index)))
(return temp))))))
#|---------------------------------------------------
;;;;; Calculation ;;;;;
Arguments: bit-vector
Returns: These methods return a 64-bit
bit-vector.
-----------------------------------------------------|#
(Defmethod up-from-s ((bb bit-vector))
"Accepts a bit-vector and returns a bit-vector with all elements 'above'
the inital true elements set to true."
(let ((Up-bb (blank-bb)))
(loop for i from 0 to 63 do
(if (eq (bit bb i) 1)
(progn
(let ((j (+ i 8)))
(loop while (< j 64) do
(setf (bit Up-BB j) 1)
(setf j (+ j 8)))))))
Up-BB))
(defmethod down-from-s ((bb bit-vector))
"Accepts a bit-vector and returns a bit-vector with all elements 'below'
the inital true elements set to true."
(let ((down-bb (blank-bb)))
(loop for i from 0 to 63 do
(let ((j (- i 8)))
(if (eq (bit bb i) 1)
(progn
(loop while (>= j 0) do
(setf (bit down-BB j) 1)
(setf j (- j 8)))))))
down-BB))
(Defmethod left-from-s ((bb bit-vector))
"Accepts a bit-vector and returns a bit-vector with all elements 'left'
of the inital true elements set to true."
(let ((left-bb (blank-bb)))
(loop for i from 0 to 63 do
(if (eq (bit bb i) 1)
(progn
(let ((k (mod i 8))(j (- i 1)))
(loop while (>= j (- i k)) do
(setf (bit left-BB j) 1)
(setf j (- j 1)))))))
left-BB))
(Defmethod right-from-s ((bb bit-vector))
"Accepts a bit-vector and returns a bit-vector with all elements 'right'
of the inital true elements set to true."
(let ((right-bb (blank-bb)))
(loop for i from 0 to 63 do
(if (eq (bit bb i) 1)
(progn
(let ((k (+ (mod i 8) 1)) (j (+ i 1)))
(loop while (<= j (+ i (- 8 k))) do
(setf (bit right-BB j) 1)
(setf j (+ j 1)))))))
right-BB))
(Defmethod upleft-diag-from-s ((bb bit-vector))
"Accepts a bit-vector and returns a bit-vector with all elements 'In the upper left diagonals'
of the inital true elements set to true."
(let ((uldiag-bb (blank-bb)))
(loop for i from 0 to 63 do
(if (eq (bit bb i) 1)
(progn
(let ((j (+ i 7)) (k (- (mod i 8) 1)))
(loop while (and (< j 64) (>= j 0) (>= k 0)) do
(setf (bit uldiag-BB j) 1)
(setf j (+ j 7))
(setf k (- k 1)))))))
uldiag-BB))
(Defmethod lowleft-diag-from-s ((bb bit-vector))
"Accepts a bit-vector and returns a bit-vector with all elements 'In the lower left diagonals'
;; of the inital true elements set to true."
(let ((lldiag-bb (blank-bb)))
(loop for i from 0 to 63 do
(if (eq (bit bb i) 1)
(progn
(let ((j (- i 9)) (k (- (mod i 8) 1)))
(loop while (and (< j 64) (>= j 0) (>= k 0)) do
(setf (bit lldiag-BB j) 1)
(setf j (- j 9))
(setf k (- k 1)))))))
lldiag-BB))
(Defmethod lowright-diag-from-s ((bb bit-vector))
"Accepts a bit-vector and returns a bit-vector with all elements 'In the lower right diagonals'
;; of the inital true elements set to true."
(let ((lrdiag-bb (blank-bb)))
(loop for i from 0 to 63 do
(if (eq (bit bb i) 1)
(progn
(let ((j (- i 7)) (k (+ (mod i 8) 1)))
(loop while (and (< j 64) (>= j 0) (< k 8)) do
(setf (bit lrdiag-BB j) 1)
(setf j (- j 7))
(setf k (+ k 1)))))))
lrdiag-BB))
(Defmethod upright-diag-from-s ((bb bit-vector))
"Accepts a bit-vector and returns a bit-vector with all elements 'In the lower right diagonals'
;; of the inital true elements set to true."
(let ((urdiag-bb (blank-bb)))
(loop for i from 0 to 63 do
(if (eq (bit bb i) 1)
(progn
(let ((j (+ i 9)) (k (+ (mod i 8) 1)))
(loop while (and (< j 64) (>= j 0) (< k 8)) do
(setf (bit urdiag-BB j) 1)
(setf j (+ j 9))
(setf k (+ k 1)))))))
urdiag-BB))