Källkod
Följande filer och mappar finns under mappen webbserverprogrammering.
Mappar visas till vänster och filer till höger. Klicka på en fil eller mapp för att öppna nedan eller visa dess innehåll.
webbserverprogrammering/exercises/classes_dice/
10 filer
Dice.php
DiceImage.php
Histogram.php
dice_1.php
dice_2.php
dice_3.php
dice_4.php
dice_5.php
dice_6.php
dice_7.php
DiceImage.php
Histogram.php
dice_1.php
dice_2.php
dice_3.php
dice_4.php
dice_5.php
dice_6.php
dice_7.php
Dice.php
49 lines UTF-8 Windows (CRLF)
<?php
class Dice {
private $faces;
private $rolls = [];
function __construct($faces = 6) {
if ($faces < 1)
$faces = 1;
$this->faces = $faces;
// echo "Dice skapad!";
// echo __METHOD__;
}
function __destruct() {
// echo "Dice förstörd!";
// echo __METHOD__;
}
function roll($times = 1) {
for ($i = 0; $i < $times; $i++) {
$this->rolls[count($this->rolls)] = rand(1, $this->faces);
}
}
function printRolls() {
echo "<pre><ul>";
foreach ($this->rolls as $roll) {
echo "<li>$roll</li>";
}
echo "</ul></pre>";
}
function getFaces() {
return $this->faces;
}
function getRolls() {
return $this->rolls;
}
function getSumOfRolls() {
return array_sum($this->rolls);
}
function getAverageRoll() {
return round($this->getSumOfRolls() / count($this->rolls), 2);
}
}
?>