Posted By

MMDeveloper on 01/14/09


Tagged

php life deduction parody


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

Scooter
umang_nine
brent-man


Life


 / Published in: PHP
 

what else can be coded into this "life program"... just a boredom project of mine.

  1. <?php
  2. class life {
  3. public $global = NULL;
  4. public $self = NULL;
  5. private $children = array();
  6. private $limits = stdClass;
  7.  
  8. private $daysAliveThisYear = 0;
  9.  
  10. public function __construct() {
  11. $this->global = $this->self = new stdClass();
  12.  
  13. $this->limits = $this->convertArrayToObject(array (
  14. "age" => rand(0, 112),
  15. "startFertility" => rand(12, 14),
  16. "endFertility" => rand(43, 52)
  17. ));
  18.  
  19. $this->global = $this->convertArrayToObject(array (
  20. "power" => 0,
  21. "respect" => 0,
  22. "appearance" => 0,
  23. "neglect" => 0
  24. ));
  25.  
  26. $this->self = $this->convertArrayToObject(array (
  27. "money" => 0,
  28. "respect" => 0,
  29. "ego" => 0,
  30. "education" => 0,
  31. "age" => 0,
  32. "weight" => $this->randomFloat(4, 8, 2), //pounds
  33. "stopGrowingAge" => rand(15, 25),
  34. "height" => $this->randomFloat(14, 20, 4), //inches
  35. "alive" => true
  36. ));
  37. $this->birth();
  38. }
  39.  
  40. public function kickBucket() {
  41. $this->self->alive = false;
  42. $this->global->power = 0;
  43. $this->global->respect = $this->global->respect / 3;
  44. $this->global->appearance = $this->global->appearance / 2;
  45. $this->setValues($this->self, 0);
  46. sleep(864000); //10 days for rotting
  47. $this->global->appearance = 0;
  48. }
  49.  
  50. private function birth() {
  51. $this->demandAttention();
  52. while($this->self->alive === true) {
  53. $this->typicalDay();
  54. sleep(86400); //1 day
  55. }
  56. }
  57.  
  58. private function typicalDay() {
  59. ++$daysAliveThisYear;
  60. if ($daysAliveThisYear == 360) {
  61. ++$this->self->age;
  62. $daysAliveThisYear = 0;
  63. } else {}
  64.  
  65. $this->educate();
  66.  
  67. $this->self->ego += rand(-5, 5);
  68. $this->self->respect += rand(-5, 5);
  69. $this->self->neglect += rand(-5, 5);
  70.  
  71. //if ate food today
  72. $this->self->weight += ($this->calcProbabilityBool() === true) ? $this->randomFloat(-0.1, 1, 4) : $this->randomFloat(-1, 0.5, 4);
  73.  
  74. if ($this->self->age <= $this->self->stopGrowingAge) {
  75. $this->self->height += $this->randomFloat(0.001, 0.003, 4) / 12;
  76. } else {}
  77.  
  78. if ($this->self->age >= $this->limits->startFertility && $this->self->age <= $this->limits->endFertility && $this->self->age % 3 == 0) {
  79. if ($this->calcProbabilityBool(rand(1,10), rand(1,400)) === true) {
  80. $this->children[] = new life();
  81. } else {}
  82. } else {}
  83. $this->demandAttention();
  84.  
  85. /*
  86. if old or you happen to get killed today
  87. */
  88. if ($this->self->age == $this->limits->age || ($this->calcProbabilityBool(rand(1,10), rand(1,1000)) === true)) {
  89. $this->kickBucket();
  90. } else {}
  91. }
  92.  
  93. public function educate() {
  94. $this->self->education += rand(1, 10);
  95. }
  96.  
  97. private function setValues(&$object, $val, $keys = array()) {
  98. $numKeys = intval(count($keys));
  99. foreach ($object as $k => $v) {
  100. if (($numKeys > 0 && in_array($k, $keys) === true) || $numKeys < 1) {
  101. $object->$k = $val;
  102. } else {}
  103. }
  104. }
  105.  
  106. private function demandAttention() {
  107. if ($this->calcProbabilityBool() === true) {
  108. ++$this->global->power;
  109. ++$this->global->respect;
  110. ++$this->global->neglect;
  111. }
  112. else {
  113. --$this->self->ego;
  114. --$this->self->respect;
  115. --$this->global->neglect;
  116. ++$this->self->education;
  117. }
  118. }
  119.  
  120. private function calcProbabilityBool($prob_True = 100, $prob_False = 150) {
  121. $probabilityMatrix = array_merge(array_fill(0, $prob_True, true), array_fill(0, $prob_False, false));
  122. shuffle($probabilityMatrix);
  123. srand(floatval(microtime() * 10000000));
  124. return array_rand($probabilityMatrix, 1);
  125. }
  126.  
  127. private function randomFloat($min = 1, $max = 10, $places = 3) {
  128. return round(($min + lcg_value() * (abs($max - $min))), $places);
  129. }
  130.  
  131. private function convertArrayToObject($array) {
  132. $result = new stdClass();
  133.  
  134. foreach ($array as $k => $v) {
  135. if (is_array($k) === true) {
  136. $result->$k = $this->convertArrayToObject($v);
  137. }
  138. else {
  139. $result->$k = $v;
  140. }
  141. }
  142.  
  143. return $result;
  144. }
  145. }
  146.  
  147. $self = new life();
  148. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Scooter on January 16, 2009

You could add a random height calculation in the constructor and then grow in size over time until that height is reached. You could also add a weight value that fluctuates over time, possibly dependant on food intake.

Posted By: MMDeveloper on January 20, 2009

I like the idea.. I'll work on that soon

Posted By: MMDeveloper on January 20, 2009

Ok I've got some height and weight factors in it. At birth, your height and weight are random values (these values are average statistics from google searching on U.S. babies). Every day, your height increases by a random floating point between .001 inches and .003 inches; so as long you are under your pre-determined age at which you stop growing (which is also random per life).

If you have eaten food today, your weight gain can still drop, but favors more toward a slight gain, however if you did not eat today, your weight gain can still increase but favors more toward a decrease.

You need to login to post a comment.