在游戏中允许3次尝试 - php

时间:2011-02-07 02:22:27

标签: php

  

可能重复:
  Loop Iteration in Php Game

试图允许用户猜三次“1”电影,如果他们没有得到它,它会告诉他们正确答案(可变:$ rand_keys)。它还扣除猜测并显示剩下的猜测量($猜猜变量可能出错?)。

这里出了什么问题?请告诉我一个可能的解决方案。

<style type="text/css">
input {border:1px solid #ADD8E6; font-size:1.2em;}
input.spec {background-color:#ddd;}
</style>
<?php
    echo "<fieldset><h1><legend>Testing your Academy Award Trivia</h1>";
    $ages['Casablanca'] = "1943";
    $ages['Around The World in 80 Days'] = "1956";
    $ages['Patton'] = "1970";
    $ages['Annie Hall'] = "1977";
    $ages['Chariots of Fire'] = "1981";
    $ages['Dances With Wolves'] = "1990";
    $ages['Crash'] = "2005";
    $ages['The Departed'] = "2006";

    $rand_keys = array_rand($ages, 1);

    $guesses = 3;

?>
<form method='post' name="inputyear" onsubmit="return validate(this);">
Give the year below won academy award<br><br>
<Strong>Movie:</strong> <input type='text' name='movie' class="spec" value='<?= $rand_keys ?>'     readonly='readonly' /><br><br>
<Strong>Year it Won the Oscar:</Strong> <input type='text' name='year' size="30" /><br/><br>
<strong>You have: </strong> <?php $guesses; ?> guesses left<br><br>
<input type='submit' name='submit' value="Get Result" onClick="makeGuess()" />
</form>

<?php
$movie = isset($_POST['movie']) ? $_POST['movie'] : false;
$guessedYear = isset($_POST['year']) ? (int) $_POST['year'] : false;
if ($movie && $guessedYear) {
    $realyear = $ages[$movie];
}
@$_SESSION[$movie]['$guesses']++;
if ($realyear && $_SESSION[$movie]['$guesses'] < 3) {

    if ($guessedYear == $realyear) {
        echo "Correct! " . "during year " . $realyear;
    }
    if ($guessedYear < $realyear) {
        echo "Wrong, year too low";
        $guesses--;
    }
    if ($guessedYear > $realyear) {
        echo "Wrong, year too high";
        $guesses--;
    }

} elseif ($_SESSION[$movie]['$guesses'] >= 3) {
    echo "Sorry, too many tries. the answer was " . $realyear;
} else {
    echo "Sorry, You managed not to pick a year. Please try again";
    $_SESSION[$movie]['guesscount']--;
}
?>

1 个答案:

答案 0 :(得分:0)

如果您将数据存储在Cookie中,我建议您对其进行加密。如果它没有加密,那么可以打开会话cookie并重置他们的分数。 (我不确定这个应用程序有多严肃,但从商业角度来看,加密总是一个好主意。)