/
chess.clj
54 lines (42 loc) · 1.25 KB
/
chess.clj
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
(ns joy.chess
"Chessboard and encapsulation example from chapter 1.")
(defn initial-board []
[\r \n \b \q \k \b \n \r
\p \p \p \p \p \p \p \p
\- \- \- \- \- \- \- \-
\- \- \- \- \- \- \- \-
\- \- \- \- \- \- \- \-
\- \- \- \- \- \- \- \-
\P \P \P \P \P \P \P \P
\R \N \B \Q \K \B \N \R])
(def *file-key* \a)
(def *rank-key* \0)
(defn- file-component [file]
(- (int file) (int *file-key*)))
(defn- rank-component [rank]
(* 8 (- 8 (- (int rank) (int *rank-key*)))))
(defn- index [file rank]
(+ (file-component file) (rank-component rank)))
(defn lookup [board pos]
(let [[file rank] pos]
(board (index file rank))))
(defn lookup2 [board pos]
(let [[file rank] (map int pos)
[fc rc] (map int [\a \0])
f (- file fc)
r (* 8 (- 8 (- rank rc)))
index (+ f r)]
(board index)))
;; fluent move example from section 9.4.2
(defrecord Move [from to castle? promotion]
Object
(toString [this]
(str "Move " (:from this)
" to " (:to this)
(if (:castle? this) " castle"
(if-let [p (:promotion this)]
(str " promote to " p)
"")))))
(defn build-move [& {:keys [from to castle? promotion]}]
{:pre [from to]}
(Move. from to castle? promotion))