Show sourcecode
The following files exists in this folder. Click to view.
webbserv1-dice/exercises/dice6/
CDice.php
CHistogram.php
dice-6.php
dice-6.php
54 lines UTF-8 Unix (LF)
<?php
include('CDice.php');
include('CHistogram.php');
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Tärningsexempel</title>
</head>
<body>
<h1>En tärning</h1>
<p>Tärningen kastas <a href='?roll=XYZ'>1 gång</a>, <a href='?roll=3'>3 gånger</a>, <a href='?roll=6'>6 gånger</a>, <a href='?roll=25'>25 gånger</a> och här är resultatet.</p>
<p>Tärning, med 10 sidor, kastas <a href='?roll=XYZ&faces=10'>1 gång</a>, <a href='?roll=3&faces=10'>3 gånger</a>, <a href='?roll=6&faces=10'>6 gånger</a>, <a href='?roll=25&faces=10'>25 gånger</a> och här är resultatet.</p>
<?php
// To save the outcome of each dice roll
$rolls = array();
// Get faces
$faces = isset($_GET['faces']) && is_numeric($_GET['faces']) ? $_GET['faces'] : 6;
// Create an instance of the class
$dice = new CDice($faces);
$histogram = new CHistogram();
// Roll the dice
$times = isset($_GET['roll']) && is_numeric($_GET['roll']) ? $_GET['roll'] : 1;
/* Alternative
if (isset($_GET['roll']) && is_numeric($_GET['roll']) ) { $times = $_GET['roll']; }
else { $times = 1}
*/
if($times > 100) {
die("Kan inte kasta fler än 100 gånger.");
}
$dice->Roll($times);
$rolls = $dice->GetRollsAsArray();
// Print out the results as a histogram
$html = null;
foreach($rolls as $val) {
$html .= "{$val}, ";
}
?>
<?=$histogram->GetHistogram($rolls, $faces)?>
<p>Alla tärningskast: <?=$html?></p>
<p>Du kastade <?=count($rolls)?> gånger. </p>
<p>Summan = <?=$dice->GetTotal()?> </p>
<p>Medel = <?= $dice->GetAverage() ?>.</p>
</body>
</html>
