Could you tell me how to make it quality code?
<?php
class Game {
public function __construct() {
session_start();
}
public function play(){
return '<input type="button" value="Play" onclick="window.location = \'?play=1\'">';
}
public function start($msg=null) {
unset($_SESSION['bees']);
$bee = new Bee();
$bee->setType('queen');
$this->addBee($bee,1);
$bee->setType('worker');
$this->addBee($bee,5);
$bee->setType('drone');
$this->addBee($bee,8);
if($msg)
return $msg;
else {
var_dump($_SESSION['bees']); // debug
return 'Game started <input type="button" value="Hit" onclick="window.location = \'?hit=1\'">';
}
}
public function hit() {
$msg = '';
if($_SESSION['bees'])
$rd = array_rand($_SESSION['bees'],1);
else
$this->start('Game will start again');
$_SESSION['bees'][$rd]['lifespan'] = $_SESSION['bees'][$rd]['lifespan'] - $_SESSION['bees'][$rd]['hit'];
if($_SESSION['bees'][$rd]['lifespan']<1) {
$_SESSION['bees'][$rd]['bees'] -=1;
$_SESSION['bees'][$rd]['lifespan'] = $_SESSION['bees'][$rd]['life'];
switch($rd)
{
case 'queen': $msg .= $this->start('Game start again, the Queen is dead.'); break;
default: $msg .= 'One \'' . $rd . '\' is gone';
}
}
if($_SESSION['bees'][$rd]['bees']<1){
unset($_SESSION['bees'][$rd]);
$msg = 'The \'' . $rd . '\' team is gone';
}
var_dump($_SESSION['bees']); // debug
return '<input type="button" value="Hit" onclick="window.location = \'?hit=1\'"> ' . $msg;
}
public function addBee(Bee $newbee, $number = 1) {
$tipo = $newbee->getType();
$bee = $newbee->get();
$_SESSION['bees'][$tipo] = $bee;
$_SESSION['bees'][$tipo]['bees'] = $number;
$_SESSION['bees'][$tipo]['life'] = $newbee->getLifespan($tipo);
}
}
class Bee
{
protected $_type = null;
protected $_types = array(
'queen' => array(
'hit' => 8,
'lifespan' => 100
),
'worker' => array(
'hit' => 10,
'lifespan' => 75
),
'drone' => array(
hit' => 12,
'lifespan' => 50
)
);
public function setHit(){}
public function setLifespan(){}
public function addNewType(){}
protected function _set_hit(){}
protected function _set_lifespan(){}
protected function _add_new_type(){}
public function getLifespan($type){
if(array_key_exists($type,$this->_types))
return $this->_types[$this->_type]['lifespan'];
else
throw new Exception('The Bee need to be a Queen, Worker or Drone');
}
public function getTypes() {
return $this->_types;
}
public function getType() {
return $this->_type;
}
public function setType($type) {
if(array_key_exists($type,$this->_types))
$this->_type = $type;
else
throw new Exception('The Bee need to be a Queen, Worker or Drone');
}
public function get() {
return $this->_types[$this->_type];
}
}
?>
<html>
<head>
<title>play Bee</title>
</head>
<body>
<?php
$game = new Game();
if(isset($_GET['play']) && $_GET['play']) {
echo $game->start();
} else if(isset($_GET['hit']) && $_GET['hit']) {
echo $game->hit();
} else {
echo $game->play();
}
?>
</body>
</html>