<?php
header("Content-Type: text/plain; charset=utf-8"); class Hero
{
public $health;
public $armorLow;
public $armorHigh;
public $block;
public $dodgeCrit;
public $crit;
public $attackLow;
public $attackHigh;
public $charisma;
public $agility;
public $skill;
public function __construct($attackLow, $attackHigh, $health, $armorLow, $armorHigh, $block, $dodgeCrit, $crit, $charisma, $agility, $skill)
{
$this->attackLow = $attackLow;
$this->attackHigh = $attackHigh;
$this->health = $health;
$this->armorLow = $armorLow;
$this->armorHigh = $armorHigh;
$this->block = $block;
$this->dodgeCrit = $dodgeCrit;
$this->crit = $crit;
$this->charisma = $charisma;
$this->agility = $agility;
$this->skill = $skill;
}
public function __destruct()
{
}
public function isChance($attribute) //общая функция для крита и блока
{
if ((mt_rand(1, 100)) < $attribute) { return 1;
}
return 0;
}
public function getDmg()
{
return mt_rand($this->attackLow, $this->attackHigh); }
public function getDef()
{
return mt_rand($this->armorLow, $this->armorHigh); }
}
class Game
{
public $heroes = array(); public $cloned = array();
public function __construct($hero1, $hero2)
{
$this->heroes[] = $hero1;
$this->heroes[] = $hero2;
$this->cloned[] = clone $hero1;
$this->cloned[] = clone $hero2;
}
public function getHit($k) // $k = 0 либо 1 (номер игрока который наносит урон)
{
foreach ($this->heroes as $number => $hero) {
if ($number == $k) {
$skill = $hero->skill;
$dmg = $hero->getDmg();
$crit = 1; // коефициент крита умножаемый на основной урон в случае отстутвия крита
if ($hero->isChance($hero->crit)) {
$crit = 2; // -||- в случае крита
}
} else {
$def = $hero->getDef();
$block = 1; // коефициент блока умножаемый на основной урон (в случае отсутствие блока)
$avoidAttackStat = $hero->skill + $hero->agility;
$avoidCrit = $hero->dodgeCrit;
if ($hero->isChance($hero->block)) {
$block = 0; // -||- в случае успешного блока
}
}
}
$hitChance = round($skill * 100 / $avoidAttackStat); $dodge = 0;
if ((mt_rand(1, 100)) < $hitChance) { $dodge = 1; // коефициент промаха (уменние / (умение и ловкость врага)) умножается на основной урон
}
if ($crit == 2 && (mt_rand(1, 100)) < $avoidCrit) // проверка на избежание крита {
$crit = 1;
}
$dmg = ($dmg - $def) * $crit * $block * $dodge;
return $dmg;
}
public function isDoubleHit($k) // $k - номер героя (0 либо 1)
{
foreach ($this->heroes as $number => $hero) {
if ($number == $k) {
$charMain = $hero->charisma;
} else {
$charEnemy = $hero->charisma;
}
}
if ($charMain < $charEnemy || $charMain == $charEnemy) {
return 0; // вернет 0 в случае если у нашего героя меньше либо столько же харизмы как у врага
}
$coef = $charMain - $charEnemy;
return 1; // двойная атака
} else {
return 0;
}
}
public function isAlive($k, $dmg) //номер героя (0,1)
{
foreach ($this->heroes as $number => $hero) {
if ($number != $k) {
if ($hero->health <= $dmg) {
$hero->health = 1;
return;
}
$hero->health -= $dmg;
}
}
}
public function getRound()
{
for ($i = 0; $i <= 1; $i++) {
if ($this->isDoubleHit($i)) {
$dmg = $this->getHit($i) + $this->getHit($i);
} else {
$dmg = $this->getHit($i);
}
$k = $this->isAlive($i, $dmg);
}
}
public function getGame()
{
for ($i = 1; $i <= 20; $i++) {
foreach ($this->heroes as $number => $hero) {
$x = $number;
if ($hero->health == 1) {
if ($x == $number) {
$x++;
}
return $x;
}
}
$this->getRound();
}
foreach ($this->heroes as $number => $hero) {
$health[$number] = $this->cloned[$number]->health - $hero->health;
}
list($healthOne, $healthTwo) = $health; $clone1 = clone ($this->cloned[0]);
$clone2 = clone ($this->cloned[1]);
$this->heroes[] = $clone1;
$this->heroes[] = $clone2;
if ($healthOne < $healthTwo) {
return 0; //победа первого
} elseif ($healthOne > $healthTwo) {
return 1; //победа второго
} elseif ($healthOne == $healthTwo) {
return 2; //ничья
}
}
}
/* временная заглушка
if($_POST['submit'])
{
if ($_POST['dmg1'] > $_POST['dmg2'] || $_POST['armorLow'] > $_POST['armorHigh'] || $_POST['dmg21'] > $_POST['dmg22'] || $_POST['armorLow1'] > $_POST['armorHigh2'])
{
echo "<HTML><HEAD>
<META HTTP-EQUIV='Refresh' CONTENT='3; URL=http://localhost/php2/lessons/index.html'>
</HEAD></HTML>";
echo "Проверьте правильность заполненных данных";
} else {
$attackLow1 = (int)$_POST['dmg1'];
$attackHigh1 = (int)$_POST['dmg2'];
$hp1 = (int)$_POST['HP'];
$armorLow1 = (int)$_POST['armorLow'];
$armorHigh1 = (int)$_POST['armorHigh'];
$block1 = (int)$_POST['block'];
$dodgeCrit1 = (int)$_POST['critDodge'];
$crit1 = (int)$_POST['crit'];
$charisma1 = (int)$_POST['charisma'];
$agility1 = (int)$_POST['agility'];
$skill1 = (int)$_POST['skill'];
$attackLow2 = (int)$_POST['dmg21'];
$attackHigh2 = (int)$_POST['dmg22'];
$hp2 = (int)$_POST['HP2'];
$armorLow2 = (int)$_POST['armorLow2'];
$armorHigh2 = (int)$_POST['armorHigh2'];
$block2 = (int)$_POST['block2'];
$dodgeCrit2 = (int)$_POST['critDodge2'];
$crit2 = (int)$_POST['crit2'];
$charisma2 = (int)$_POST['charisma2'];
$agility2 = (int)$_POST['agility2'];
$skill2 = (int)$_POST['skill2'];
$firstHero = new Hero ($attackLow1, $attackHigh1, $hp1, $armorLow1, $armorHigh1, $block1, $dodgeCrit1, $crit1, $charisma1, $agility1, $skill1);
$secondHero = new Hero ($attackLow2, $attackHigh2, $hp2, $armorLow2, $armorHigh2, $block2, $dodgeCrit2, $crit2, $charisma2, $agility2, $skill2);
$together = array ($firstHero, $secondHero);
$game = new Game ($together);
}
} */
$hero1 = new Hero(504, 563, 19520, 225, 276, 39, 25, 51, 1276, 890, 1182);
$hero2 = new Hero(350, 397, 14275, 25, 27, 50, 25, 41, 775, 774, 928);
$game = new Game($hero1, $hero2);
$k = 10;
$first = 0;
$second = 0;
$draw = 0;
for ($i = 1; $i <= $k; $i++) {
$win = $game->getGame();
if ($win == 0) {
$first++;
} elseif ($win == 1) {
$second++;
} elseif ($win == 2) {
$draw++;
}
}
echo $first;
echo "\n";
echo $second;
echo "\n";
echo $draw;
echo "\n";
$first = round((($first * 100) / $k), 2); $second = round((($second * 100) / $k), 2); $draw = round((($draw * 100) / $k), 2); echo $first;
echo "\n";
echo $second;
echo "\n";
echo $draw;
echo "\n";