Skip to content

Instantly share code, notes, and snippets.

@bChiquet
Created January 6, 2017 11:01
Show Gist options
  • Save bChiquet/6cf09fa7d6271d529068c717963f6e5a to your computer and use it in GitHub Desktop.
Save bChiquet/6cf09fa7d6271d529068c717963f6e5a to your computer and use it in GitHub Desktop.
import random
city = {"gold" : 5,
"food" : 5,
"health" : 5,
"civilians" : 5,
"army" : 5}
def hasFallen (city) :
def militaryPutsch(city):
return city["army"] >= 10
def civilianOverthrow(city):
return city["civilians"] >= 10
def noCivilianLeft(city):
return city["civilians"] <= 0
return noCivilianLeft(city) or civilianOverthrow(city) or militaryPutsch(city)
def onlyWithinBounds(event):
def eventWithinBound(*args):
def enforceParameterBounds(city):
for param in city:
if city[param] > 10 : city[param] = 10
if city[param] < 0 : city[param] = 0
city = args[0]
event(city)
enforceParameterBounds(city)
return eventWithinBound
def randomEventHappensTo(city):
@onlyWithinBounds
def harvest(city):
city["food"] +=1
@onlyWithinBounds
def trade(city):
city["gold"] +=1
@onlyWithinBounds
def plague(city):
city["health"] -=1
@onlyWithinBounds
def hail(city):
city["food"] -=1
@onlyWithinBounds
def medics(city):
city["health"] +=1
possibleEvents = [harvest, trade, medics, hail, plague, plague]
eventHappensTo = random.choice(possibleEvents)
eventHappensTo(city)
def automaticEvolutionOf(city):
def nothingHappens(city):
pass
def increase(param):
@onlyWithinBounds
def increaseParam(city):
city[param] +=1
return increaseParam
def decrease(param):
@onlyWithinBounds
def decreaseParam(city):
city[param] -=1
return decreaseParam
def do(event):
class eventContainer:
def onlyIf(self, predicate):
if predicate :
return event
else :
return nothingHappens
return eventContainer()
def healthImpactOn(city):
return [do(decrease("civilians")).onlyIf(city["health"] <4),
do(decrease("civilians")).onlyIf(city["health"] <2),
do(increase("civilians")).onlyIf(city["health"] >=8)]
def foodImpactOn(city):
return [do(decrease("health")).onlyIf(city["food"] <3),
do(decrease("health")).onlyIf(city["food"] <1),
do(increase("health")).onlyIf(city["food"] >=8)]
def apply(impacts, _, city):
for impact in impacts :
impact(city)
impacts = healthImpactOn(city) + foodImpactOn(city)
apply(impacts, "on", city)
def turnOfPlay(city):
if hasFallen(city) :
print("YOU LOSE")
exit()
else :
automaticEvolutionOf(city)
randomEventHappensTo(city)
print("A year passed")
while 1 :
print(city)
turnOfPlay(city)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment