<?php
class Dice
{
var $diceType = 20;
var $numberDice = 1;
var $result = array();
function Dice()
{
}
function setDiceType($type)
{
if($this->isInteger($type))
{
$this->diceType = $type;
return(TRUE);
}
else
{
return(FALSE);
}
}
function setNumberDice($number)
{
if($this->isInteger($number))
{
$this->numberDice = $number;
return(TRUE);
}
else
{
return(FALSE);
}
}
function isInteger($value)
{
$number = $value + 0;
if($value != $number)
{
return(FALSE);
}
return(is_int($number));
}
function roll()
{
$this->result = array();
for($ix = 1; $ix <= $this->numberDice; $ix++)
{
$this->result[$ix] = rand(1, $this->diceType);
}
}
function displayResult()
{
$class = "die";
switch($this->diceType)
{
case 4:
$class .= " four";
break;
case 6:
$class .= " six";
break;
case 8:
$class .= " eight";
break;
case 10:
$class .= " ten";
break;
case 12:
$class .= " twelve";
break;
case 20:
$class .= " twenty";
break;
}
foreach($this->result as $value)
{
echo "<div class='$class'><p class='shadow'>$value</p><p class='top'>$value</p></div>\n";
}
}
function total()
{
if(count($this->result))
{
return(array_sum($this->result));
}
return(FALSE);
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang='en'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>
<title>Page Title</title>
<!-- link rel='stylesheet' href='style.css' type='text/css' -->
<style type="text/css">
<!--
body {
margin: 0;
padding: 10px 20px;
font: medium arial, helvetica, sans-serif;
}
.die {
float: left;
height: 75px;
width: 118px;
margin: 10px;
padding: 35px 0 0 2px;
font: 45px arial, helvetica, sans-serif;
color: #fc3;
font-weight: bold;
text-align: center;
line-height: 1.3em;
}
.four {
background: white url(d4.jpg) no-repeat 50% 50%;
}
.six {
background: white url(d6.jpg) no-repeat 50% 50%;
}
.eight {
background: white url(d8.jpg) no-repeat 50% 50%;
}
.ten {
background: white url(d10.jpg) no-repeat 50% 50%;
}
.twelve {
background: white url(d12.jpg) no-repeat 50% 50%;
}
.twenty {
background: white url(d20.jpg) no-repeat 50% 50%;
}
.die p {
margin: 0;
padding: 0;
line-height: 1em;
text-align: center;
}
.die .shadow {
color: black;
}
.die .top {
position: relative;
top: -1.05em;
left: -0.05em;
}
#total {
clear: both;
font-weight: bold;
font-size: large;
padding-top: 1em;
}
select { margin-right: 1em; }
hr {
margin: 2em 0 1em;
height: 0;
border-top: solid 1px black;
}
-->
</style>
</head>
<body>
<h1>PHP Dice Roller</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>
<p>
<label for='number'>Number of Dice:</label>
<select name='number' id='number'>
<?php
for($num = 1; $num <= 10; $num++)
{
$selected = (!empty($_POST['number']) and $_POST['number'] == $num) ?
' selected' : '';
echo "<option$selected>$num</option>\n";
}
?>
</select>
<label for='type'>Dice Type:</label>
<select name='type' id='type'>
<?php
foreach(array(4, 6, 8, 10, 12, 20) as $type)
{
$selected = (!empty($_POST['type']) and $_POST['type'] == $type) ?
' selected' : '';
echo "<option$selected>$type</option>\n";
}
?>
</select>
<input type='submit' name='submit' value='Roll Dice'></p>
</form>
<?php
if(!empty($_POST['type']) and !empty($_POST['number']))
{
$dice = new Dice();
$dice->setDiceType($_POST['type']);
$dice->setNumberDice($_POST['number']);
$dice->roll();
$dice->displayResult();
echo "<p id='total'>Total: " . $dice->total() . "</p>\n";
}
?>
<hr>
<p><a href="showcode.php">View the source code.</a></p>
<p><a href="/index.php">Home Page</a></p>
<p>Copyright © 2006 by Charles Reace<br>
If you find this program useful and would like to encourage me to post more
programs here, how about buying me something from my
<a href="http://www.amazon.com/gp/registry/wishlist/35197HOKJQ38O/103-1622520-8686266?reveal=unpurchased&filter=all&sort=priority&layout=compact&x=9&y=14">Amazon.com wish list</a>.</p>
</body>
</html>