Skip to content

Instantly share code, notes, and snippets.

@kindlychung kindlychung/1.clj Secret
Created Apr 2, 2015

Embed
What would you like to do?
(ns kaiyin.wumpus.game
(:require [kaiyin.wumpus.graphing :refer [dot-label]]))
(def city-nodes (atom nil))
(def city-edges (atom nil))
(def player-pos (ref nil))
(def visited-nodes (ref #{}))
(def player-status (atom :in-progress))
(def ^:dynamic *node-num* 10)
(def ^:dynamic *edge-num* 10)
(def ^:dynamic *worm-num* 3)
(def ^:dynamic *cop-odds* 2)
(defn filterb
[pred coll]
(let [rt (filter pred coll)]
(if (empty? rt)
nil
rt)))
(defn all-nodes []
(range 1 (inc *node-num*)))
(defn rand-node []
(inc (rand-int *node-num*)))
(defn edge-pair [x y]
(when-not (= x y)
[[x y] [y x]]))
(defn sample-2diff-nodes []
(let [[x y] [(rand-node) (rand-node)]]
(if (= x y)
(sample-2diff-nodes)
[x y])))
(defn edge-pair-rand []
(let [[x y] (sample-2diff-nodes)
edge-pair1 (fn [[x y]]
(if-not (= x y)
[[x y] [y x]]))]
(edge-pair1 [x y])))
(defn make-edge-vec []
(->> (repeatedly #(edge-pair-rand))
(take *edge-num*)
(apply concat)
set ;; ensure there are no duplicates
vec))
(defn get-connected
[edge-list]
(set (mapcat identity edge-list))) ;; return a set of nodes, known to be connected to at least one other node
(defn find-islands
[node-list edge-list]
(filter (complement (get-connected edge-list))
node-list))
(defn connect-all-islands [edge-vec]
(let [connected-nodes (vec (get-connected edge-vec))]
(concat
(mapcat (fn [island]
(edge-pair
island
(nth connected-nodes (rand-int (count connected-nodes)))))
(find-islands (all-nodes) edge-vec))
edge-vec)))
;(defn edges-map [edges-vec edges-with-cops]
; (let [cops-set (set edges-with-cops)
; adorn-edge (fn [edge]
; (if (cops-set (sort edge))
; [[(second edge) :COPS]]
; [[(second edge)]]))]
; (apply merge-with (fn [& args] (vec)))))
(let* [vs (make-edge-vec)
vs-cops (filter #((= (rand-int *cop-odds*) 0)) vs)]
(prn vs)
(prn (filter (complement #{1 2}) [1 2 3 4]))
(prn (get-connected vs))
(prn (filter (complement (get-connected vs)) (all-nodes)))
(prn (find-islands (all-nodes) vs))
(prn vs-cops)
)
(user=> CompilerException clojure.lang.ArityException: Wrong number of args (1) passed to: game/eval1083/fn--1084, compiling:(/Users/kaiyin/workspace/land-of-clojure/src/kaiyin/wumpus/game.clj:74:18)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.