开发者

Allowing 3 attempts at game - php [duplicate]

开发者 https://www.devze.com 2023-02-08 18:13 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: Loop Iteration in Php Game
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Loop Iteration in Php Game

Trying to allow the user to guess at "1" movie, three times, if they dont get it, it tells them the right answer (which is variable: $rand_keys). It also deducts guesses and displays the amount of guesses left (possible error with $guesses variable?).

Whats going wrong here? Please show me a possible solution.

<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']--;
}
?>


If you're storing data in cookies I recommend you encrypt it. If it's not encrypted then one could open up the session cookie and reset their score. (I'm not sure how serious this application is, but from a commercial point of view, encryption is always a good idea.)

0

精彩评论

暂无评论...
验证码 换一张
取 消