logoWebbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

webbserv1-dice/exercises/dice6/

CDice.php
CHistogram.php
dice-6.php

CDice.php

78 lines ASCII Unix (LF)
<?php
/**
 * A CDice class to play around with a dice.
 *
 */
class CDice {

  
/**
   * Properties
   *
   */
  
private $rolls = array();
  private 
$faces;

 
/**
  * Contructor
  */
 
public function __construct($facesInput=6) {
   echo 
__METHOD__;
   
$this->faces = $facesInput;
 }
 
 
/**
  * Destructor
  */
 
public function __destruct() {
   echo 
__METHOD__;
 }

  
/**
   * Get the number of faces.
   *
   */
  
public function GetFaces() {
    return 
$this->faces;
  }


  
/**
   * Get the rolls as an array.
   *
   */
  
public function GetRollsAsArray() {
    return 
$this->rolls;
  }
  
  
/**
   * Roll the dice
   *
   */
  
public function Roll($times) {
    
$this->rolls = array();

    for(
$i = 0; $i < $times; $i++) {
      
$this->rolls[$i] = rand(1, $this->faces);
    }
  }
  
  
/**
   * Get the total from the last roll(s).
   *
   */
  
public function GetTotal() {
    return 
array_sum($this->rolls);
  }


  
/**
   * Get the average from the last roll(s).
   *
   */
  
public function GetAverage() {
    return 
round(array_sum($this->rolls) / count($this->rolls), 1);
  }

}
?>