I am learning clojure this week end and just starting to develop a simple checkers engine. I currently coded some basic functions to create the board and display it. I would love to get some feedback !
(ns sandbox.checkers)
(def standard-size 10)
(defn empty-board [size]
(vec (replicate size (vec (replicate size :empty)))))
(defn str-piece [piece]
(cond
(= piece :empty) " "
(= piece :white_man) "O"
(= piece :black_man) "0"))
(defn str-board [board]
(str "\n"
(clojure.string/join
"\n"
(map
#(clojure.string/join " | " (map str-piece %))
board))))
(defn print-board [board]
(println (str-board board)))
(defn fill-white [size]
(apply comp
(map
(fn [pos]
(fn [board] (assoc-in board pos :white_man)))
(for [x (range size)
y (range (inc (/ size 2)) size)
:when (or (and (even? x) (even? y))
(and (odd? x) (odd? y)))]
[y x]))))
(defn fill-black [size]
(apply comp
(map
(fn [pos]
(fn [board] (assoc-in board pos :black_man)))
(for [x (range size)
y (range 0 (dec (/ size 2)))
:when (or (and (even? x) (even? y))
(and (odd? x) (odd? y)))]
[y x]))))
(defn starting-board [size]
((fill-black size) ((fill-white size) (empty-board size))))
(print-board (starting-board standard-size))