Skip to content

Instantly share code, notes, and snippets.

@michaelcmartin
Created May 21, 2012 00:21
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 michaelcmartin/2760021 to your computer and use it in GitHub Desktop.
Save michaelcmartin/2760021 to your computer and use it in GitHub Desktop.
Computational core of the 'king' game.
(ns king.model)
(defn year-prices
"Returns an update structure with new prices for land and farming."
[]
{:landvalue (+ 95 (rand-int 11))
:plantcost (+ 10 (rand-int 6))})
(defn initial-stats
"Produces an initial state, which is the Year 1 initial position."
[]
(conj {:treasury (+ 60000 (rand-int 1000) (- (rand-int 1000)))
:termlen 8
:year 1
:population (+ 500 (rand-int 10) (- (rand-int 10)))
:foreignpop 0
:farmland 1000}
(year-prices)))
(defn year-results [{:keys [sold distro farmed pollution-control] :as move}
{:keys [year population treasury farmland foreignpop landvalue plantcost] :as status}]
(let [midfarmland (- farmland sold)
midtreasury (- treasury distro (* farmed plantcost) pollution-control (- (* sold landvalue)))
total-sold (- 1000 midfarmland)
clean-factor (max (int (/ pollution-control 25)) 1)
starved (max 0 (- population (int (/ distro 100))))
polluted (int (/ (rand-int total-sold) clean-factor))
died (min (+ starved polluted) population)
funeral-expenses (* died 9)
forced-sale (if (> funeral-expenses midtreasury)
(int (Math/ceil (/ (double funeral-expenses) ; Uses java.lang.Math - scandalous!
(double landvalue))))
0)
foreign-import (if (> (+ sold forced-sale) 0)
(let [base (+ sold forced-sale (rand-int 10) (- (rand-int 20)))]
(if (< base 0) (+ base 20) base))
0)
migration (- (+ (int (/ distro 100)) (int (/ pollution-control 25))) population (int (/ total-sold 50)) (int (/ died 2)))
crops-spoiled (min farmed (int (/ (* total-sold (+ (rand) 1.5) 0.5) clean-factor)))
harvested (- farmed crops-spoiled)
harvest-profits (int (* harvested (/ landvalue 2)))
tourist-gross (+ (* population 22) (rand-int 500))
tourist-spoiled (int (/ (* total-sold 15) clean-factor))
tourist-net (max 0 (- tourist-gross tourist-spoiled))
final-farmland (- midfarmland forced-sale)
final-treasury (+ midtreasury (* (+ sold forced-sale) landvalue) (- funeral-expenses) harvest-profits tourist-net)
interim-pop (+ population migration (- died))
final-migration (if (< interim-pop 0)
(- migration interim-pop)
migration)
final-pop (max interim-pop 0)
fate (cond (< midtreasury 0) :illegal-overspent
(and (< midfarmland 0) (> farmland 0)) :illegal-oversold
(> farmed (* 2 population)) :illegal-overworked
(and (> farmed 0) (> farmed farmland)) :illegal-overfarmed
(< distro 5000) :midyear-fail
(> died 200) :massdeath
(< final-pop 384) :lowpop
(and (> midtreasury 500) (> starved 1)) :callous
(> (+ (:foreignpop status) foreign-import) final-pop) :coopted
(= year 8) :victory
true :success)]
{:starved starved :polluted polluted :funeral-expenses funeral-expenses
:forced-sale forced-sale :foreign-import foreign-import :migration final-migration
:harvested harvested :harvest-profits harvest-profits :crops-spoiled crops-spoiled
:tourism tourist-net :tourism-spoiled tourist-spoiled :post-budget midtreasury
:farmed farmed :fate fate
:next-status (if (#{:illegal-overspent :illegal-oversold :illegal-overworked :illegal-overfarmed} fate)
status
(conj status
{:treasury final-treasury
:year (inc year)
:population final-pop
:foreignpop (+ (:foreignpop status) foreign-import)
:farmland final-farmland}
(year-prices)))}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment