Tärning | Visa källkod | Hangman | XHTML | CSS | How to become a hacker

Visa källkod

Nedanstående filer finns i denna katalogen. Klicka på en fil för att visa dess innehåll.

dice6/

CDice.php
CDiceSvg.php
CHistogram.php
FDice.php
FHistogram.php
common.php
dice.php
source.php
template.php

dice.php
<?php
// ===========================================================================================
//
// dice.php
//
// Description: Kasta lite tärning
//
// Author: Mikael Roos
//


// -------------------------------------------------------------------------------------------
//
// Error handling on/off
//
error_reporting(E_ALL | E_STRICT);
$debug = "";
$debugEnable = TRUE;  // TRUE to enable debugging, FALSE to not print out debug information


// -------------------------------------------------------------------------------------------
//
// Take care of GET variables, and validate them
//
$antal = filter_input(INPUT_GET, 'antal', FILTER_VALIDATE_INT);
if($antal === FALSE || $antal < 0) die("Felaktigt värde."); 


// -------------------------------------------------------------------------------------------
// 
// Create a form for managing input.
//
$form = <<< EOD
<div>
    <form action='{$_SERVER['PHP_SELF']}' method='get'>
        <fieldset>
            <input type='text' name='antal' value='{$antal}' />
            <button type='submit'>Kasta</button>
        </fieldset>
    </form>
    <a href='{$_SERVER['PHP_SELF']}'>Starta om</a>
</div>

EOD;


// -------------------------------------------------------------------------------------------
//
// Throw dice and show outcome and histogram
//
require_once("CDiceSvg.php");
require_once("CHistogram.php");

$histogram     = new CHistogram();
$dice                = new CDiceSvg();

$diceHtml = "<p>";

if($antal) {

    $dice->RollRepeatedly($antal);
    //$slag = $dice->iLastThrows;
    $slag = $dice->GetLastThrows();
    
    $serie    = $histogram->ShowValues($slag);
    $medel    = round($histogram->Average($slag), 1);
    $graf        = $histogram->PrintGraf($slag);

    // Show all dices as Svg
    foreach($slag as $key => $value) {
        $diceHtml .= $dice->GetSvg($value);
    }
    
    // Present the result in text
    $diceHtml .= <<<EOD
<p>
Slagserien är: <br />
{$serie}
</p>

<p>
Medelvärdet av slagserien är: {$medel}
</p>

<p>
En graf över histogrammet:
<br />
{$graf}
</p>

EOD;
} else {
    $diceHtml .= "<a href='http://en.wikipedia.org/wiki/Alea_iacta_est'>Alea iacta est...</a>";
}

$diceHtml .= "</p>";


// -------------------------------------------------------------------------------------------
//
// The content of the page
//
$html = <<<EOD
<div>
<h1>Min Tärning</h1>
<p>
Välkommen att kasta lite tärning, ange hur många kast du vill göra.
</p>
{$form}
</div>
{$diceHtml}

EOD;


// -------------------------------------------------------------------------------------------
//
// Create and print out the html-page
//
require_once('common.php');
$title         = "Min tärning";
$charset     = "iso-8859-1";
$language    = "sv";
$debug         = $debugEnable ? $debug : "";

$html = <<< EOD
<?xml version="1.0" encoding="{$charset}" ?> 
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="{$language}" lang="{$language}">  
    <head>
        <meta http-equiv="Content-Type" content="text/html; {$charset};" />
        <title>{$title}</title>
    </head>
    <body>
        {$header}
        {$html}
        <div style='font-size: small;'>
            {$debug}
        </div>
        {$footer}
        </body>
</html>
EOD;

header("Content-Type: application/xhtml+xml; charset={$charset}");
echo $html;
exit;

?>
 .                        
..:    © Mikael Roos, 2009