Skip to content

Instantly share code, notes, and snippets.

@pieter-van-prooijen
Forked from skuro/README.md
Last active July 17, 2019 19:15
Show Gist options
  • Save pieter-van-prooijen/358b846c66323fed581dc2b14354ef12 to your computer and use it in GitHub Desktop.
Save pieter-van-prooijen/358b846c66323fed581dc2b14354ef12 to your computer and use it in GitHub Desktop.
Clojure meetup #115
(ns chess.core
(:require [clojure.string :as string]))
(def board
{[:a 1] [:R :w]
[:b 1] [:N :w]
[:c 1] [:B :w]
[:d 1] [:Q :w]
[:e 1] [:K :w]
[:f 1] [:B :w]
[:g 1] [:N :w]
[:h 1] [:R :w]
[:a 2] [:P :w]
[:b 2] [:P :w]
[:c 2] [:P :w]
[:d 2] [:P :w]
[:e 2] [:P :w]
[:f 2] [:P :w]
[:g 2] [:P :w]
[:h 2] [:P :w]
[:a 7] [:P :b]
[:b 7] [:P :b]
[:c 7] [:P :b]
[:d 7] [:P :b]
[:e 7] [:P :b]
[:f 7] [:P :b]
[:g 7] [:P :b]
[:h 7] [:P :b]
[:a 8] [:R :b]
[:b 8] [:N :b]
[:c 8] [:B :b]
[:d 8] [:Q :b]
[:e 8] [:K :b]
[:f 8] [:B :b]
[:g 8] [:N :b]
[:h 8] [:R :b]})
(def files [:a :b :c :d :e :f :g :h])
(defn print-rank [board rank]
(->> (map vector files (repeat rank))
(map (fn [k] (board k [" " " "])))
(map (fn [[piece colour]] (str (name piece) (name colour))))
(string/join " ")))
(defn print-board [board]
(->> (range 8 0 -1)
(map (partial print-rank board))
(map println)))
(defn create-coord [file-ch rank-ch]
[(keyword (str file-ch)) (Integer/parseInt (str rank-ch))])
;; Piece + from + to
;; Xa1b2 -> [:a 1] [:b 2]
(defn move [board move-str]
(let [[piece file-from rank-from file-to file-from] move-str
from (create-coord file-from rank-from)
to (create-coord file-to rank-to)]
(-> board
(dissoc from)
(assoc to))))
(comment (print-rank board 6)
(print-board board)
(name " "))
Rb Nb Bb Qb Kb Bb Nb Rb
Pb Pb Pb Pb Pb Pb Pb Pb
Pw Pw Pw Pw Pw Pw Pw Pw
Rw Nw Bw Qw Kw Bw Nw Rw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment