<?php

namespace Rasb14\Dice;

class DiceHand
{
    /**
     * @var array $dices The DiceHands Dices.
     */
    private $dices;

    /**
     * Constructor to create a Dice.
     * 
     * @param int $dices The number of dices of the DiceHand.
     */
    public function __construct(int $dices)
    {
        $this->dices = array();

        for ($i = 0; $i < $dices; $i++) {
            array_push($this->dices, new Dice());
        }
    }

    /**
     * Roll all dices.
     *
     * @return void.
     */
    public function roll()
    {
        for ($i = 0; $i < count($this->dices); $i++) {
            $this->dices[$i]->roll();
        }
    }

    /**
     * Get values of dices from last roll.
     *
     * @return array with values of the last roll.
     */
    public function values()
    {
        $vals = array();
        for ($i = 0; $i < count($this->dices); $i++) {
            array_push($vals, $this->dices[$i]->value());
        }
        return $vals;
    }

    /**
     * Get the sum of all dices.
     *
     * @return int as the sum of all dices.
     */
    public function sum()
    {
        $val = 0;
        for ($i = 0; $i < count($this->dices); $i++) {
            $val += $this->dices[$i]->value();
        }
        return $val;
    }

    /**
     * Get the average of all dices.
     *
     * @return float as the average of all dices.
     */
    public function average()
    {
        return $this->sum() / count($this->dices);
    }
}
