<?php
header("Content-Type: text/plain; charset=utf-8"); class Game
{
public $hero;
public $monsters;
public $gold=0;
public function __construct($monsters, $items)
{
$this->hero = new Hero($items);
$this->monsters = $monsters;
}
public function getKillMonster()
{
$monsters = $this->monsters; //копия переменной монстров
for ($i = 1; $i <= $this->monsters; $i++)
{
$items = $this->hero->items;
$typeOfNewItem = mt_rand(1, count($items)); //генерация номера слота выпавшего предмета $oldItem = $this->hero->getOldItem($typeOfNewItem); //уровень старого предмета
$newItem = mt_rand (1, ($oldItem +1)); //генерация илвл нового предмета if ($newItem >= $oldItem)
{
$this->hero->getChangeItem($typeOfNewItem, $newItem); //замена старого предмета новым
$this->gold += $oldItem; //продажа старого предмета
}
$monsters--;
}
$this->monsters = $monsters; // количество монстров оставшихся в живых
}
}
class Hero
{
public function __construct($k)
{
for ($i = 1; $i <= $k; $i++)
{
$item = new Item($i);
$items[] = $item;
}
$this->items = $items;
}
public function getOldItem($numberItem)//взять илвл старого предмета данного слота
{
foreach ($this->items as $item) // перебор, чтобы добраться до нужного предмета
{
if ($item->slot == $numberItem) //проверка на подходящий слот
{
return $item->itemLvl; //возвращение уровня предмета
}
}
}
public function getChangeItem($numberItem, $itemLvl) //замена старого предмета новым
{
foreach ($this->items as $item) // перебор, чтобы добраться до нужного предмета
{
if ($item->slot == $numberItem) //проверка на подходящий слот
{
$item->itemLvl = $itemLvl; //присвоение нового уровня предмета
}
}
}
}
class Item
{
public $slot;
public $itemLvl=1;
public function __construct($slot)
{
return $this->slot = $slot;
}
}
function getGame($n, $k)
{
$game = new Game ($n, $k);
return $game;
}
$game = getGame(5, 2);
$game->getKillMonster();
PD9waHAKaGVhZGVyKCJDb250ZW50LVR5cGU6IHRleHQvcGxhaW47IGNoYXJzZXQ9dXRmLTgiKTsKbWJfaW50ZXJuYWxfZW5jb2RpbmcoIlVURi04Iik7CmNsYXNzIEdhbWUgCnsKCXB1YmxpYyAkaGVybzsKCXB1YmxpYyAkbW9uc3RlcnM7CglwdWJsaWMgJGdv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