Skip to content

Instantly share code, notes, and snippets.

@stianeikeland
Created December 2, 2014 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stianeikeland/884edfb11357c00aeed2 to your computer and use it in GitHub Desktop.
Save stianeikeland/884edfb11357c00aeed2 to your computer and use it in GitHub Desktop.
tic-tac-zipper
(ns ticzip.core
(:require [clojure.zip :as z]))
(def game {:board (vec (repeat 9 nil))
:player :x})
(def next-player {:x :o, :o :x})
(defn- free-tiles [{:keys [board]}]
(->> board
(keep-indexed (fn [idx itm] (when (nil? itm) idx)))
(seq)))
(defn- play-move [{:keys [board player]} move]
{:board (assoc board move player)
:player (next-player player)})
(defn- children [game]
(reduce #(assoc %1 %2 (play-move game %2))
{}
(free-tiles game)))
(defn- children [game]
(map (partial play-move game) (free-tiles game)))
(defn- make-node [game children]
(assoc game :children children))
(def ticzip (z/zipper free-tiles children make-node game))
(-> ticzip
z/down
z/right
z/down
z/down
z/down
z/children)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment