Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Civilization generator.  (Read 1463 times)

sjm9876

  • Bay Watcher
  • Did not so much Fall as Saunter Vaguely Downwards
    • View Profile
Civilization generator.
« on: March 31, 2013, 02:00:13 pm »

WARNING: title probably sounds overly impressive.

Basically, i mess around with both python and World building.
I decided to combine the two to a very limited extent.

thus: python def that when called will generate (n) civilizations in a text file, each with 3 keywords, and relations (0-9)to the other civs.

eg format.
A: trait1 trait2 trait3 relB relC relD
B: trait1 trait2 trait3 relC relD
C: trait1 trait2 trait3 relD
D: trait1 trait2 trait3

Note: this is made as a guideline to be built on with imagination, and just a little inspiration.

And here is the python code:
Code: [Select]
def civgen(n):
    from random import randint
    f = open('civ.txt', 'a')
    x = 0
    z = n-1
    g = 0
    while (x < n):
        a = randint(1,3)
        b1 = randint(1,5)
        b2 = randint(1,5)
        b3 = randint(1,5)
        c = randint(1,2)
        y = g
        if (a == 1):
            if ((b1) == 1):
                f.write("""agressive... """)
            elif((b1) == 2):
                f.write("""territorial. """)
            elif((b1) == 3):
                f.write("""crusading... """)
            elif((b1) == 4):
                f.write("""colonizing.. """)
            elif((b1) == 5):
                f.write("""suspicous... """)

            if ((b2) == 1):
                f.write("""agressive... """)
            elif((b2) == 2):
                f.write("""territorial. """)
            elif((b2) == 3):
                f.write("""crusading... """)
            elif((b2) == 4):
                f.write("""colonizing.. """)
            elif((b2) == 5):
                f.write("""suspicous... """)               

            if ((b3) == 1):
                f.write("""religous.... """)
            elif ((b3) == 2):
                f.write("""grandiose... """)
            elif ((b3) == 3):
                f.write("""inventive... """)
            elif ((b3) == 4):
                f.write("""carefree.... """)
            elif ((b3) == 5):
                f.write("""artistic.... """)           

        elif (a == 2):
            if ((b1) == 1):
                f.write("""religous.... """)
            elif ((b1) == 2):
                f.write("""grandiose... """)
            elif ((b1) == 3):
                f.write("""inventive... """)
            elif ((b1) == 4):
                f.write("""carefree.... """)
            elif ((b1) == 5):
                f.write("""artistic.... """)

            if ((b2) == 1):
                f.write("""religous.... """)
            elif ((b2) == 2):
                f.write("""grandiose... """)
            elif ((b2) == 3):
                f.write("""inventive... """)
            elif ((b2) == 4):
                f.write("""carefree.... """)
            elif ((b2) == 5):
                f.write("""artistic.... """)

            if (c == 1):
                if ((b3) == 1):
                    f.write("""agressive... """)
                elif((b3) == 2):
                    f.write("""territorial. """)
                elif((b3) == 3):
                    f.write("""crusading... """)
                elif((b3) == 4):
                    f.write("""colonizing.. """)
                elif((b3) == 5):
                    f.write("""suspicous... """)
            else:
                if ((b3) == 1):
                    f.write("""welcoming... """)
                elif ((b3) == 2):
                    f.write("""scholarly... """)
                elif ((b3) == 3):
                    f.write("""innocent.... """)
                elif ((b3) == 4):
                    f.write("""agricultural """)
                elif ((b3) == 5):
                    f.write("""loyal....... """)
        elif (a==3):
            if ((b1) == 1):
                f.write("""welcoming... """)
            elif ((b1) == 2):
                f.write("""scholarly... """)
            elif ((b1) == 3):
                f.write("""innocent.... """)
            elif ((b1) == 4):
                f.write("""agricultural """)
            elif ((b1) == 5):
                f.write("""loyal....... """)

            if ((b2) == 1):
                f.write("""welcoming... """)
            elif ((b2) == 2):
                f.write("""scholarly... """)
            elif ((b2) == 3):
                f.write("""innocent.... """)
            elif ((b2) == 4):
                f.write("""agricultural """)
            elif ((b2) == 5):
                f.write("""loyal....... """)

            if ((b3) == 1):
                f.write("""religous.... """)
            elif ((b3) == 2):
                f.write("""grandiose... """)
            elif ((b3) == 3):
                f.write("""inventive... """)
            elif ((b3) == 4):
                f.write("""carefree.... """)
            elif ((b3) == 5):
                f.write("""artistic.... """)
        while (y < z):
            y += 1
            r = randint(0,9)
            f.write(str(r) + ',')
        x += 1
        g += 1
        f.write("""
""")

