#!/usr/bin/env python2.4 ''' Osm by Mike Kramlich of ZodLogic Games http://zodlogic.webfactional.com ''' ''' README Osm is a simple simulation of a small society. It has been brought to you by the letters O, S and M, but most especially by S. (see 'simple simulation of a small society' for details) The primary purpose of Osm is to serve as an online and freely downloadable example of my knowledge of programming, OOD, Python and game design. It's small and simple, but hey, it's better than nothing. I may replace it with newer, better revisions in the future, who knows. The secondary purpose is to serve as a free springboard for new programmers or game designers to hack on and play with. Instructions: run it, read it, understand it, run it again, then read again, tweak, get error, fix, run again, hack, nasty hack, run, tweak again, hack, hack, cough, hack, cough. The quaternary purpose is to promote ZodLogic Games. Did I forget to mention ZodLogic Games? I know I have a link to it around here somewhere... *rummages around in the dark* Oh yes, here it is: http://zodlogic.webfactional.com There is no tertiary purpose. No really. There is no tertiary purpose. These are not the tertiary porpoises you are looking for. This program is made available to the public under no fancy-shmancy Capital Name license. You are free to use it, hack it, modify it, run it. I ask that you not sell it (though I don't see why you would try to) or try to pass it off as your own creation. You are free to create and distribute derivative works. In that case, I ask that you publically acknowledge that your work is based on mine, and display my __doc__ string value (shown above) somewhere in your version. I retain copyright and control over my original version of the source, of course. Because that's a wonderful horse, of course. I was really desperately trying to make that rhyme again. I apologize. Any decent programmer should know how to run and use this program, especially one familar with Python or Unix. It may run just fine, without any special hand-waving on your part on most modern machines, particularly Linux or Mac. You mainly need Python installed. All modern Macs have it. Most Linux boxes probably have it or can get it easily. And no other platforms or operating systems exist, that I'm aware of. *crickets chirping* So let's get to the actual code... ''' import random import sys people = [] def msg(txt): print txt class Person: def __init__(self, name, money=0): self.name = name self.money = money self.car = False def act(self): pass def __str__(self): return self.name class Steve(Person): def __init__(self, name='Steve Miller', money=10000): Person.__init__(self,name,money) def act(self): car_need(self) if odds(1,3): msg('%s studies.' % self.name) else: pay = 100 self.money += pay msg('%s works, earning $%s.' % (self.name,pay)) class Bubba(Person): def __init__(self, name='Bubba Diddly', money=1000): Person.__init__(self,name,money) def act(self): car_need(self) if odds(1,5): msg('%s exclaims, "Well, golly!"' % self.name) else: msg('%s watches TV.' % self.name) class Arthur(Person): def __init__(self, name='Arthur von Reiche', money=1000000): Person.__init__(self,name,money) def act(self): car_need(self) msg('%s plays polo.' % self.name) class Goka(Person): def __init__(self, name='Goka', money=10): Person.__init__(self,name,money) def act(self): msg('%s scavenges for food at the village dump.' % self.name) def car_need(p): cost = 10000 if not p.car and cost <= p.money: p.money -= cost p.car = True msg('%s buys a car for $%s and has $%s left.' % (p.name, cost, p.money)) elif p.car: msg('%s already has a car.' % p.name) elif cost > p.money: msg('%s does not have the $%s needed to buy a car, only $%s.' % (p.name, cost, p.money)) def odds(num, denom): return num >= random.randint(1,denom) def status(): for p in people: msg('\t%s: $%s' % (p.name,p.money)) def usage(): print 'Usage:' print 'osm []' def main(): global people tocks = 5 if len(sys.argv) >= 2: arg = sys.argv[1] try: tocks = int(arg) if tocks < 0: tocks = 0 except: print 'problem parsing argument "%s" so aborting.' % arg usage() sys.exit(1) print __doc__ print '-' * 80 people = [Steve(), Bubba(), Arthur(), Goka()] pres_party2names = {} pres_party2names['Republican'] = ('Reagan','Bush') pres_party2names['Democrat'] = ('Clinton','Obama') pres_name = 'Bush' pres_party = 'Republican' savings_interest_rate = 0.03 inherit_tax_rate = 0.5 msg('There are %s people in the world:' % len(people)) names = [] for p in people: names.append(p.name) names = ', '.join(names) msg('\t%s.' % names) msg('Here are their respective starting situations:') status() s = (tocks != 1) and 's' or '' print 'Now the simulation will run for %s tock%s.\n' % (tocks, s) for t in xrange(tocks): for p in people: interest = int(p.money * savings_interest_rate) p.money += interest if interest > 0: msg('%s earns $%s in savings interest.' % (p.name, interest)) p.act() for p in people[:]: if odds(1,20): childname = '%s+' % p.name childclass = p.__class__ child = childclass(childname,0) people.append(child) msg('%s has a child named %s.' % (p.name,childname)) people.remove(p) msg('%s dies.' % p.name) inheritance = int(p.money - (p.money * inherit_tax_rate)) child.money += inheritance msg('%s inherits $%s.' % (child.name,inheritance)) if p.car and odds(1,2): child.car = p.car msg('%s inherits a car' % child.name) if odds(1,4): pres_party = random.choice(pres_party2names.keys()) pres_name = random.choice(pres_party2names[pres_party]) msg('US Presidential election occurs. %s a %s wins.' % (pres_name,pres_party)) if pres_party == 'Republican' and odds(1,3): msg('President %s proposed cutting taxes again for the very rich.' % pres_name) if pres_party == 'Republican' and inherit_tax_rate > 0 and odds(1,3): inherit_tax_rate /= 2 msg("President %s's proposal passed to CUT the inheritance tax rate to %s." % (pres_name,inherit_tax_rate)) if pres_party == 'Democrat' and inherit_tax_rate < 0.5 and odds(1,3): inherit_tax_rate *= 2 inherit_tax_rate = max(inherit_tax_rate,0.5) msg("President %s's proposal passed to RAISE the inheritance tax rate to %s." % (pres_name,inherit_tax_rate)) if odds(1,3): msg('The majority of Senate Republicans today voted in favor of more pollution.') if odds(1,3): msg('The majority of Senate Democrats today voted in favor of more schools, teachers, smaller class sizes and/or more college financial aid.') if odds(1,3): msg('The majority of Senate Republicans today voted in favor of greater use of religion throughout government. Especially their religion.') # Tock End Status Report msg('Status: (%s)' % t) status() if __name__ == '__main__': main()