Quantcast
Channel: Forest - A Simulated Ecosystem - Code Golf Stack Exchange
Viewing all articles
Browse latest Browse all 5

Forest - A Simulated Ecosystem

$
0
0

NOTE

This problem was taken from this reddit thread (spoiler alert!), and I have adjusted it to make it fit with this site's format. All credit goes to reddit user "Coder_d00d".

In this problem, we will simulate a forest.

For this simulated forest we will be dealing with 3 aspects.

  • Trees which can be a Sapling, Tree or Elder Tree.
  • Lumberjacks (Hechops down down trees, he eats his lunch and goes to the Lava-try)
  • Bears (He mauls the lumberjacks who smells like pancakes)

A fore-warning: these rules are most probably not perfect. See them as a guideline, and if you need to tweak anything slightly that is fine (spawning rates have been pointed out as an issue, see kuroi neko's answer as an example of this.

Cycle of time:

The simulation will simulate by months. You will progessive forward in time with a "tick". Each "tick" represents a month. Every 12 "ticks" represents a year. Our forest will change and be in constant change. We will record the progress of our forest and analyze what happens to it.

Forest:

The forest will be a two dimensional forest. We will require an input of N to represent the size of the forest in a grid that is N x N in size. At each location you can hold Trees, Bears or Lumberjacks. They can occupy the same spot but often events occur when they occupy the same spot.

Our forest will be spawned randomly based on the size. For example if your value of N = 10. You will have a 10 by 10 forest and 100 spots.

  • 10% of the Forest will hold a Lumberjack in 10 random spots. (using our 100 spot forest this should be 10 lumberjacks)
  • 50% of the Forest will hold Trees (Trees can be one of 3 kinds and will start off as the middle one of "Tree") in random spots.
  • 2% of the Forest will hold Bears.

How you receive the size of the forest is up to you (read from stdin, a file, or hardcode it in). I would recommend keeping N like 5 or higher. Small Forests are not much fun.

Events:

During the simulation there will be events. The events occur based on some logic which I will explain below. I will describe the events below in each description of the 3 elements of our forest.

The events follow the order of trees first, lumberjacks second and bears last.

Trees:

  • Every month a Tree has a 10% chance to spawn a new "Sapling". In a random open space adjacent to a Tree you have a 10% chance to create a "Sapling".

  • For example a Tree in the middle of the forest has 8 other spots around it. One of these (if they are empty) will become a "Sapling".

  • After 12 months of being in existence a "Sapling" will be upgrade to a "Tree". A "Sapling" cannot spawn other trees until it has matured into a "Tree".

  • Once a "Sapling" becomes a tree it can spawn other new "Saplings".

  • When a "Tree" has been around for 120 months (10 years) it will become an "Elder Tree".

  • Elder Trees have a 20% chance to spawn a new "Sapling" instead of 10%.

  • If there are no open adjacent spots to a Tree or Elder Tree it will not spawn any new Trees.

Lumberjacks:

Lumberjacks cut down trees, they skip and jump they like to press wild flowers.

  • Each month lumberjacks will wander. They will move up to 3 times to a randomly picked spot that is adjacent in any direction. So for example a Lumberjack in the middle of your grid has 8 spots to move to. He will wander to a random spot. Then again. And finally for a third time. NB: This can be any spot (so they can walk into bears, resulting in a maul).

  • When the lumberjack moves, if he encounters a Tree (not a sapling), he will stop and his wandering for that month comes to an end. He will then harvest the Tree for lumber. Remove the tree. Gain 1 piece of lumber.

  • Lumberjacks will not harvest "Saplings".

  • Lumberacks also harvest Elder Trees. Elder Trees are worth 2 pieces of lumber.

Lumber Tracking:

Every 12 months the amount of lumber harvested is compared to the number of lumberjacks in the forest.

  • If the lumber collected equals or exceeds the amount of lumberjacks in the forest a number of new lumberjacks are hired and randomly spawned in the forest.

  • Work out the number of lumberjacks to hire with:floor(lumber_collected / number_of_lumberjacks)

  • However if after a 12 month span the amount of lumber collected is below the number of lumberjacks then a lumberjack is let go to save money and 1 random lumberjack is removed from the forest. Note that you will never reduce your Lumberjack labor force below 0.

Bears:

Bears wander the forest much like a lumberjack. However instead of 3 spaces a Bear will roam up to 5 spaces.

  • If a bear comes across a Lumberjack he will stop his wandering for the month. (For example after 2 moves the bear lands on a space with a lumberjack he will not make any more moves for this month)

  • Lumberjacks smell like pancakes. Bears love pancakes. Therefore the Bear will unfortunately maul and hurt the lumberjack. The lumberjack will be removed from the forest (He will go home and shop on wednesdays and have buttered scones for tea).

  • We will track this as a "Maul" accident.

  • Note that the lumberjack population can never drop below 1 - so if the last lumberjack is mauled just spawn another one in.

Maul Tracking:

  • During the course of 12 months if there 0 "Maul" accidents then the Bear population will increase by 1. If however there are any "Maul" accidents the Lumberjacks will hire a Zoo to trap and take a Bear away. Remove 1 random Bear. Note that if your Bear population reaches 0 bears then there will be no "Maul" accidents in the next year and so you will spawn 1 new Bear next year.

  • If there is only 1 lumberjack in the forest and he gets Mauled, he will be sent home, but a new one will be hired immediately and respawned somewhere else in the forest. The lumberjack population can never drop below 1.

Time:

The simulation occurs for 4800 months (400 years), or until no Saplings, Trees or Elder Trees exist.

Output:

Every month you will print out a map of the forest - perhaps using an ASCII map, or using graphics and colours.

Optional Extras

  • You could output the populations of trees, lumberjacks and bears each tick.
  • You could output whenever an event occurs (e.g: "A bear mauled a lumberjack.")

Scoring

This is a popularity contest, so most upvotes wins!

EDIT - People have pointed out a number of flaws in my rules, and while you can feel free to ask me questions, it is also okay to tweak the rules a bit to suit your own program, or interpretation of the program.


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images