just thought i'd share.
« Last Edit: March 31, 2013, 02:21:21 pm by sjm9876 »
Logged
My dreams are not unlike yours - they long for the safety, and break like a glass chandelier.
But there's laughter and oh there is love, just past the edge of our fears.
And there's chaos when push comes to shove, but it's music to my ears.

Sigtext

emac

  • Escaped Lunatic
    • View Profile
Re: Civilization generator.
« Reply #1 on: March 31, 2013, 06:57:15 pm »

When I'm bored, I occasionally lurk this forum to see what fun coding projects everyone is working on. I enjoy world-building programs, but most people don't post their code, but rather screenshots of the the worlds they've made. I took a look at your code, but when you see a lines upon lines of nested if/else-if statements, it's almost a sure thing there's a better way to accomplish the same task. I don't know Python, but I know C++ and Java, so I thought this would be a fun way to learn a bit, exploring different ways to do the same thing and making code more concise in an unfamiliar environment. I assume you're learning Python yourself, so maybe it would help to see what I've done with your code.

I had to take a minute to analyse your if-else structure. It looks like you have three categories of keywords -- aggressive, religious, welcoming. Two keywords are picked from one category, and one is picked from a second category, but 'aggressive' and 'welcoming' are mutually exclusive.

I put all your keywords into a single array (I think Python calls them lists?) and by calculating a few numbers, I was able to do away with the whole if/elif structure. ~130 lines of code became ~20. If you plan on expanding your code to include more keywords and categories, it should be easier to do so by adding a few words to the list and changing a couple numbers here and there.

Code:
Spoiler (click to show/hide)

There are lots of peculiarities in Python regarding lists and variable assignments that I don't know about and that don't exist in Java or C, so there may yet be better ways of doing this.
Logged

Swiftling

  • Bay Watcher
    • View Profile
Re: Civilization generator.
« Reply #2 on: April 01, 2013, 04:36:06 am »

heh, I did something similar a few days ago while messing around with files and that. It was pretty fun to do.

Code: [Select]
from random import randint
f = open('civilisations.txt', 'a')

def names():

front_syl = ['Ae', 'Mor', 'Say', 'Obar', 'Ur']
end_syl = ['dar', 'gon', 'ken', 'lor', 'bak']

pre = 'The '
verb = ['Rolling ', 'Smashing ', 'Forging ', 'Flying ', 'Dancing ', 'Trekking ', 'Climbing ', 'Dropping ', 'Restoring ']
noun = ['Orphanages ', 'Bridges ', 'Halls ', 'Lakes ', 'Swamps ', 'Cities ', 'Seas ', 'Ravines ', 'Armouries ', 'Hills ']
of = 'of '

f_s = randint(0, len(front_syl) - 1)
e_s = randint(0, len(end_syl) - 1)
v = randint(0, len(verb) - 1)
n = randint(0, len(noun) - 1)

name = front_syl[f_s] + end_syl[e_s]
title = pre + verb[v] + noun[n] + of + name

f.write(title)
f.write('\n')

def civgen():

traits = ['Loyal', 'Honest', 'Fearsome', 'Wealthy', 'Religious']
priorities = ['War', 'Peace', 'Trade']

