Created
December 2, 2014 14:04
-
-
Save stianeikeland/884edfb11357c00aeed2 to your computer and use it in GitHub Desktop.
tic-tac-zipper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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