Write in PHP a game where 2 heroes fight against each other. Each hero start with 10 HP.
- User
Write in PHP a game where 2 heroes fight against each other. Each hero start with 10 HP.
Assistant
<?php class Hero { private $name; private $hp; public function __construct($name) { $this->name = $name; $this->hp = 10; } public function attack($opponent) { $opponent->hp -= 1; } public function isAlive() { return $this->hp > 0; } public function getName() { return $this->name; } public function getHp() { return $this->hp; } } class Game { private $hero1; private $hero2; public function __construct(Hero $hero1, Hero $hero2) { $this->hero1 = $hero1; $this->hero2 = $hero2; } public function play() { while ($this->hero1->isAlive() && $this->hero2->isAlive()) { $this->hero1->attack($this->hero2); $this->hero2->attack($this->hero1); } if ($this->hero1->isAlive()) { echo $this->hero1->getName() . " wins!"; } else { echo $this->hero2->getName() . " wins!"; } } } $hero1 = new Hero("Hero 1"); $hero2 = new Hero("Hero 2"); $game = new Game($hero1, $hero2); $game->play(); ?>
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.