t = randint(0, len(traits) - 1)
p = randint(0, len(priorities) - 1)

trait = "They are %s," % traits[t]
priority = "They want %s. \n" % priorities[p]

f.write(trait)
f.write('\n')
f.write(priority)

disasterlist = ['Flood', 'Plague', 'War', 'Famine']
flourishlist = ['Trade', 'Agricultural Revolutions', 'Wealth']

pop_range = randint(1, 3)
d = randint(0, len(disasterlist) - 1)
fl = randint(0, len(flourishlist) - 1)

disaster = "it was decimated by %s recently." % disasterlist[d]
flourish = "it has flourished due to %s." % flourishlist[fl]

if pop_range == 1:
pop = str(randint(800, 1500))
low = "The population is around %s, " % pop
f.write(low)
f.write(disaster)
f.write('\n \n')

elif pop_range == 2:
pop = str(randint(2500, 7000))
medium = "The population is around %s." % pop
f.write(medium)
f.write('\n \n')

else:
pop = str(randint(10000, 18000))
high = "The population is around %s, " % pop
f.write(high)
f.write(flourish)
f.write('\n \n')

names()
civgen()

Kind of sloppy, I'm still learning :P
Logged

sjm9876

  • Bay Watcher
  • Did not so much Fall as Saunter Vaguely Downwards
    • View Profile
Re: Civilization generator.
« Reply #3 on: April 01, 2013, 05:14:06 am »

Both pretty sweet. the reason for all the if/elifs was that I'm teaching a mate and wanted an example of what could be done with just what we'd done so far. that and that I'm still not used to arrays. but cheers. This is why I posted it here. Constructive criticism FTW.  :D

edit:
 
for i in range()
does i increment automatically here?
« Last Edit: April 01, 2013, 05:48:36 am by sjm9876 »
Logged
My dreams are not unlike yours - they long for the safety, and break like a glass chandelier.
But there's laughter and oh there is love, just past the edge of our fears.
And there's chaos when push comes to shove, but it's music to my ears.

Sigtext

Killjoy

  • Bay Watcher
    • View Profile
Re: Civilization generator.
« Reply #4 on: April 01, 2013, 08:07:50 am »

for i in range()
does i increment automatically here?

Well, it is not quite that simple. But essentially yes.

Basic looping can be done in two ways:
Code: [Select]
for i in range(n1, n2):
  #stuff
Means that i will iterate from n1 through all the integers between n1 and n2 but not n2.

Code: [Select]
for i in range(n):
  #stuff
Mean that i will iterate from 0 through all the integers to n, but not n.

How python works is actually quite confusing as python changed the meaning of range since 3.0, and functionality of for. I won't go into it because it is not really important, and it will be quite some time before python 3.0 will be relevant. (Even if we are at python 3.3 now...)

You can also use the for loop on lists, sets, maps and tuples. Iterating is very easy in python.
Code: [Select]
#Iterating over list
for i in [1,2,3, 4]:
  print(i)


#Iterating over set
for i in {1,1,2,2,3,3,4,4}:
  print(i)


#Iterating over tuple
for i in (1,2,3,4):
  print(i)


#Iterating over map
for k, i in {"first":1, "second":2, "third":3, "forth":4}.items():
  print(k + " " + str(i))

In 3.0 you can also iterate over generators (As far as I know), but I doubt that will be relevant for you at the moment. It is mostly semantics.
Logged
Merchants Quest me programming a trading game with roguelike elements.

sjm9876

  • Bay Watcher
  • Did not so much Fall as Saunter Vaguely Downwards
    • View Profile
Re: Civilization generator.
« Reply #5 on: April 01, 2013, 08:15:17 am »

Cheers, that's quite neat.
Logged
My dreams are not unlike yours - they long for the safety, and break like a glass chandelier.
But there's laughter and oh there is love, just past the edge of our fears.
And there's chaos when push comes to shove, but it's music to my ears.

Sigtext