lunes, 9 de febrero de 2009

Clase y números aleatorios en PHP

Digamos que queremos crear una clase en php para un juego de rol y pretendemos que sus abilidades se generen automáticamente, pudiendo valer de 8 a 18.

class cBeing{

var $IdBeing;
var $Nombre;
var $Apellidos;
var $Str;
var $Con;
var $Int;
var $Wis;
var $Cha;

function cBeing(){

//Constructor por defecto de Being.

$this->Str=12;
$this->Con=12;
$this->Int=12;
$this->Wis=12;
$this->Cha=12;



}

function rollAbilities(){

//Función que genera valores aleatorios para las cuatro abilidades principales.

$this->Str=devuelveAleatorio18();
$this->Con=devuelveAleatorio18();
$this->Int=devuelveAleatorio18();
$this->Wis=devuelveAleatorio18();
$this->Cha=devuelveAleatorio18();


}

function devuelveAleatorio18(){



$random = mt_rand(8,18);

return $random;
}

}

No hay comentarios: