fork download
  1. <?php
  2. header("Content-Type: text/plain; charset=utf-8");
  3. class Hero
  4. {
  5. public $health;
  6. public $armorLow;
  7. public $armorHigh;
  8. public $block;
  9. public $dodgeCrit;
  10. public $crit;
  11. public $attackLow;
  12. public $attackHigh;
  13. public $charisma;
  14. public $agility;
  15. public $skill;
  16. public function __construct($attackLow, $attackHigh, $health, $armorLow, $armorHigh, $block, $dodgeCrit, $crit, $charisma, $agility, $skill)
  17. {
  18. $this->attackLow = $attackLow;
  19. $this->attackHigh = $attackHigh;
  20. $this->health = $health;
  21. $this->armorLow = $armorLow;
  22. $this->armorHigh = $armorHigh;
  23. $this->block = $block;
  24. $this->dodgeCrit = $dodgeCrit;
  25. $this->crit = $crit;
  26. $this->charisma = $charisma;
  27. $this->agility = $agility;
  28. $this->skill = $skill;
  29. }
  30. public function __destruct()
  31. {
  32.  
  33. }
  34.  
  35. public function isChance($attribute) //общая функция для крита и блока
  36. {
  37. if ((mt_rand(1, 100)) < $attribute) {
  38. return 1;
  39. }
  40. return 0;
  41. }
  42. public function getDmg()
  43. {
  44. return mt_rand($this->attackLow, $this->attackHigh);
  45. }
  46. public function getDef()
  47. {
  48. return mt_rand($this->armorLow, $this->armorHigh);
  49. }
  50.  
  51. }
  52. class Game
  53. {
  54. public $heroes = array();
  55. public $cloned = array();
  56.  
  57. public function __construct($hero1, $hero2)
  58. {
  59. $this->heroes[] = $hero1;
  60. $this->heroes[] = $hero2;
  61. $this->cloned[] = clone $hero1;
  62. $this->cloned[] = clone $hero2;
  63. }
  64. public function getHit($k) // $k = 0 либо 1 (номер игрока который наносит урон)
  65. {
  66. foreach ($this->heroes as $number => $hero) {
  67. if ($number == $k) {
  68. $skill = $hero->skill;
  69. $dmg = $hero->getDmg();
  70. $crit = 1; // коефициент крита умножаемый на основной урон в случае отстутвия крита
  71. if ($hero->isChance($hero->crit)) {
  72. $crit = 2; // -||- в случае крита
  73. }
  74. } else {
  75. $def = $hero->getDef();
  76. $block = 1; // коефициент блока умножаемый на основной урон (в случае отсутствие блока)
  77. $avoidAttackStat = $hero->skill + $hero->agility;
  78. $avoidCrit = $hero->dodgeCrit;
  79. if ($hero->isChance($hero->block)) {
  80. $block = 0; // -||- в случае успешного блока
  81. }
  82. }
  83. }
  84. $hitChance = round($skill * 100 / $avoidAttackStat);
  85. $dodge = 0;
  86. if ((mt_rand(1, 100)) < $hitChance) {
  87. $dodge = 1; // коефициент промаха (уменние / (умение и ловкость врага)) умножается на основной урон
  88. }
  89. if ($crit == 2 && (mt_rand(1, 100)) < $avoidCrit) // проверка на избежание крита
  90. {
  91. $crit = 1;
  92.  
  93. }
  94. $dmg = ($dmg - $def) * $crit * $block * $dodge;
  95. return $dmg;
  96. }
  97. public function isDoubleHit($k) // $k - номер героя (0 либо 1)
  98. {
  99. foreach ($this->heroes as $number => $hero) {
  100. if ($number == $k) {
  101. $charMain = $hero->charisma;
  102. } else {
  103. $charEnemy = $hero->charisma;
  104. }
  105.  
  106. }
  107. if ($charMain < $charEnemy || $charMain == $charEnemy) {
  108. return 0; // вернет 0 в случае если у нашего героя меньше либо столько же харизмы как у врага
  109. }
  110. $coef = $charMain - $charEnemy;
  111. if ((mt_rand(1, 100)) < $coef) {
  112. return 1; // двойная атака
  113. } else {
  114. return 0;
  115. }
  116.  
  117. }
  118. public function isAlive($k, $dmg) //номер героя (0,1)
  119. {
  120. foreach ($this->heroes as $number => $hero) {
  121. if ($number != $k) {
  122. if ($hero->health <= $dmg) {
  123. $hero->health = 1;
  124. return;
  125. }
  126. $hero->health -= $dmg;
  127. }
  128.  
  129. }
  130.  
  131. }
  132. public function getRound()
  133. {
  134. for ($i = 0; $i <= 1; $i++) {
  135. if ($this->isDoubleHit($i)) {
  136. $dmg = $this->getHit($i) + $this->getHit($i);
  137. } else {
  138. $dmg = $this->getHit($i);
  139. }
  140. $k = $this->isAlive($i, $dmg);
  141. }
  142. }
  143. public function getGame()
  144. {
  145. for ($i = 1; $i <= 20; $i++) {
  146. foreach ($this->heroes as $number => $hero) {
  147. $x = $number;
  148. if ($hero->health == 1) {
  149. if ($x == $number) {
  150. $x++;
  151. }
  152. return $x;
  153. }
  154. }
  155. $this->getRound();
  156. }
  157. $health = array();
  158. foreach ($this->heroes as $number => $hero) {
  159. $health[$number] = $this->cloned[$number]->health - $hero->health;
  160. }
  161. list($healthOne, $healthTwo) = $health;
  162. $clone1 = clone ($this->cloned[0]);
  163. $clone2 = clone ($this->cloned[1]);
  164. unset($this->heroes);
  165. $this->heroes[] = $clone1;
  166. $this->heroes[] = $clone2;
  167. if ($healthOne < $healthTwo) {
  168. return 0; //победа первого
  169. } elseif ($healthOne > $healthTwo) {
  170. return 1; //победа второго
  171. } elseif ($healthOne == $healthTwo) {
  172. return 2; //ничья
  173. }
  174. }
  175.  
  176. }
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201. /* временная заглушка
  202. if($_POST['submit'])
  203. {
  204. if ($_POST['dmg1'] > $_POST['dmg2'] || $_POST['armorLow'] > $_POST['armorHigh'] || $_POST['dmg21'] > $_POST['dmg22'] || $_POST['armorLow1'] > $_POST['armorHigh2'])
  205. {
  206. echo "<HTML><HEAD>
  207. <META HTTP-EQUIV='Refresh' CONTENT='3; URL=http://localhost/php2/lessons/index.html'>
  208. </HEAD></HTML>";
  209. echo "Проверьте правильность заполненных данных";
  210. } else {
  211. $attackLow1 = (int)$_POST['dmg1'];
  212. $attackHigh1 = (int)$_POST['dmg2'];
  213. $hp1 = (int)$_POST['HP'];
  214. $armorLow1 = (int)$_POST['armorLow'];
  215. $armorHigh1 = (int)$_POST['armorHigh'];
  216. $block1 = (int)$_POST['block'];
  217. $dodgeCrit1 = (int)$_POST['critDodge'];
  218. $crit1 = (int)$_POST['crit'];
  219. $charisma1 = (int)$_POST['charisma'];
  220. $agility1 = (int)$_POST['agility'];
  221. $skill1 = (int)$_POST['skill'];
  222.  
  223. $attackLow2 = (int)$_POST['dmg21'];
  224. $attackHigh2 = (int)$_POST['dmg22'];
  225. $hp2 = (int)$_POST['HP2'];
  226. $armorLow2 = (int)$_POST['armorLow2'];
  227. $armorHigh2 = (int)$_POST['armorHigh2'];
  228. $block2 = (int)$_POST['block2'];
  229. $dodgeCrit2 = (int)$_POST['critDodge2'];
  230. $crit2 = (int)$_POST['crit2'];
  231. $charisma2 = (int)$_POST['charisma2'];
  232. $agility2 = (int)$_POST['agility2'];
  233. $skill2 = (int)$_POST['skill2'];
  234. $firstHero = new Hero ($attackLow1, $attackHigh1, $hp1, $armorLow1, $armorHigh1, $block1, $dodgeCrit1, $crit1, $charisma1, $agility1, $skill1);
  235. $secondHero = new Hero ($attackLow2, $attackHigh2, $hp2, $armorLow2, $armorHigh2, $block2, $dodgeCrit2, $crit2, $charisma2, $agility2, $skill2);
  236. $together = array ($firstHero, $secondHero);
  237. $game = new Game ($together);
  238. }
  239. } */
  240. $hero1 = new Hero(504, 563, 19520, 225, 276, 39, 25, 51, 1276, 890, 1182);
  241. $hero2 = new Hero(350, 397, 14275, 25, 27, 50, 25, 41, 775, 774, 928);
  242. $game = new Game($hero1, $hero2);
  243. $k = 10;
  244. $first = 0;
  245. $second = 0;
  246. $draw = 0;
  247.  
  248. for ($i = 1; $i <= $k; $i++) {
  249. $win = $game->getGame();
  250. if ($win == 0) {
  251. $first++;
  252. } elseif ($win == 1) {
  253. $second++;
  254. } elseif ($win == 2) {
  255. $draw++;
  256. }
  257. }
  258. echo $first;
  259. echo "\n";
  260. echo $second;
  261. echo "\n";
  262. echo $draw;
  263. echo "\n";
  264. $first = round((($first * 100) / $k), 2);
  265. $second = round((($second * 100) / $k), 2);
  266. $draw = round((($draw * 100) / $k), 2);
  267. echo $first;
  268. echo "\n";
  269. echo $second;
  270. echo "\n";
  271. echo $draw;
  272. echo "\n";
  273. print_r($game);
