There are two players. Everyone chooses "attack" and "defense", it can be:
Head - 1
Stomach - 2
Hands - 3
Feet - 4
Numbers - ID.
The winner is determined in the case when the player hit the "unprotected" area of ​​the enemy and correctly defended his own.
Those. in each game there are actions of player 1 (for example, 1.1 - attack of the head and defense of the head too) and actions of player 2 (for example, 2.2 - attack of the stomach and defense of the belly too), respectively. in this case, the result is a draw. Help write the algorithm for determining the result of a game on php as briefly as possible.
Sincerely and thank you in advance!

  • What does "game result" mean? Damage done? Probably, it depends on any characteristics, like evasion chances, crit, weapons, armor (on the corresponding parts of the body), buffs, etc.? - vp_arth
  • not. The result of the game is victory, defeat, or a draw. Determined based on the choice of attack and defense zones by two players. The winner is the one who chose the “unprotected” area of ​​the enemy as an attack and defended the one where the enemy beats. - Arthur
  • For example, if the first player chooses a blow to the stomach and hand protection, and the second - a blow to the hands and head protection, then the first player wins, as he protected his hands from the enemy’s blow and hit the stomach, the unprotected area of ​​the enemy. - Arthur
  • What, in this case, you do not like the answer below? - vp_arth
  • one
    @Arthur so take and check, it was written as a universal solution, i.e. for any values) - Manitikyl

2 answers 2

$PLAYER_1 = [1,1]; $PLAYER_2 = [2,2]; $TYPE = 0; if ($PLAYER_1[1] == $PLAYER_2[0]) { $TYPE += 1; } if ($PLAYER_2[1] == $PLAYER_1[0]) { $TYPE += 2; } switch ($TYPE) { case '1': var_dump('Победил игрок 1'); break; case '2': var_dump('Победил игрок 2'); break; case '0': case '3': var_dump('Ничья'); break; default: break; } 
  • These values ​​are taken as an example, they are constantly changing - Arthur
  • Ie there can be 1.4 and 2.2, 2.3 and 4.1 and so on - Arthur
  • @Arthur so what's the problem? first 2 lines. - Manitikyl
  • 2
    @Peresada Please do not teach people to give answers. You are not in the subject, if you are not satisfied with my answer - give it better. Absolutely not see anything bad here. My task is to give a solution, I gave it. For other issues - another topic. Am I available? What you do not like? What I did not create a class, and did not put the code there? The author himself can not do this? Or maybe according to yours, I generally had to raise the hosting, run the whole thing on the frame and throw the source code here, then justify why I chose this particular frame and provide a comparative table, + attach the results of unit tests? - Manitikyl
  • So always draw returns - Arthur

The same as in the @Mantikyl answer, but we consider it a victory to hit the unprotected area

 class Turn { const IN_HEAD = 1; const IN_BODY = 2; const IN_BELL = 3; const IN_FOOT = 4; public static $targets = [self::IN_HEAD, self::IN_BODY, self::IN_BELL, self::IN_FOOT]; public $attack; public $defence; public function __construct($attack, $defence) { $this->attack = $attack; $this->defence = $defence; } } function fight($turn1, $turn2) { $res = 0; if ($turn2->defence !== $turn1->attack) $res = $res | 1; // Игрок 1 попал в незащищённую область if ($turn1->defence !== $turn2->attack) $res = $res | 2; // Игрок 2 попал в незащищённую область switch (true) { case ($res & 1) && ($res & 2): case $res === 0: default: return 'Ничья'; case $res & 1: return 'Победа первого игрока'; case $res & 2: return 'Победа второго игрока'; } } $possibleTurns = []; foreach (Turn::$targets as $a) { foreach (Turn::$targets as $d) { $possibleTurns[] = new Turn($a, $d); } } foreach ($possibleTurns as $t1) { foreach ($possibleTurns as $t2) { $res = fight($t1, $t2); echo "P1({$t1->attack}) VS P2({$t2->defence})", PHP_EOL; echo "P2({$t2->attack}) VS P1({$t1->defence})", PHP_EOL; echo "Result: {$res}", PHP_EOL, '---', PHP_EOL; } }