Ga rden simulator

by erichbschulz

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.js"></script>
<div ng-app="myApp">
  <div ng-controller="myCtrl">
    <h1>
 The garden
   <button ng-click="cycle(world)">
      cycle1
    </button>
    <button ng-click="cycle(world, {rain: 0})">
      drought
    </button>
        <button ng-click="start()">
      start
    </button>        <button ng-click="stop()">
      stop
    </button>
</h1> Average Health: {{world.stats.health / world.stats.pigs | number:1}} Grass per Pig: {{world.stats.grass/world.stats.pigs | number:1}} Pigs: {{world.stats.pigs | number:0}}
    <table>
      <tr ng-repeat="row in world.grid track by $index">
        <td ng-repeat="cell in row track by $index">
          <img src="http://www.freepngimg.com/download/grass/5-grass-png-image-green-grass-png-picture.png" height="{{cell.grass}}" width="10">
          <img src="http://www.outworldz.com/SeamlessTextures/master/Grass%20textures,CC0/3td_Africa_Grass01.png" height="{{cell.thorns*2}}" width="10">
          <span ng-if="cell.critter" title="{{cell.critter.type}} health: {{cell.critter.health}} {{ cell.critter.sex ==='female' ? ( 'due:' + cell.critter.due) : '' }} ">
{{cell.critter.type[0]}}</span>

        </td>
      </tr>

    </table>
    {{world.stats}}
  </div>

JavaScript

"use strict";
//var angular = {module: function() {return {factory: function() {return {controller: function() {}}}}}}

function init_grid(size_x, size_y) {
  var rows = Array(size_x)
  _.each(rows, function(row, x) {
    rows[x] = Array(size_y)
    _.each(rows[x], function(cell, y) {
      rows[x][y] = {
        grass: 5,
        thorns: 1,
        coords: {
          x: x,
          y: y
        }
      }
    })
  })
  return rows
}

function init_critters(world) {
  function empty(grid) {
    var x, y
    do { // find an empty cell
      x = _.random(0, world.size.x - 1, false)
      y = _.random(0, world.size.y - 1, false)
    } while (grid[x][y].critter)
    return {
      x: x,
      y: y
    }
  }
  for (var i = 0; i < 4; i++) { // make 10 critters
    let coords = empty(world.grid)
    let critter = make_critter({
      coords: coords
    })
    console.log('Creating critter', critter, 'at', coords)
    add_critter(world, critter)
  }
}

function add_critter(world, critter) {
  world.critters.push(critter)
  var coords = critter.coords
  world.grid[coords.x][coords.y].critter = critter

}

function make_critter(params) {
  var sex = (Math.random() > 0.5 ? 'male' : 'female')
  var defaults = {
    type: 'pig',
    health: 5,
    sex: sex,
    age: 10
  }
  if (sex === 'female') {
    defaults.due = -10
  }
  return _.defaults(params, defaults)
}

function cycle(world, status) {
  status = _.defaults({}, status, {
    rain: 2
  })
  world.stats = {
      grass: 0,
      thorns: 0,
      pigs: 0,
      health: 0
    }
    // console.log('cycling', status)
    // walk the grid:
  _.each(world.grid, function(row, i) {
    _.each(row, function(cell, j) {
      // grass grows
      if (status.rain > 1 && cell.grass < 20) {
        cell.grass += 1
      }
      // grass dies
      if (status.rain === 0 && cell.grass > 0 && Math.random() > 0.5) {
        cell.grass -= 1
      }
      if (cell.grass < 5 && cell.thorns < 10 && Math.random() > 0.5) {
       ...