<?php
function battle($a, $b) {
if ($a > 2 || $b > 2) {
$a = (int)($a / 3);
$b = (int)($b / 3);
}
return ($a - $b + 3) % 3;
}
$hand_list = array(
0 => 'グー',
1 => 'チョキ',
2 => 'パー',
3 => '奴隷',
6 => '皇帝',
);
$result_list = array(
2 => 'あなたの勝ちです',
1 => 'あなたの負けです',
0 => 'あいこです',
-1 => '不明',
);
// 手が送信されたとき勝負を実行
if (isset($_POST['you'])) {
// 整数型にキャスト(不正な値をエラー無しに防ぐ効果もある)
$you = (int)$_POST['you'];
// コンピュータの手を選出
$com = array_rand($hand_list);
if (!isset($hand_list[$you])) {
// 不正な値のときは自分の手を「?」、結果を「不明」にする
$hand_list[$you = -1] = '?';
$result = -1;
} else {
// 正しい値のときは関数に渡す
$result = battle($you, $com);
}
}
// ヘッダー送信
header('Content-Type: application/xhtml+xml; charset=utf-8');
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja">
<head>
<title>じゃんけんゲーム</title>
<style type="text/css">
label { display: block; }
</style>
</head>
<body>
<h1>じゃんけん</h1>
<form action="<?=basename($_SERVER['SCRIPT_NAME'])?>" method="post">
<label><input type="radio" name="you" value="0" checked="checked" />グー</label>
<label><input type="radio" name="you" value="1" />チョキ</label>
<label><input type="radio" name="you" value="2" />パー</label>
<label><input type="radio" name="you" value="6" />皇帝</label>
<label><input type="radio" name="you" value="3" />奴隷</label>
<label><input type="submit" value="勝負!" /></label>
</form>
<?php if (isset($result)): ?>
<h1>勝負!</h1>
<p>
あなた: <?=$hand_list[$you]?><br />
コンピュータ: <?=$hand_list[$com]?><br />
<?=$result_list[$result]."\n"?>
</p>
<?php endif; //isset($result)がTrueのときだけ上記を表示できる ?>
</body>
</html>
- Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.37, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.7
- <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja">
<head>
<title>じゃんけんゲーム</title>
<style type="text/css">
label { display: block; }
</style>
</head>
<body>
<h1>じゃんけん</h1>
<form action="lj3pu" method="post">
<label><input type="radio" name="you" value="0" checked="checked" />グー</label>
<label><input type="radio" name="you" value="1" />チョキ</label>
<label><input type="radio" name="you" value="2" />パー</label>
<label><input type="radio" name="you" value="6" />皇帝</label>
<label><input type="radio" name="you" value="3" />奴隷</label>
<label><input type="submit" value="勝負!" /></label>
</form>
</body>
</html>
- Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
- <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja">
<head>
<title>じゃんけんゲーム</title>
<style type="text/css">
label { display: block; }
</style>
</head>
<body>
<h1>じゃんけん</h1>
<form action="<?=basename($_SERVER['SCRIPT_NAME'])?>" method="post">
<label><input type="radio" name="you" value="0" checked="checked" />グー</label>
<label><input type="radio" name="you" value="1" />チョキ</label>
<label><input type="radio" name="you" value="2" />パー</label>
<label><input type="radio" name="you" value="6" />皇帝</label>
<label><input type="radio" name="you" value="3" />奴隷</label>
<label><input type="submit" value="勝負!" /></label>
</form>
</body>
</html>
preferences:
138 ms | 404 KiB | 210 Q