from random import randrange class Terminate(Exception): pass def decision(quest,cond,msg): while True: val = raw_input(quest) if val.lower() == "quit": raise Terminate(quitstrings[randrange(0,len(quitstrings))]) try: val = abs(int(val)) if cond(val): return val print msg except: print "Please enter a number, or 'quit'" quitstrings = [ "Coward!","Game Quitter!","Chicken!"] deathstrings = [ "The survivors revolt and put a poisonous snake in your \ bed; you die.", "The survivors put poison in your drink; you die.", """You were taking a walk in the night, suddenly hands grab your shoulders... your funeral is very small; only your wife is there.""", """You wake up in the middle of the night when you hear your wife scream that the assasins are here. You knew they would come - there was no food. You see a glint in the air, you try to hide but it's to late - you and your wife are buried the next morning.""", "The survivors revolt, you are thrown out of a window.", """The survivors revolt - they starve you, whip you till you're half dead, then chop your head off.""", """You are riding on your horse by a cliff. Suddenly a stone flies through the air and hits your horse. He rears and you fall into the cliff...""", """ There is a war because of the lack of food; half the city is on your side, and the other half is against you; you die fighting.""", """You were murdered by Mr. Carey!""" ] grain = 2000 people = 500 wisemen = 0 deaths = 0 births = 0 people= people - deaths + births soldiers = 0 attack = 0 turn = 1 strength = grain + (soldiers*10)//people def cycle(): global attack,turn,people,grain,wisemen,soldiers,deaths,births,strength if attack == 1: print " " print "The Assyrians attacked you..." enemy = soldiers + randrange(-25,25) print "They have %d soldiers." % enemy print "You have %d soldiers." % soldiers oursurvivors = randrange(10,(soldiers-10)) theirsurvivors = randrange(10,(enemy-10)) if oursurvivors > theirsurvivors: print "You defeated the enemy!" print "%d soldiers survived." % oursurvivors soldiers = soldiers - oursurvivors attack = 0 elif oursurvivors < theirsurvivors: print "The enemy defeated you." deaths = randrange(10,(people)) print "%d peasants were killed." % people wisemen = 0 print "All your wisemen were killed." if oursurvivors+10 < theirsurvivors: raise Terminate("You were executed by the enemy.") elif oursurvivors+10 > theirsurvivors: print "The enemy has graciously saved your life." attack = 0 print "","","" print "You are on turn number %d" % turn print "There are %d peasants in your city." % people print "You have %d bushels of grain." % grain print "You have %d wisemen." % wisemen print "You have %d soldiers." %soldiers if turn != 1: if deaths != 0: print "%d peasants died." % deaths print "%d peasants were born." % births promotions = decision("How many peasants do you want to promote to wisemen?", lambda x: x <= people,"You don't have that many peasants") wisemen = wisemen + promotions people = people - promotions if turn != 1: demotions = decision("How many wisemen do you want to demote to peasants?", lambda x:x <= wisemen, "You don't have that many wisemen") wisemen = wisemen - demotions people = people + demotions enlistments = decision("How many peasants do you want to enlist in the army?", lambda x: x <= people,"You don't have that many peasants.") soldiers = soldiers + enlistments people = people - enlistments print "You now have %d peasants, %d wisemen, and %d soldiers." % (people, wisemen, soldiers) plant=decision("How much will you plant?",lambda x:x <= people, "With only %d peasants, you can plant at most %d bushels." % ( people,people )) tempfood = grain - plant print "At most, you can give %d bushels of food to the people." % tempfood food=decision("How much food will you give the people?", lambda x: x + plant <= grain, "We don't have that much grain.") yld=randrange(0,11) harvest=yld*plant store=grain-plant-food grain = grain + store shrinkage=randrange(10,50)- wisemen/5 if shrinkage < 0: shrinkage = 0 loss=store*shrinkage/100 print "You harvested %d bushels." % harvest print "You stored %d bushels." % store print "The rats ate %d bushels." % loss req = people + wisemen*1.5 + soldiers*2 + 3 turn += 1 if food > req: migration = randrange(0,50) * (food - people) /100 print "%d peasants came to the city." % migration deaths = randrange(0,10) * people/100 births = randrange(0,10) * people/100 people = people + migration + births - deaths attack = randrange(0,strength) if attack == 5: attack = 1 elif food < req: starved = max(req - food,people) print "%d peasants starved." % starved people = people - starved if starved > people /2: raise Terminate(deathstrings[randrange(0,len(deathstrings))]) if __name__ == '__main__': try: while True: cycle() except Terminate,x: print x