Advertisement
HristoBaychev

battleofPHP

Apr 13th, 2023 (edited)
860
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.50 KB | None | 0 0
  1. <?php
  2.  
  3. class Player {
  4.     private static $lastId = 0;
  5.     private $id;
  6.  
  7.     private $nickname;
  8.     private $health;
  9.     private $attack;
  10.    
  11.     public function __construct(string $nickname, int $health, int $attack) {
  12.         $this->nickname = $nickname;
  13.         $this->health = $health;
  14.         $this->attack = $attack;
  15.         $this->id = ++self::$lastId;
  16.     }
  17.  
  18.  
  19.  
  20.     public function getAttack(): int {
  21.         return $this->attack;
  22.     }
  23.  
  24.     public function reduceHealth(int $health) {
  25.         $this->health -= $health;
  26.     }
  27.  
  28.     public function isAlive(): bool {
  29.         return $this->health > 0;
  30.     }
  31.  
  32.     public function __toString(): string {
  33.         return $this->nickname;
  34.     }
  35.     public function setName($nickname){
  36.         $this->nickname = $nickname;
  37.     }
  38.     public function setAttack($attack){
  39.         $this->attack = $attack;
  40.     }
  41.     public function setHealth($health){
  42.         $this->health = $health;
  43.     }
  44.  
  45.     public function attack(Player $opponent) {
  46.         if ($opponent === $this) {
  47.             throw new Exception("Cannot attack yourself");
  48.         }
  49.  
  50.         $opponent->reduceHealth($this->getAttack());
  51.         echo "{$this} attack {$opponent}. {$opponent} remain with {$opponent->health} health." . PHP_EOL;
  52.     }
  53. }  
  54.  
  55. $players = [];
  56. $playerNames = [
  57.     "Aleksandar Shishkov",
  58.     "Galina Nikolova",
  59.     "Imran",
  60.     "Ivan Panayotov",
  61.     "Kiril Bardarov",
  62.     "Martin",
  63.     "Pavel Zhelyazkov",
  64.     "Plamen Mitev",
  65.     "Rumen",
  66.     "Rusi Nedelchev",
  67.     "Veselin",
  68.     "Zahari",
  69.     "Borislav",
  70.     "IO"
  71. ];
  72.  
  73. foreach ($playerNames as $names) {
  74.     $player = new Player($names,rand(500,1000),rand(10,30));
  75.     // $player = new Player("traycho", 1, 2);
  76.     // $player->setName($names);
  77.     // $player->setAttack(rand(10,50));
  78.     // $player->setHealth(rand(500,1000));
  79.     array_push($players, $player);
  80.    
  81. }
  82.  
  83. /*
  84. $player1 = new Player("Aleksandar Shishkov", rand(200,500), rand(10,30) );
  85. $player2 = new Player("Galina Nikolova", rand(200,500), rand(10,30));
  86. $player3 = new Player("Imran", rand(200,500), rand(10,30));
  87. $player4 = new Player("Ivan Panayotov", rand(200,500), rand(10,30));
  88. $player5 = new Player("Kiril Bardarov", rand(200,500), rand(10,30));
  89. $player6 = new Player("Martin", rand(200,500), rand(10,30));
  90. $player7 = new Player("Pavel Zhelyazkov", rand(200,500), rand(10,30));
  91. $player8 = new Player("Plamen Mitev", rand(200,500), rand(10,30));
  92. $player9 = new Player("Rumen", rand(200,500),rand(10,30));
  93. $player10 = new Player("Rusi Nedelchev", rand(200,500), rand(10,30));
  94. $player11 = new Player("Veselin", rand(200,500), rand(10,30));
  95. $player12 = new Player("Zahari", rand(200,500), rand(10,30));
  96. $player13 = new Player("Borislav", rand(200,500), rand(10,30));
  97. $player14 = new Player("IO", rand(200,500), rand(10,30));
  98.  
  99. $players = [$player1, $player2, $player3, $player4, $player5, $player6, $player7, $player8, $player9, $player10, $player11, $player12, $player13, $player14];
  100. */
  101.  
  102. shuffle($players);
  103.  
  104. while (count($players) > 1) {
  105.     $attacker = array_shift($players);
  106.     $defenderKey = array_rand($players);
  107.     $defender = $players[$defenderKey];
  108.     if ($defender->isAlive()) {
  109.         $attacker->attack($defender);
  110.         if (!$defender->isAlive()) {
  111.             echo "{$defender} is killed from {$attacker}." . PHP_EOL;
  112.             unset($players[$defenderKey]);
  113.         }
  114.     } else {
  115.         echo "{$defender} is dead from {$attacker}." . PHP_EOL;
  116.     }
  117. }
  118.  
  119. echo "{$players[0]} is a WINNER!!!!" . PHP_EOL;
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement