私はちょうど PHP を学んでいて、自分自身のプロジェクトとして、ユーザーが 1 から 100 までの数字を推測する小さなゲームを開発しています。彼が正しければ、同じか、そうでなければ答えを示します。これを 3 回の試行に制限し、3 回目の試行後に「Your 3 Tries are over」というメッセージをユーザーに表示するようにします。セッションを破棄してゲームを再開できるようにする「再試行」ボタンとともに。同じことで私を助けてください。
コードは以下のとおりです。
<?php
$rand= rand(1,100);
$guess=$_POST['guess'];
$submit=$_POST['submit'];
session_start();
if(isset($submit)) {
if (!isset($_SESSION["attempts"])) $_SESSION["attempts"] = 0;
if ($_SESSION["attempts"] < 3) {
if($guess<1 ||$guess>100) {
echo "your guess must be a number between 1 and 100";
}
elseif($guess!=$rand)
{
echo "Incorrect,the correct answer is".$rand;
$_SESSION["attempts"] = $_SESSION["attempts"] + 1;
}
else
{
echo "That is correct!";
}
}
}
else
{
unset($_SESSION["attempts"]);
include_once('game.php');
}
?>