Mercurial > dylan
view mtg/continuation.clj @ 11:1f112b4f9e8f tip
Fixed what was baroque.
author | Dylan Holmes <ocsenave@gmail.com> |
---|---|
date | Tue, 01 Nov 2011 02:30:49 -0500 |
parents | b4de894a1e2e |
children |
line wrap: on
line source
1 (ns mtg.continuation)2 ;; convention: cps function names end in &4 (def not-nil? (comp not nil?))5 (defn cpt "Continuation-passing transform. Makes f take a continuation as an additional argument."[f]6 (fn[& args]7 ((last args) (apply f (butlast args)))))11 ;; Card operations14 ;; Open predicates15 ;(defmacro literal-to-keyword[x] `(keyword (quote ~x)))16 (defrecord Oracle [])17 (def *oracle* (atom (Oracle.)))19 (defn ask* [m k](get @m k (cpt(constantly nil))))20 (defn tell!* [m k v] (swap! m assoc k v))22 (def ask(partial ask* *oracle*))23 ;(def ? (fn [k & args] (apply (ask k) args)))25 (def tell! (partial tell!* *oracle*))26 (defn extend[k f] ((ask k) f))28 ;; PRELIMINARY DEFINITIONS29 (defmacro defq "Defines a query." [name params & body]30 `(tell! (keyword ~name) (fn ~params ~@body)))31 (defmacro ? "Asks a query." [name & args]32 `((ask (keyword name)) ~@args))34 (defn true-preds "Returns a sequence of the preds for which (pred obj) returns true."35 [obj & preds]36 (map (comp (partial apply str) rest butlast str)37 (filter #(? % obj) preds)))39 (tell! :the-players #{})40 (tell! :player? (fn[obj & _] (not-nil? (? the-players obj))))48 (defq object? [obj & _] ;; 109.149 ((comp not empty?)(true-preds obj :card? :copied-card? :token? :spell? :permanent? :emblem?)))51 (defq has-controller? [obj & _]52 (if (and (not (? in-zone 'battlefield obj))53 (not (? on-stack obj))) false))55 (defq take-turn [player & _] nil)58 (tell! :characteristics (fn[obj & _]59 (list :name :mana-cost :color :card-type :subtype :supertype :expansion-symbol :rules-text :abilities :power :toughness :loyalty :hand-modifier :life-modifier)))64 (tell! :colors65 (fn[obj & _]66 (true-preds obj :red? :blue? :green? :white? :black?)))73 ;; GAME / TURN MECHANICS74 (defn new-game "Start a new two-player game."[]75 (tell! :the-players #{'PLAYERONE 'PLAYERTWO})76 (tell! :life-total (reduce #(assoc %1 (keyword %2) 20) {} (ask :the-players) ))77 )81 ;;(ask :blue) blue? = (fn[& args](fn[k] (k ...) ))82 ;; (cpt (constantly nil))83 ;;(ask k) = (get self k (cpt(constantly nil)))90 (defn reverse&[coll k]91 (if (empty? coll) (k '())92 (recur (rest coll) (fn[x](k (conj x (first coll))))94 )))111 (defn choose "Asks player to choose a member of the universe which matches all the given predicate; returns the chosen member." [player & preds]113 )116 (defn get* "Returns the value of key for the object, default or nil if key is not present."117 ([obj key])118 ([obj key default]))132 ;; shuffle decks133 ;; anyone may cut anyone else's deck134 ;; deck => library135 ;; decide who goes first136 ;; players decide whether to mulligan in APNAP137 ;; players mulligan simultaneously138 ;; ... finish mulligans139 ;; opening hand abilities140 ;; beginning game abilities141 ;; 2P game: first player skips draw phase of first turn143 ;; pooled mana: produced-by, spendable-on148 ;; VALUES WITH MODIFICATION HISTORY149 ;(deftype trace[subject ])151 ;(defn trace [x]{:subject x :modifiers '()})152 ;(defn modifiers[tr] (get :modifiers tr '()))153 ;(defn subject[tr] (:subject tr))155 ;(defn modify[tr f](assoc tr :modifiers (conj (modifiers tr) f)))156 ;(defn compute[tr] (:modifiers tr))158 ;; players