Show sourcecode
The following files exists in this folder. Click to view.
webbserv1-dice/exercises/dice5/
CDice.php
CHistogram.php
dice-5.php
CDice.php
61 lines ASCII Unix (LF)
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
<?php
/**
* A CDice class to play around with a dice.
*
*/
class CDice {
/**
* Properties
*
*/
public $rolls = array();
public $faces;
/**
* Contructor
*/
public function __construct($facesInput=6) {
echo __METHOD__;
$this->faces = $facesInput;
}
/**
* Destructor
*/
public function __destruct() {
echo __METHOD__;
}
/**
* Roll the dice
*
*/
public function Roll($times) {
$this->rolls = array();
for($i = 0; $i < $times; $i++) {
$this->rolls[$i] = rand(1, $this->faces);
}
}
/**
* Get the total from the last roll(s).
*
*/
public function GetTotal() {
return array_sum($this->rolls);
}
/**
* Get the average from the last roll(s).
*
*/
public function GetAverage() {
return round(array_sum($this->rolls) / count($this->rolls), 1);
}
}
?>
