I'm rather new to Clojure and so I decided to program a Connect Four for fun and learning.
The code below is a Clojure implementation of this bitboard algorithm.
The whole code can be found here: https://gist.github.com/3639220
What I'm unsure about are the functions (insert) and (bit-check-board):
(defn insert "Inserts symbol for given player (either 1 or 2) at specified x and calls bit-insert for corresponding bitboard." [boards x player-num] (let [y (get-y (boards 0) x)] (if (= player-num 1) ; TODO: improve (vector (assoc-in (boards 0) [y x] (player player-num)) (bit-insert (boards 1) (- 5 y) x) (boards 2)) (vector (assoc-in (boards 0) [y x] (player player-num)) (boards 1) (bit-insert (boards 2) (- 5 y) x))))) (defn bit-check-board ; TODO: improve "Checks whether given bitboard is a win." [bitboard] (let [diag1 (bit-and bitboard (bit-shift-right bitboard 6)) hori (bit-and bitboard (bit-shift-right bitboard 7)) diag2 (bit-and bitboard (bit-shift-right bitboard 8)) vert (bit-and bitboard (bit-shift-right bitboard 1))] (bit-or (bit-and diag1 (bit-shift-right diag1 (* 2 6))) (bit-and hori (bit-shift-right hori (* 2 7))) (bit-and diag2 (bit-shift-right diag2 (* 2 8))) (bit-and vert (bit-shift-right vert (* 2 1))))))
- insert: Is there a better way to somehow make function calls depend on some test?
- bit-check-board: A lot of similar lines where just one number (and one variable name) is different.