Created
February 12, 2012 22:15
-
-
Save claj/1811127 to your computer and use it in GitHub Desktop.
baremud.clj
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 joy.baremud | |
(:require [clojure.set :as set])) | |
;;My apartment consists of | |
(def graph [#{:myroom :hallways} ;;my-room, connected to hallways | |
#{:hallways :bathroom} ;;the bathroom, connected to hallways | |
#{:otherroom :hallways :kitchen} ;;a loop with otherroom, kitchen, hallways all connected | |
#{:hallways :stairs} ;;a door out to the stairs | |
#{:stairs :elevator}]) ;;in the stairs there's an elevator | |
;;start in myroom | |
(def pos (atom :myroom)) | |
(defn neis | |
"sort out all the sets containing the sent in position, unite them to a set | |
neis is short for neighbor" | |
[w] (reduce set/union #{} (filter (fn [conn] (contains? conn w)) graph))) | |
(defn just-neis | |
"take out the room you are in and reuse neis about" | |
[w] (set/difference (neis w) #{w})) | |
(defn move | |
"check if you move to a neighbor, and moves | |
and prints out the new neighbors if that's the case" | |
[to] | |
(if (contains? (just-neis @pos) to) | |
(do | |
(reset! pos to) | |
(println "ok, you can go to:") | |
(println (just-neis @pos))) | |
(println "could not move"))) | |
(comment | |
@pos | |
:my-room | |
(move :hallways) | |
;;ok, you can go to: | |
#{:otherroom :myroom :stairs :bathroom :kitchen} | |
(move :kitchen) | |
;;ok, you can go to: | |
#{:otherroom :hallways}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment