LoginSignup
6
5

More than 5 years have passed since last update.

第1話 兄弟ゲンカ

Last updated at Posted at 2016-01-29

兄弟ゲンカは、いかん!

太郎と次郎のケンカをコード化しました。(謎)

battle.php
<?php

function dice($param) {
    $_value = 0;
    if (preg_match('/^(\d+)?[dD]\d+([+]\d+)?$/', $param, $_check) === 1) {
        $_param = $_check[0];
        $_result = preg_split('/[dD]/', $_param);
        $_shakes = ($_result[0] == '') ? 1 : $_result[0];
        $_param = $_result[1];
        $_result = preg_split('/[+]/', $_param);
        $_faces = $_result[0];
        $_baseValue = (!isset($_result[1]) || $_result[1] == '') ? 0 : $_result[1];
        for ($_index = 0; $_index < $_shakes; $_index++)
            $_value += mt_rand(1, $_faces);
        $_value += $_baseValue;
    }
    return $_value;
}

$weapon = array (
    "name" => "こんぼう",
    "ATK" => "2d6",
);

function calcDamages($weapon, $defPoint, &$message) {
    $damages = (int)(dice($weapon["ATK"]) - $defPoint);
    if ($damages == 0) {
        $message = "攻撃が弾き返された!";
    }
    if ($damages < 0) {
        $message = "攻撃は届かなかった.";
        $damages = 0;
    }
    if ($damages > 0) {
        $message = $damages."のダメージを与えた!";
    }
    return $damages;
}

function Attack($a, $b) {
    $dmg = calcDamages($a->weapon, $b->defPoint, $msgs);
    echo $a->name . "が跳びかかった!<br>\n";
    echo $b->name . "に". $msgs . "<br>\n";
    $b->hitPoint -= $dmg;
    if ($b->hitPoint < 0) {
        $b->hitPoint = 0;
    }
}

$taro = new stdClass();
$taro->name = "太郎";
$taro->hitPoint = dice("3d6");
$taro->defPoint = dice("1d6");
$taro->weapon = $weapon;

$jiro = new stdClass();
$jiro->name = "次郎";
$jiro->hitPoint = dice("3d6");
$jiro->defPoint = dice("1d6");
$jiro->weapon = $weapon;
?>

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>ヨンプル</title>
</head>
<body>
<?php
echo $taro->name . "HP:" . $taro->hitPoint . " / " . $jiro->name . "HP:" . $jiro->hitPoint . "<br>\n<hr>\n";
while ($taro->hitPoint > 0 && $jiro->hitPoint > 0) {
    $taro->initiative = dice("1d6");
    $jiro->initiative = dice("1d6");
    if ($taro->initiative > $jiro->initiative) {
        Attack($taro, $jiro);
        if ($jiro->hitPoint > 0) {
            Attack($jiro, $taro);
        }
    } else {
        Attack($jiro, $taro);
        if ($taro->hitPoint > 0) {
            Attack($taro, $jiro);
        }
    }
    echo "<hr>\n" . $taro->name . "HP:" . $taro->hitPoint . " / " . $jiro->name . "HP:" . $jiro->hitPoint . "<br>\n<hr>\n";
}
?>
</body>
</html>

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5