Webbserverprogrammering 1

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

dice_7.php

94 lines UTF-8 Windows (CRLF)
<?php
error_reporting
(-1); // Report all type of errors
ini_set('display_errors'1); // Display all errors 
ini_set('output_buffering'0); // Do not buffer outputs, write directly

session_start();

?>

<!DOCTYPE html>
<html lang="sv">
<head>
 <title>Tärning 7</title> <!-- Egentligen uppgift 7 -->
 <meta charset="utf-8">
 <style type="text/css">
  body {
   font-family: Arial;
  }
  img {
   width: 2.7em;
   margin-right: 0.2em;
  }
  h2 {
   margin: 0.68em 0;
  }
  #histogram {
   position: absolute;
   top: 17vh;
   left: 34vw;
   font-family: Monospace;
  }
  .histogram-label {
   white-space: pre;
  }
 </style>
</head>
<body>
 <h2>En tärning</h2>
 
 <?php

 
include("Dice.php");
 include(
"DiceImage.php");
 include(
"Histogram.php");

 if (isset(
$_POST["submit"]) || !isset($_SESSION["dice6rolls"]))
  
$_SESSION["dice6rolls"] = [];

 if (isset(
$_GET["faces"]) && $_GET["faces"] > 0)
  
$myDice = new DiceImage($_GET["faces"]);
 else
  
$myDice = new DiceImage();

 if (isset(
$_GET["roll"]) && $_GET["roll"] > 0)
  
$myDice->roll($_GET["roll"]);
 else
  
$myDice->roll();

 
$_SESSION["dice6rolls"] = array_merge($_SESSION["dice6rolls"], $myDice->getRolls());

 echo 
"<p>En tärning med {$myDice->getFaces()} sidor kastas " count($myDice->getRolls()) . " gånger.</p>";
 
?>

 Kasta: <a href="?roll=3">3 gånger</a>, <a href="?roll=6">6 gånger</a>, <a href="?roll=11">11 gånger</a>, <a href="?roll=26">26 gånger</a>.
 Default är 6 gånger. Alla heltal större än 0 är tillåtna.
 
 <?php

 $myDice
->printRolls();
 
$myDice->getRollsAsImages();

 
// parameters are the values & maximum possible value
 // the highest roll might not be the maximum possible value, it is recommended to use both parameters.
 // Default value of "max possible value" is the actual highest value in values-array
 
Histogram::getHistogram($myDice->getRolls(), $myDice->getFaces());

 
?>
  <form method="post" action="">
   <p><strong>Totalt sessionsmedelvärde:</strong> <?= round(array_sum($_SESSION["dice6rolls"]) / count($_SESSION["dice6rolls"]), 2?>
    &nbsp;&nbsp;&nbsp;&nbsp;
    <input type="submit" value="Rensa sessionsmedelvärde" name="submit" id="clearSession">
   </p>
  </form>

  <p><strong>Summa:</strong> <?= $myDice->getSumOfRolls() ?></p>
  <p><strong>Medelvärde:</strong> <?= $myDice->getAverageRoll() ?></p>
 


 <script type="text/javascript">

 </script>
</body>
</html>