BAM! Custom Dice Roller anyone?

This is where you share YOUR scripts with others

Moderators: egami, macek, gesf

Post Reply
xeniar
New php-forum User
New php-forum User
Posts: 67
Joined: Tue Jan 24, 2012 1:54 pm

Sat Feb 04, 2012 4:15 pm

NOTE: There are no coded into this because since you can specify a custom number for the number of sides a die has then, well, that would require a TON of images..... sorry, don't have the time. :D

Here it is:

diceroller.php
(unless you change the filename in both displayRollForm() AND reRollForm() you MUST name this file diceroller.php)

Code: Select all

<?php
if(!class_exists(dice_roller)) {
	class dice_roller {
		private $_rollspecs = array();
		private $_rolldata = array();
		public function rollDice($numrolls,$numdice,$numsides,$numsides_custom){
			if($numsides == "Custom"){ $sides = $numsides_custom; }
			else{ $sides = $numsides; }
			for($x=0;$x<$numrolls;$x++){
				for($y=0;$y<$numdice;$y++){
					$rolldata[$x + 1][] = rand(1,$sides);
				}
			}
			$this->_rollspecs = array(
			"rolls"=>$numrolls,
			"dice"=>$numdice,
			"sides"=>$sides);
			$this->_rolldata = $rolldata;
		}

		public function displayRolldata($sortby,$maxwidth) {
			$column = 0;
			echo "<table>";
			if($sortby == "roll"){$title = "Roll";$subtitle="Dice";}
			elseif($sortby == "die"){
				for($x=0;$x<$this->_rollspecs['dice'];$x++){
					for($y=0;$y<$this->_rollspecs['rolls'];$y++){
						$rolldata[$x][] = $this->rolldata[$y][$x];
					}
				}$title = "Dice";$subtitle="Roll";
				$this->_rolldata = $rolldata;}
			foreach($this->_rolldata as $roll_number=>$dice){
				if($column == 0){ echo "<TR>"; } //Start a new row every 5 columns
				echo "<td>";
				echo "<table border=1 cellpadding=2>
						<tr>
							<th align='center' colspan='2'>$title #$roll_number</th>
							</tr>";
				foreach($dice as $die_number=>$roll){
					echo "<tr><td align='center'>$subtitle #".($die_number+1).": </td><td align='center'>$roll</td></tr>";
				}
				echo "</td></tr></table>";
				if($column == $maxwidth){
					echo "</tr>";
					$column = 0;
				} else {
					$column++;
				}
			}
			echo "</table>";
		}

		function displayRollForm(){

		$commonsides = array(0=>"4","6","8","10","12","20","Custom");
			echo "<form method='post' action='diceroller.php'>
		<table border='0'>
		  <tr>
		    <td>Number of Rolls: </td>
		    <td><input name='numrolls' type='text' size='3' maxlength='3'/></td>
		  </tr>
		  <tr>
		    <td>Number of Dice per Roll: </td>
		    <td><input name='numdice' type='text' size='3' maxlength='3'/></td>
		  </tr>
		  <tr>
		    <td>Number of Sides per Dice: </td>
		    <td align='right'><select name='numsides'>";
		      foreach($commonsides as $val){
		      	echo "<option value=$val>$val</option>";
		      }
		      echo "</select>
		        Custom Number: <br><input name='numsides_custom' type='text' size='3' maxlength='3'/></td>
		  </tr>
		  <tr>
		  	<td>
		  		Sort by:
		  	</td>
		  	<td>
		    	<select name='sortby'>
		    		<option value='roll'>Sort by Roll #</option>
		    		<option value='die'>Sort by Dice #</option>
		    	</select>
		   </td>
		  </tr>
		  <tr>
		    <td colspan='2' align='center'><input type='submit' name='Submit' value='Submit' />
		        <input type='reset' name='reset' value='Reset' /></td>
		  </tr>
		</table></form>";

		}

		function reRollForm(){
			echo "<form method='post' action='diceroller.php'>
				<input type='hidden' name='numrolls' value='".$_POST['numrolls']."'>
				<input type='hidden' name='numdice' value='".$_POST['numdice']."'>
				<input type='hidden' name='numsides' value='".$_POST['numsides']."'>
				<input type='hidden' name='numsides_custom' value='".$_POST['numsides_custom']."'>
				<input type='hidden' name='sortby' value='".$_POST['sortby']."'>
				<input type='submit' name='Submit' value='Re-Roll'>
				<a href='diceroller.php'>Change Roll Details</a><br><br>
				</form>";
		}
	}
}
$roller = new dice_roller;
if(isset($_POST['Submit'])){
	$roller->reRollForm();
	$roller->rolldice($_POST['numrolls'],$_POST['numdice'],$_POST['numsides'],$_POST['numsides_custom']);
	$roller->displayRollData($_POST['sortby'],"10");
} else {
	$roller->displayRollForm();
}
By default, it displays up to 10 rolls/dice per row but you can change that to whatever you want. Just change the "10" in this line to whatever you want: $roller->displayRollData($_POST['sortby'],"10");

Enjoy!

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 0 guests