Skip to content

Instantly share code, notes, and snippets.

@skuro
Created July 17, 2019 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save skuro/71a52a14a44b1b9036f4c18ebf295db8 to your computer and use it in GitHub Desktop.
Save skuro/71a52a14a44b1b9036f4c18ebf295db8 to your computer and use it in GitHub Desktop.
Clojure meetup #115

Fork this gist and put your code in there!

@jeroenvandijk
Copy link

Chess by Rohen and Jeroen

(ns chess-dojo.core)



;; Copied from https://github.com/ibdknox/colorize

(def ansi-colors {:reset "[0m"
                  :default "[39m"
                  :white   "[37m"
                  :black   "[30m"
                  :red     "[31m"
                  :green   "[32m"
                  :blue    "[34m"
                  :yellow  "[33m"
                  :magenta "[35m"
                  :cyan    "[36m"
                  :black-bg "[40m"
                  :red-bg "[41m"
                  :green-bg "[42m"
                  :yellow-bg "[43m"
                  :blue-bg "[44m"
                  :magenta-bg "[45m"
                  :cyan-bg "[46m"
                  :white-bg "[47m"
                  :bold "[1m"
                  :italic "[3m"
                  :underline "[4m"
                  :inverse "[7m"
                  :strikethrough "[9m"})

(defn ansi
  "Get the ansi code for a specific color"
  [code]
  (str \u001b (get ansi-colors code (:reset ansi-colors))))

(defn color 
  "Wrap the given strings in the provided color. Color should be
  a keyword and can be any of the following:
  
  [:reset :default :white :black :red :green :blue :yellow :magenta :cyan
   :black-bg :red-bg :green-bg :yellow-bg :blue-bg :magenta-bg :cyan-bg :white-bg
   :underline :italic :bold :strikethrough :inverse].
  Each of these also has a function created for it: (cyan \"woohoo\")"
  [code & s]
  (str (ansi code) (apply str s) (ansi :reset)))

(defn show-all []
  (let [all-colors (apply juxt (for [cur (keys ansi-colors)]
                                 (fn [input]
                                   [cur (color cur input)])))]
    (pprint (into (sorted-map) (all-colors "test")))))

(defmacro create-colors []
  (apply list 'do 
         (for [k (keys ansi-colors)]
           (let [code (ansi k)
                 reset (ansi :reset)]
             `(defn ~(symbol (name k)) [& s#] 
                (str ~code (apply str s#) ~reset))))))

(create-colors)


;; / Copied from https://github.com/ibdknox/colorize












(defn valid-move [board command]

  )

(defn handle-instruction [state input]
  (let )
  (cond
    (:entering-names? state)
    (let [names (clojure.string/split input #", ")]
      (assoc state :names names))
    
    (nil? (:names state))
    (do (println "Please type two names (seperated by comma)")
        (assoc state :entering-names? state))

    

    
    :else
    (do
      (println "Received instruction:" input)
      state)))

(defn enter-names [state name-a name-b]
  (assoc state )
  
  )

(def base-line-pieces [:R :N :B :Q :K :B :N :R])
(def pawn-line-pieces (repeat 8 :P))


(def empty-line (repeat 8 nil))


(defn initialize-board []
  (mapv vec [(map vector base-line-pieces (repeat :W))
             (map vector pawn-line-pieces (repeat :W))
             empty-line
             empty-line
             empty-line
             empty-line
             (map vector pawn-line-pieces (repeat :B))
             (map vector base-line-pieces (repeat :B))
             ]))

(defn initialize-state []
  {:board (initialize-board)})

(defn print-red [x]
  (print (color :red x)))


(defn print-board [board]
  (doseq [row board]
    (doseq [col row]
      (let [[piece color] col] 
        (if piece
          (if (= color :B)
            (do
              (print-red (name piece))
              (print "  "))
            (print (name piece) " "))
          (print "   "))))
    (println)))

(defn print-state [{:keys [board] :as state}]

  (print-board board)
  #_state)

(print-board (initialize-board))

(defn enter-names [s]
  s)

(defn pick-color [s]
  s)

(defn parse-coord [coord]
  (let [[x-str y-str] (clojure.string/split (name coord) #"")
        x (or (get (zipmap ["A" "B" "C" "D" "E" "F" "G" "H"] (range)) x-str)
              (throw (ex-info "X out of range" {})))
        y (let [v (- 8 (Long/parseLong y-str))]
            (if (<= 0 v 7)
              v
              (throw (ex-info "Y out of range" {}))))]
    [y x]))

(parse-coord :A7)
(parse-coord :B5)

(defn move-piece [{:keys [board] :as state} start end]
  (let [start-coord (parse-coord start)
        end-coord (parse-coord end)]
    (assoc state :board
           (-> board
               (assoc-in end-coord (get-in board start-coord))
               (assoc-in start-coord nil)))))

#_
(defn check-board [board color]
  (reduce-kv (fn []) [board])

  )

(->
 (initialize-state)
 (enter-names "Rohen" "Jeroen")
 (pick-color)
 (move-piece :A7 :A8)
 (move-piece :A7 :A8)
 (move-piece :A7 :A8)
 (move-piece :A7 :A8)
 
 

 )





(defn repl []
  (loop [state {}]
    (let [input (read-line)]
      (if (= input "q")
        (println "Exit..")
        (recur (handle-instruction state input))))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment