Created
March 17, 2021 22:25
-
-
Save clarade/3841544198b2d90b2a3f3ea2104ffff9 to your computer and use it in GitHub Desktop.
Exercise about POO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Fighter | |
{ | |
const MAX_LIFE = 100; | |
private $name; | |
private $life = self :: MAX_LIFE; | |
private $strenght; | |
private $dexterity; | |
public function __construct(string $name, int $strenght, int $dexterity) | |
{ | |
$this->name = $name; | |
$this->strenght = $strenght; | |
$this->dexterity = $dexterity; | |
} | |
public function setName(string $name) : Fighter | |
{ | |
$this->name = $name; | |
return $this; | |
} | |
public function getName() : string | |
{ | |
return $this->name; | |
} | |
public function setDexterity($dexterity) | |
{ | |
$this->dexterity = $dexterity; | |
return $this; | |
} | |
public function getDexterity() | |
{ | |
return $this->dexterity; | |
} | |
public function getStrenght() | |
{ | |
return $this->strenght; | |
} | |
public function setStrenght($strenght) | |
{ | |
$this->strenght = $strenght; | |
return $this; | |
} | |
/** | |
* Get the value of life | |
*/ | |
public function getLife() | |
{ | |
return $this->life; | |
} | |
/** | |
* Set the value of life | |
* | |
* @return self | |
*/ | |
public function setLife($life) | |
{ | |
/* if ($life >= 0) { | |
$this->life = $life; | |
} else { | |
$life = 0; | |
} */ | |
$this->life = $life >= 0 ? $life : 0; | |
return $this; | |
} | |
public function getDefense() : int | |
{ | |
return $this->getDexterity(); | |
} | |
public function getDamage() : int | |
{ | |
$damage = rand(1, $this->getStrenght()); | |
return $damage; | |
} | |
// soustraire aux dégats le score de defense de l'attaqué | |
public function fight(Fighter $targetFighter) | |
{ | |
$damages = $this->getDamage(); | |
$damages -= $targetFighter->getDefense(); | |
$damages = $damages < 0 ? 0 : $damages; | |
$targetFighter->setLife($targetFighter->getLife() - $damages); | |
} | |
// diminuer le nb de pts de vie l'attaqué par la valeur ainsi obtenue | |
public function isDead() : bool | |
{ | |
return $this->getLife() <= 0 ? true : false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once '../src/Fighter.php'; | |
$fighter1 = new Fighter('Héraclès', 20, 6); | |
$fighter2= new Fighter('Lion de Némée', 11, 13); | |
/* echo $fighter1->getName() . " a " . $fighter1->getLife() . '</br>' ; | |
echo $fighter2->getName() . " a " . $fighter2->getLife() . '</br>' ; */ | |
// $fighter2->fight($fighter1); | |
// echo $fighter1->getName() . " a " . $fighter1->getLife() . '</br>' ; | |
// echo $fighter2->getName() . " a " . $fighter2->getLife(); | |
$counter = 1; | |
// while ($fighter1->getLife() > 0 && $fighter2->getLife() > 0) | |
while (!$fighter1->isDead() && !$fighter2->isDead()) { | |
echo "round no $counter </br>" ; | |
echo $fighter1->getName() . " attaque " . $fighter2->getName() . "</br>"; | |
$fighter1->fight($fighter2); | |
if ($fighter2->isDead()) { | |
echo $fighter1->getName() . " a vaincu " . $fighter2->getName() . "</br>"; | |
} else { | |
echo $fighter2->getName() . " attaque " . $fighter1->getName() . "</br>"; | |
$fighter2->fight($fighter1); | |
if ($fighter1->isDead()) { | |
echo $fighter2->getName() . " a vaincu " . $fighter1->getName() . "</br>"; | |
} | |
} | |
echo " Il reste " . $fighter1->getLife() . " à " . $fighter1->getName() . "</br>"; | |
echo " Il reste " . $fighter2->getLife() . " à " . $fighter2->getName() . "</br>"; | |
echo "</br>"; | |
$counter ++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment