(defn initial-board []
"初始化棋盘,棋盘坐标
8
7
6
5
4
3
2
1
a b c d e f g h
"
[\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])
=> #'user/initial-board
#_ 列的索引
(def ^:dynamic *file-key* \a)
=> #'user/*file-key*
#_ 行的索引
(def ^:dynamic *rank-key* \0)
=> #'user/*rank-key*
(defn- rank-component [rank]
"计算垂直坐标
(int *rank-key*) 即 (int \0) => 48
\1 代表第一行,(int \1) => 49,即 49 - 48 = 1
8 - 1 = 7 即取第 “1” 行
7 * 8 = 56 取 board vertor 的 56 位置的元素
"
(->> (int *rank-key*)
(- (int rank))
(- 8)
(* 8)))
=> #'user/rank-component
(defn- file-component [file]
"计算水平坐标
计算列,列以a、b、c、d...索引。返回第几列:以字符传入, (int *file-key*) 即 (int \a) => 97
"
(- (int file) (int *file-key*)))
=> #'user/file-component
(defn- index [file rank]
"将ID布局映射为逻辑2D的棋盘"
(+ (file-component file) (rank-component rank)))
=> #'user/index
(defn lookup [board pos]
(let [[file rank] pos]
(board (index file rank))))
=> #'user/lookup
(lookup (initial-board) "a1")
=> \R
05-06
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交