Moonglobe
09-30-2003, 12:02 AM
well, as most of those reading this forum will know, i recently created the PEBoD classes using php5.......and i was wondering if you guys could find anything wrong in the way i've coded things........ and suggestions for additions are welcome too ;).<?php
class PEBoD
{
public $cage;
public $hunger;
public $anger;
protected $hunger_array;
protected $time_to_eat;
protected $feedback;
protected $e_code;
protected $free;
protected $anger_array;
public function __construct()
{
$this->cage =& new PEBoD_Cage(&$this);
$this->hunger_array = range(0,200,2);
$this->hunger = $this->hunger_array[0];
$this->feedback = "";
$this->anger_array = range(1,100);
$this->anger = $this->anger_array[0];
}
private function contemplate_life($length=10800)
{
$now = time();
if ($length < $now)
{
$then = time()+$length;
}
else
{
$then = $length;
}
while ($now < $then)
{
sleep(1);
$now++;
}
}
private function sf($str="")
{
$this->feedback = $str;
$this->e_code[1] = $str===""?false:true;
return true;
}
public function feedback()
{
return $this->e_code[1]?$this->feedback:"No Feedback";
}
public function put_in_cage()
{
if ($this->free)
{
if ($this->cage->is_open())
{
$this->free = false;
$this->cage->fill();
return true;
}
else
{
$this->sf("Cannot put beaver in closed cage");
return false;
}
}
else
{
$this->sf("Beaver already in cage!");
return false;
}
}
public function is_hungry()
{
if ($this->hunger > 100) return true;
return false;
}
public function feed(&$food)
{
srand(make_seed());
$ranking = is_object($food)?
21
:is_resource($food)?
18
:is_array($food)?
15:is_string($food)?12:is_float($food)?9:is_int($food)?6:is_bool($food)?
3:is_null($food)?0:0;
$hunger_index = ($this->hunger -($ranking + rand((0 - 3),3)))
$this->hunger = $this->hunger_array[$hunger_index];
unset($GLOBALS[array_search($food,$GLOBALS)]);
unset($food);
}
public function is_angry()
{
srand(make_seed());
if (($this->anger + rand((0 - 3),3)) < 51) return true;
return false;
}
}
################################################################################
class Cage
{
public $filled;
protected $feedback;
protected $e_code;
protected $open;
public function __construct()
{
$this->open = false;
$this->feedback = "";
$this->e_code = array(0,0);
}
public function feedback()
{
return $this->e_code[1]?$this->feedback:"No Feedback";
}
public function open()
{
if ($this->open)
{
$this->sf("Cage already open!");
$this->e_code[0] = 1;
return false;
}
else
{
$this->open = true;
$this->sf("Cage opened successfully!");
$this->e_code[0] = 0;
return true;
}
}
public function close()
{
if (!$this->open)
{
$this->sf("Cage already closed!");
$this->e_code[0] = 2;
return false;
}
else
{
$this->open = false;
$this->sf("Cage close successfully!");
$this->e_code[0] = 0;
return true;
}
}
public function is_open()
{
return $this->open;
}
public function is_closed()
{
return !$this->open;
}
public function is_full()
{
return $this->full;
}
public function is_empty()
{
return !$this->full;
}
private function sf($str="")
{
$this->feedback = $str;
$this->e_code[1] = $str===""?false:true;
return true;
}
public function fill()
{
$this->full = true;
return true;
}
}
################################################################################
class PEBoD_Cage extends Cage
{
private $parent;
private $crack;
public function __construct(&$input)
{
if (!is_object($input) && get_class($input) !== "pebod")
{
$this->sf("Input not a proper object!");
return false;
}
if (!$input->cage->is_parented()) $this->parent =& $input;
parent::__construct();
$this->crack = 10.25;
}
public function is_parented()
{
return isset($this->parent);
}
public function shake()
{
srand(make_seed);
$is_free = rand(1,100)>=$this->parent->anger?false:true;
if ($is_free)
{
$this->full = false;
$this->parent->free = false;
$this->parent->sf("Beaver has been released!");
$this->sf("Beaver has been released!");
return "HAHA!";
}
else
{
$this->parent->anger += 10;
$this->sf("Hey! You were lucky this time!");
return true;
}
}
public function pull_in(&$food,$size='')
{
if (!$size && $size !== 0) $size = (strlen($food)+5)/10;
if ($size > $this->crack) return false;
$this->parent->feed($food);
}
}
?>
btw these are Parse Error-Free in PHP5b2-dev.
class PEBoD
{
public $cage;
public $hunger;
public $anger;
protected $hunger_array;
protected $time_to_eat;
protected $feedback;
protected $e_code;
protected $free;
protected $anger_array;
public function __construct()
{
$this->cage =& new PEBoD_Cage(&$this);
$this->hunger_array = range(0,200,2);
$this->hunger = $this->hunger_array[0];
$this->feedback = "";
$this->anger_array = range(1,100);
$this->anger = $this->anger_array[0];
}
private function contemplate_life($length=10800)
{
$now = time();
if ($length < $now)
{
$then = time()+$length;
}
else
{
$then = $length;
}
while ($now < $then)
{
sleep(1);
$now++;
}
}
private function sf($str="")
{
$this->feedback = $str;
$this->e_code[1] = $str===""?false:true;
return true;
}
public function feedback()
{
return $this->e_code[1]?$this->feedback:"No Feedback";
}
public function put_in_cage()
{
if ($this->free)
{
if ($this->cage->is_open())
{
$this->free = false;
$this->cage->fill();
return true;
}
else
{
$this->sf("Cannot put beaver in closed cage");
return false;
}
}
else
{
$this->sf("Beaver already in cage!");
return false;
}
}
public function is_hungry()
{
if ($this->hunger > 100) return true;
return false;
}
public function feed(&$food)
{
srand(make_seed());
$ranking = is_object($food)?
21
:is_resource($food)?
18
:is_array($food)?
15:is_string($food)?12:is_float($food)?9:is_int($food)?6:is_bool($food)?
3:is_null($food)?0:0;
$hunger_index = ($this->hunger -($ranking + rand((0 - 3),3)))
$this->hunger = $this->hunger_array[$hunger_index];
unset($GLOBALS[array_search($food,$GLOBALS)]);
unset($food);
}
public function is_angry()
{
srand(make_seed());
if (($this->anger + rand((0 - 3),3)) < 51) return true;
return false;
}
}
################################################################################
class Cage
{
public $filled;
protected $feedback;
protected $e_code;
protected $open;
public function __construct()
{
$this->open = false;
$this->feedback = "";
$this->e_code = array(0,0);
}
public function feedback()
{
return $this->e_code[1]?$this->feedback:"No Feedback";
}
public function open()
{
if ($this->open)
{
$this->sf("Cage already open!");
$this->e_code[0] = 1;
return false;
}
else
{
$this->open = true;
$this->sf("Cage opened successfully!");
$this->e_code[0] = 0;
return true;
}
}
public function close()
{
if (!$this->open)
{
$this->sf("Cage already closed!");
$this->e_code[0] = 2;
return false;
}
else
{
$this->open = false;
$this->sf("Cage close successfully!");
$this->e_code[0] = 0;
return true;
}
}
public function is_open()
{
return $this->open;
}
public function is_closed()
{
return !$this->open;
}
public function is_full()
{
return $this->full;
}
public function is_empty()
{
return !$this->full;
}
private function sf($str="")
{
$this->feedback = $str;
$this->e_code[1] = $str===""?false:true;
return true;
}
public function fill()
{
$this->full = true;
return true;
}
}
################################################################################
class PEBoD_Cage extends Cage
{
private $parent;
private $crack;
public function __construct(&$input)
{
if (!is_object($input) && get_class($input) !== "pebod")
{
$this->sf("Input not a proper object!");
return false;
}
if (!$input->cage->is_parented()) $this->parent =& $input;
parent::__construct();
$this->crack = 10.25;
}
public function is_parented()
{
return isset($this->parent);
}
public function shake()
{
srand(make_seed);
$is_free = rand(1,100)>=$this->parent->anger?false:true;
if ($is_free)
{
$this->full = false;
$this->parent->free = false;
$this->parent->sf("Beaver has been released!");
$this->sf("Beaver has been released!");
return "HAHA!";
}
else
{
$this->parent->anger += 10;
$this->sf("Hey! You were lucky this time!");
return true;
}
}
public function pull_in(&$food,$size='')
{
if (!$size && $size !== 0) $size = (strlen($food)+5)/10;
if ($size > $this->crack) return false;
$this->parent->feed($food);
}
}
?>
btw these are Parse Error-Free in PHP5b2-dev.