Jump to content

OOP / Arrays help please


parino_esquilado

Recommended Posts

I have created two classes. One is called 'move' and the other 'unit'. To make my example easier to follow, I shall use the much loved pokemon series to explain my dilemma.

 

Here are the class declarations.

 

new class unit {
var $name, $moves;

public function newUnit($name, $moves) {
$this->name = $name;
$this->moves = $moves;
}

}

new class move {
var $name, $attack;

public function newMove($name, $attack) {
$this->name = $name;
$this->moves = $moves;
}

}

 

Now I shall instantiate these classes (if that makes sense xD) - I just started OOP yesterday, do mind my ignorance.

 


$unit = array(); 
$move = array();

$unit[0] = new unit; $unit[0]->newUnit("picachu",array("thunderbolt","quick-attack"));

 

Now, let's say I want to populate the $move array with the moves in the unit object (thunderbolt and quick-attack). I can retrieve their attack values from a database:

 


$thunderboltAttack = 200;
$quick-attackAttack = 40;

 

I now want to create a function that will create instances of the 'move' class to produce:

 


$move[0] = new move; $move[0]->newMove("thunderbolt",200);
$move[1] = new move; $move[1]->newMove("quick-attack",40);

 

Here is my dire attempt to do so:

 


function createUnits($moves){  //$moves is the array holding the unit's moves

for($i; $i < count($moves); $i++) {

$arr[] =new move;
$arr[count($units) - 1]->name = $moves[0];
$arr[count($units) - 1]->attack = $someAttackData;

}
return $arr;
}

$moves[] = createUnits;

 

I'm ending up with a multi-dimensional array when I want a linear one like this:

 


$move[0] = new move; $move[0]->newMove("thunderbolt",200);
$move[1] = new move; $move[1]->newMove("quick-attack",40);

 

 

What do I need to do. Do I have to resort to making $move global from within the function and do things that way?

Link to comment
Share on other sites

class Pokemon
{
    protected $type;
    protected $abilities;
    protected $gender;
    protected $weaknesses;
    protected $evolution;
    
    public function __construct(PokemonEvolution $evolution) {
        $this->evolve($evolution);
    }
    
    public function evolve(PokemonEvolution $evolution) {
        $this->evolution = $evolution;
    }
    
    public function damage($points) {
        $this->evolution->damage($points);
    }
    
    public function attack(Pokemon $pokemon) {
        $this->evolution->attack($pokemon);
    }
}

abstract class PokemonEvolution
{
    protected $height;
    protected $width;
    protected $species;
    
    protected $hp;
    protected $attack;
    protected $defense;
    protected $specialAttack;
    protected $specialDefense;
    protected $speed;
    
    public function damage($points) {
        $this->hp -= ($points - ($this->defense + $this->specialDefense) / 2);
    }
    
    public function attack(Pokemon $pokemon) {
        $pokemon->damage($this->attack);
    }
    
    public function specialAttack(Pokemon $pokemon) {
        $pokemon->damage($this->specialAttack);
    }
}

 

$pikachu = new Pokemon(new Pikachu());
$bulbasaur = new Pokemon(new Bulbasaur());
$pikachu->attack($bulbasaur);
$pikachu->evolve(new Raichu());
$pikachu->specialAttack($bulbasaur); // K.O.

 

class PokeBall {
    private $db;
    public function store($pokemon) {/* INSERT INTO .. */}
    public function retrieve($pokemon) {/* SELECT .. */}
}

 

$pokeball = new PokeBall($db);
$pikachu = $pokeball->retrieve('Pikachu');
$bulbasaur = $pokeball->retrieve('Bulbasaur');
$pikachu->attack($bulbasaur);
$pikachu->evolve(new Raichu());
$pikachu->specialAttack($bulbasaur); // K.O.

 

This isn't a finished design, a lot needs fixing but you get the general idea.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.