function battle_wrapper(&$pa, &$pd, $active) { if (eval(__MAGIC__)) { return $___RET_VALUE; } battle_prepare($pa, $pd, $active); battle($pa, $pd, $active); battle_finish($pa, $pd, $active); //写回数据库 eval(import_module('sys', 'logger', 'player', 'metman')); if ($active) { if ($pd['hp'] <= 0 && $pa['hp'] > 0) { $pa['action'] = 'corpse' . $pd['pid']; } if ($pa['hp'] <= 0 && $pd['hp'] > 0 && $pd['action'] == '' && $pd['type'] == 0) { $pd['action'] = 'pacorpse' . $pa['pid']; } } else { if ($pd['hp'] <= 0 && $pa['hp'] > 0 && $pa['action'] == '' && $pa['type'] == 0) { $pa['action'] = 'pacorpse' . $pd['pid']; } if ($pa['hp'] <= 0 && $pd['hp'] > 0) { $pd['action'] = 'corpse' . $pa['pid']; } } if ($active) { $edata = $pd; \player\player_save($pa); \player\player_save($pd); \metman\metman_load_playerdata($pd); if ($pd['type'] == 0) { save_enemy_battlelog($pd); } \player\load_playerdata($pa); } else { $edata = $pa; \player\player_save($pa); \player\player_save($pd); \metman\metman_load_playerdata($pa); if ($pa['type'] == 0) { save_enemy_battlelog($pa); } \player\load_playerdata($pd); } $battle_title = '战斗发生'; $main = MOD_METMAN_MEETMAN; \metman\init_battle(1); if (substr($action, 0, 6) == 'corpse') { \corpse\findcorpse($edata); } else { include template(MOD_BATTLE_BATTLERESULT); $cmd = ob_get_contents(); ob_clean(); $action = ''; } if (defined('MOD_CLUBBASE')) { include template(MOD_CLUBBASE_NPCSKILLPAGE); } }
function battle($playerStats, $bossStats) { $playerHit = $playerStats[1] - $bossStats[2]; $bossHit = $bossStats[1] - $playerStats[2]; if ($playerHit < 1) { $playerHit = 1; } if ($bossHit < 1) { $bossHit = 1; } $bossNewHP = $bossStats[0] - $playerHit; if ($bossNewHP <= 0) { return true; } else { $playerNewHP = $playerStats[0] - $bossHit; if ($playerNewHP <= 0) { return false; } else { $playerNewStats = [$playerNewHP, $playerStats[1], $playerStats[2]]; $bossNewStats = [$bossNewHP, $bossStats[1], $bossStats[2]]; return battle($playerNewStats, $bossNewStats); } } }
function choixOrdi() { $tableau = array("pierre", "feuille", "ciseaux"); $choixRandom = $tableau[array_rand($tableau)]; echo $choixRandom . "\n"; return $choixRandom; } function choixUtil() { $choixUtil = $_POST["choix"]; echo $choixUtil . "\n"; return $choixUtil; } function battle() { $tableau = array(0 => "pierre", 1 => "feuille", 2 => "ciseaux"); $choixRandom = choixOrdi(); $choixUtil = choixUtil(); if ($choixRandom == $choixUtil) { echo "Egalite"; } elseif ($choixRandom == $tableau[0] && $choixUtil == $tableau[1] || $choixRandom == $tableau[1] && $choixUtil == $tableau[2] || $choixRandom == $tableau[2] && $choixUtil == $tableau[0]) { echo "Gagne"; } else { echo "Perdu"; } } battle(); ?> </body> </html>
$cost += $ring1Stats['cost']; foreach ($rings as $ring2Name => $ring2Stats) { $cost += $ring2Stats['cost']; if ($ring2Name != $ring1Name) { $user = array('hp' => $myHp, 'dmg' => $weaponStats['dmg'], 'arm' => $armorStats['arm']); if ($ring1Stats['dmg'] > 0) { $user['dmg'] += $ring1Stats['dmg']; } else { $user['arm'] += $ring1Stats['arm']; } if ($ring2Stats['dmg'] > 0) { $user['dmg'] += $ring2Stats['dmg']; } else { $user['arm'] += $ring2Stats['arm']; } if (battle($user, $enemy)) { $costArr[] = $cost; } } $cost -= $ring2Stats['cost']; // refund cost of ring2 } $cost -= $ring1Stats['cost']; // refund cost of ring1 } $cost -= $armorStats['cost']; // refund cost of armor } $cost -= $weaponStats['cost']; // refund cost of weapon }
<?php $input = file_get_contents('day22.txt'); preg_match_all('~(\\d+)~', $input, $matches); $boss = array_combine(['hp', 'damage'], $matches[0]); $player = ['hp' => 50, 'mana' => 500, 'armor' => 0, 'spent' => 0]; $spells = ['Magic Missile' => ['type' => 'instant', 'cost' => 53, 'damage' => 4, 'heal' => 0, 'turns' => 1, 'armor' => 0, 'mana' => 0], 'Drain' => ['type' => 'instant', 'cost' => 73, 'damage' => 2, 'heal' => 2, 'turns' => 1, 'armor' => 0, 'mana' => 0], 'Shield' => ['type' => 'effect', 'cost' => 113, 'damage' => 0, 'heal' => 0, 'turns' => 6, 'armor' => 7, 'mana' => 0], 'Poison' => ['type' => 'effect', 'cost' => 173, 'damage' => 3, 'heal' => 0, 'turns' => 6, 'armor' => 0, 'mana' => 0], 'Recharge' => ['type' => 'effect', 'cost' => 229, 'damage' => 0, 'heal' => 0, 'turns' => 5, 'armor' => 0, 'mana' => 101]]; printf('ans#22.1: %u' . PHP_EOL, battle($spells, $player, $boss, false)); printf('ans#22.2: %u' . PHP_EOL, battle($spells, $player, $boss, true)); // $runs is an arbitrarily chosen number; the higher its value the more accurate the answer will be function battle($spells, $player, $boss, $hardmode = false, $runs = 10000.0) { $debug = false; $minSpent = PHP_INT_MAX; $retries = 0; while ($retries < $runs) { $players = ['p' => $player, 'b' => $boss]; $activeEffects = []; while ($players['p']['spent'] < $minSpent) { foreach (array_keys($players) as $who) { // hardmode: reduce hp by 1 if ($hardmode && $who == 'p') { $players['p']['hp']--; if ($players['p']['hp'] <= 0) { break 2; } } // reset player's damage and armor $players['p']['damage'] = 0; $players['p']['armor'] = 0; // apply all effects that are currently active
$answer2_2 = preg_replace('#[^a-zA-Z0-9 ]#', '', strtolower(trim(fgets(STDIN)))); } else { if ($answer1 == preg_replace('#[^a-zA-Z0-9 ]#', '', strtolower("Confine yourself"))) { echo "You go and sit in the cell forever.\nYour Journey Ends\n"; die; } else { echo $incorrect; die; } } } // Battle echo $break; echo $break; $question3_1 = "'Very good, {$name}!' Good and Evil say in tandem, 'Now go forth into the abyss. You will find what you are looking for!' You walk further into the darkness, "; $question3_2 = ""; if ($answer2_1 == preg_replace('#[^a-zA-Z0-9 ]#', '', strtolower("Attack"))) { battle($rat, $health, $strength, $youmax); // battle($goblin, $health, $strength, $youmax); fwrite(STDOUT, $question3_1); $answer3_1 = preg_replace('#[^a-zA-Z0-0 ]#', '', strtolower(trim(fgets(STDIN)))); } else { if ($answer2_2 == preg_replace('#[^a-zA-Z0-9 ]#', '', strtolower("Attack"))) { battle($rat, $health, $strength, $youmax); fwrite(STDOUT, $question3_2); $answer3_2 = preg_replace('#[^a-zA-Z0-9 ]#', '', strtolower(trim(fgets(STDIN)))); } else { echo $incorrect; die; } }
$ship2Quantity = isset($_POST['ship2_quantity']) ? $_POST['ship2_quantity'] : 1; if (!$ship1Name || !$ship2Name) { header('Location: /index.php?error=missing_data'); die; } if (!isset($ships[$ship1Name]) || !isset($ships[$ship2Name])) { header('Location: /index.php?error=bad_ships'); die; } if ($ship1Quantity <= 0 || $ship2Quantity <= 0) { header('Location: /index.php?error=bad_quantities'); die; } $ship1 = $ships[$ship1Name]; $ship2 = $ships[$ship2Name]; $outcome = battle($ship1, $ship1Quantity, $ship2, $ship2Quantity); ?> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>OO Battleships</title> <!-- Bootstrap --> <link href="css/bootstrap.min.css" rel="stylesheet"> <link href="css/style.css" rel="stylesheet"> <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet"> <link href='http://fonts.googleapis.com/css?family=Audiowide' rel='stylesheet' type='text/css'>