Tuesday, October 15, 2024
Analytics Coding STEM

“God” game—revisited (aka Pop game)

What seems like yonks ago, I wrote a fun, DOS based visual game in college that’s a simple simulation of population growth with some user-inputs to the game (i.e. playing “God”, hence the title). You can read about it here. But here, I’m sharing info about that game, revisited, with a different twist. I rewrote it in Python (for cerebral exercise, education, and fun), and added new triggers, scoring, and factors governing the game. Basically, a new game but still around population.

Here’s the premise: you start with a given population (children, adults, seniors make it up…the ratio is set by the program). They’re born and they die at a ratio that’s globally a reality in developed countries today each year. In addition to the natural births, deaths, every x years, there’s a calamity of some sort that wipes out some population. You’re given a starting fund. You need the fund to sustain your land (and population).

There are other factors that also affect how successful you’ll be in ruling your principality. To let my code (i.e. PC) do most of the simulation, I only limit the user-inputs to 2 variables: Education, and Tax. However, the rest of the factors are calculated and re-calculated each year and they affect: Environment, Density, overall health (OHI), and in addition to the default birth/death rates, the demographics of your population will also affect birthrate (e.g. if there are more seniors or children, the birthrate is going to below lower if your population is, say, under 40, and so on). Of course, the birth/death rates affect density, density affects safety, environment…which affect the OHI. Tax rates add to your fund but after a certain limit, the OHI will take a hit. Education improves safety and environment variables, and it also lowers birth rates. The more people in your population, the more fund you’ll be inflowing based on your tax rate but the maintenance cost of running your empire will also go up. At the end of each year, the demographic is shuffled by the program to keep it fresh and less predictable. That’s the gist.

The entire game starts with minimal input and requires zero to minimal input. If you just let it run, you’ll  lose after about 110 years (cycles) by running out of fund. To score higher and continue the game, you’ll need to adjust tax and education (which affects the other variables mentioned above) every now and then. The screen will show the status of each cycle, and it will also log all the data.

During the game, you can interrupt the loop by ctrl-c and then entering the allowable inputs, which are: T to increase tax. V to decrease tax. E to increase Education investment, X to decrease it.

Additionally, ? for helpful tips. And Q to quit the game.

Each actual length of a year is of course set by the gamer…from milliseconds to hours per cycle. The + and – keys make the game go faster or slower respectively.

Game ends when any of these conditions occur: You’re out of fund. Population is below 2. Your OHI is dismal.

But there’s even a bigger twist to all this! 

I not only wanted the game to show all the data on screen, I also wanted the data saved for analysis. The analysis is not just for pretty charts, rather, it helped me tweak the game parameters to make it more fair, sensible, adjust the challenge-level, etc. by understanding trends and affects by parameters over time.

Technical Background

Written in Python. Writes all data to two CSV files: 1 file is for all key metrics of each cycle. 2nd file that logs triggers (e.g. calamities, user-input, game-end conditions).

For analysis, I open the files, merge them by a foreign key into a single table into Excel for charts. After import, the calamities are triggers events logged by the Python app are now aligned with the yearly metrics data per cycle and the table looks like this:

Now that I have all the data set, it’s time to party with charts! With charts, I can now visualize cause-effect and fine-tune the game logic to rectify any anomaly. Additionally, it lets me play with thousands of cycles of data easily and visually!

The Visuals

Here’s a visual rep of the timeline of events from above table for the first 110 years…

The following charts show data for 370 years.

The number of deaths caused by calamities…

When looked at data of about 1,000 years, I see that the density rate slows down considerably (which is exactly what I expected by the code); the following shows the trends for first 370 years…

 

As funds were dropping (due to higher maintenance cost due to population), tax was increased (remember: only tax increase/decrease and education increase/decrease are the user-inputs, everything else is algorithmically controlled) resulting in the fund uptick (green line)…

This clearly shows the point at which the funds started to fall shorter due to higher population (maintenance and tax factored in)…

The OHI is a result of 3 factors: education, safety, environment. The following shows exactly when education investment was increased and thus increased the OHI, safety, and environment…

The effect of population density on safety and environment is easily seen below…

The makeup of population over 370 years. This completely code-driven. The average age seem to be hovering just below 50 years of age…

The Game Code

The actual code is 400+ lines of Python v3.6+ code, so I’m not going to torture you with that here. However, if you want to get more info, please see the support info below.

The app utilizes and demonstrates the following key methods, features, and concepts:

  • Keyboard interrupt during infinte loop
  • File open/close/save as text/CSV without using pandas library
  • Controlling time delay, and randomization functions, and much more.

This post is not meant to be a step-by-step, detailed tutorial, instead to serve key concepts, and approaches to problem-solving. Basic->Intermediate technical/coding knowledge is assumed.
If you like more info or source file to learn/use, or need more basic assistance, you may contact me at tanman1129 at hotmail dot com. To support this voluntary effort, you can donate via Paypal from the button in my Home page, or become a Patron via my Patreon site. A supporter/patron gets direct answers on any of my blogs including code/data/source files whenever possible.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top
+