第2話 カツアゲ

**カツアゲはいかんよ！**

兄弟ゲンカしていた太郎と次郎が仲直りして、コンビニへおやつを買いに行く途中、
不良中学生グループに出くわして、カツアゲされる様子をコード化しました。（謎）

```html+php: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;
}

/*
 * ダメージ計算
 */
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;
	}
}

/*
 * チームの状態を調べる
 */
function teamPoint($team) {
	$value = 0;
	foreach ($team->members as $member) {
		$value += $member->hitPoint;
	}
	return $value;
}

/*
 * 武器
 */
$weapon = array (
	"name" => "金属バット",
	"ATK" => "3d6",
);

$hand = array (
	"name" => "素手",
	"ATK" => "1d6",
);

/*
 * 太郎と次郎
 */

$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;

/*
 * チーム（パーティ）の結成
 */
$teamA = new stdClass();

$teamA->members[] = $taro;
$teamA->members[] = $jiro;

$taro->team = $teamA;
$jiro->team = $teamA;

/*
 * 不良たち
 */
$hooliganA = new stdClass();
$hooliganA->name = "リーゼント";
$hooliganA->hitPoint = dice("4d6");
$hooliganA->defPoint = dice("1d6+4");
$hooliganA->weapon = $hand;

$hooliganB = new stdClass();
$hooliganB->name = "角刈り";
$hooliganB->hitPoint = dice("4d6");
$hooliganB->defPoint = dice("1d6+4");
$hooliganB->weapon = $hand;

$hooliganC = new stdClass();
$hooliganC->name = "グラサン";
$hooliganC->hitPoint = dice("4d6");
$hooliganC->defPoint = dice("1d6+4");
$hooliganC->weapon = $hand;

/*
 * ゴロツキの結成
 */
$teamB->members[] = $hooliganA;
$teamB->members[] = $hooliganB;
$teamB->members[] = $hooliganC;

$hooliganA->team = $teamB;
$hooliganB->team = $teamB;
$hooliganC->team = $teamB;

?>

<!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;
echo "  |  ";
echo $hooliganA->name . "HP:" . $hooliganA->hitPoint . " / " . $hooliganB->name . "HP:" . $hooliganB->hitPoint . " / " . $hooliganC->name . "HP:" . $hooliganC->hitPoint;
echo "<br>\n<hr>\n";

while (teamPoint($teamA) > 0 && teamPoint($teamB) > 0) {
	// 攻撃順序を決める
	$initiative = array();
	foreach ($teamA->members as $member) {
		if ($member->hitPoint > 0)
			$initiative[] = $member;
	}
	foreach ($teamB->members as $member) {
		if ($member->hitPoint > 0)
			$initiative[] = $member;
	}
	// ランダムでｗ
	shuffle($initiative);
	// 個別行動ループ
	foreach ($initiative as $offence) {
		$diffence = null;
		if ($offence->team === $teamA) {
			if ($offence->hitPoint > 0 && teamPoint($teamB) > 0) {
				shuffle($teamB->members);
				foreach($teamB->members as $tmp) {
					if ($tmp->hitPoint <= 0) continue;
					$diffence = $tmp;
					break;
				}
			}
		} else {
			if ($offence->hitPoint > 0 && teamPoint($teamA) > 0) {
				shuffle($teamA->members);
				foreach($teamA->members as $tmp) {
					if ($tmp->hitPoint <= 0) continue;
					$diffence = $tmp;
					break;
				}
			}
		}
		if ($diffence != null)
			Attack($offence, $diffence);
	}
	// 1ターン終了
	echo "<hr>\n";
	echo $taro->name . "HP:" . $taro->hitPoint . " / " . $jiro->name . "HP:" . $jiro->hitPoint;
	echo "  |  ";
	echo $hooliganA->name . "HP:" . $hooliganA->hitPoint . " / " . $hooliganB->name . "HP:" . $hooliganB->hitPoint . " / " . $hooliganC->name . "HP:" . $hooliganC->hitPoint;
	echo "<br>\n<hr>\n";
}
?>
</body>
</html>
```