2
1

More than 1 year has passed since last update.

【PHP】ハイ&ローゲーム

Last updated at Posted at 2021-05-27

解説

・コンピュータが決めた数字をプレイヤーの入力した数値が大きいか小さいか予想するゲーム。

1・プレイヤーは1~9のどれかの数字を入力
2・1から9までの中からランダムに数値を一つ取得
3・入力した数字が、コンピュータの数字より大きければハイ
4・入力した数字が、コンピュータの数字より小さければロー
5・入力した数字が、コンピュータの数字と同じならドロー

コード

<?php
$input = fgets(STDIN);
$number = mt_rand(1, 9);
echo '入力値:',$input," 相手",$number,PHP_EOL;
if($number < $input){
    echo 'ハイ';
}else if($number > $input){
    echo 'ロー';
} else {
    echo 'ドロー';
}

結果

入力値:5 相手5
ドロー
<?php
 
$number = mt_rand(1, 9);
$result = '';
 
if($_SERVER['REQUEST_METHOD'] === 'POST'){
  $choice = '';
  if(isset($_POST['choice']) === true){
    $choice = $_POST['choice'];
  }
 
  $previous = '';
  if(isset($_POST['previous']) === true){
    $previous = $_POST['previous'];
  }
 
  if($previous < $number){
    $answer = 'ハイ';
  } else if ( $previous > $number ){
    $answer = 'ロー';
  } else {
    $answer = 'ドロー';
  }
 
  if($answer === 'ドロー'){
    $result = '引き分け';
  } else if ($answer === $choice){
    $result = '勝ち!';
  } else {
    $result = '負け...';
  }
}
 
?>
<!DOCTYPE html>
<html lang="ja">
<head>
  <title>ハイアンドローゲーム</title>
</head>
<body>
  <h1>ハイアンドローゲーム</h1>
  <?php if($_SERVER['REQUEST_METHOD'] === 'POST'){ ?>
    <p>対象の数値: <?php echo $previous; ?>
    <p>選択: <?php echo $choice; ?></p>
    <p>出た数字: <?php echo  $number; ?>
    <p>判定: <?php echo $answer; ?> 今回の勝負は<?php echo $result; ?></p>
 
    <p><a href="high_and_low.php">次の勝負へ</a></p>
  <?php } else { ?>
 
    <p>現在の数値: <?php echo $number; ?></p>
 
    <!-- method="post"でフォームを作成 -->
    <form method="post">
      <div>
        <label>ハイ: <input type="radio" name="choice" value="ハイ" required></label>
        <label>ロー: <input type="radio" name="choice" value="ロー" required></label>
      </div>
      <!-- 現在の数値を送信 -->
      <input type="hidden" name="previous" value="<?php echo $number; ?>">
      <input type="submit" value="勝負">
    </form>
  <?php } ?>
</body>
</html>
2
1
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
2
1