Success #stdin #stdout 0.02s 24448KB
stdin
Standard input is empty
stdout
10
0
0
100
0
0
Game Object
(
    [heroes] => Array
        (
            [0] => Hero Object
                (
                    [health] => 19520
                    [armorLow] => 225
                    [armorHigh] => 276
                    [block] => 39
                    [dodgeCrit] => 25
                    [crit] => 51
                    [attackLow] => 504
                    [attackHigh] => 563
                    [charisma] => 1276
                    [agility] => 890
                    [skill] => 1182
                )

            [1] => Hero Object
                (
                    [health] => 14275
                    [armorLow] => 25
                    [armorHigh] => 27
                    [block] => 50
                    [dodgeCrit] => 25
                    [crit] => 41
                    [attackLow] => 350
                    [attackHigh] => 397
                    [charisma] => 775
                    [agility] => 774
                    [skill] => 928
                )

        )

    [cloned] => Array
        (
            [0] => Hero Object
                (
                    [health] => 19520
                    [armorLow] => 225
                    [armorHigh] => 276
                    [block] => 39
                    [dodgeCrit] => 25
                    [crit] => 51
                    [attackLow] => 504
                    [attackHigh] => 563
                    [charisma] => 1276
                    [agility] => 890
                    [skill] => 1182
                )

            [1] => Hero Object
                (
                    [health] => 14275
                    [armorLow] => 25
                    [armorHigh] => 27
                    [block] => 50
                    [dodgeCrit] => 25
                    [crit] => 41
                    [attackLow] => 350
                    [attackHigh] => 397
                    [charisma] => 775
                    [agility] => 774
                    [skill] => 928
                )

        )

)