Show sourcecode
The following files exists in this folder. Click to view.
webbserv1-dice/exercises/dice7/
CDice.php
CDiceImage.php
CHistogram.php
css.css
dice-7.php
dicefaces_32x32.jpg
CDice.php
78 lines ASCII Unix (LF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
<?php
/**
* A CDice class to play around with a dice.
*
*/
class CDice {
/**
* Properties
*
*/
protected $rolls = array();
private $faces;
/**
* Contructor
*/
public function __construct($facesInput=6) {
echo __METHOD__;
$this->faces = $facesInput;
}
/**
* Destructor
*/
public function __destruct() {
echo __METHOD__;
}
/**
* Get the number of faces.
*
*/
public function GetFaces() {
return $this->faces;
}
/**
* Get the rolls as an array.
*
*/
public function GetRollsAsArray() {
return $this->rolls;
}
/**
* 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);
}
}
?>
