Skip to content

Instantly share code, notes, and snippets.

@kindlychung
Created April 1, 2015 22:18
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 kindlychung/f33f6f73f5b08e0e5b90 to your computer and use it in GitHub Desktop.
Save kindlychung/f33f6f73f5b08e0e5b90 to your computer and use it in GitHub Desktop.
(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* 8)
(def ^:dynamic *edge-num* 5)
(def ^:dynamic *worm-num* 3)
(def ^:dynamic *cop-odds* 4)
(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)))
(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)))))
(let [vs (make-edge-vec)]
(prn vs)
(prn (find-islands (all-nodes) vs)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment