Joc Ghiceste numarul
Scris: Mar Noi 18, 2014
Salut,
am un joc, vedeti mai jos scriptul. Problema este ca dupa ce numarul este ghicit sistemul permite ca jocul sa continue derutand intr-un fel jucatorul. Cum facem sa evitam acest lucru?
Cod: Selectaţi tot
<div>
<h4>JOC DISTRACTIV</h4>
Ghiceste numarul (min. <b>1</b> - max. <b>100</b>)
<p class="mic-gri">
<br />
Din prima incercare = Castigi Marele Premiu de 10 ori POT-ul
<br />
Din 2 incercari = Castigi de 4 ori POT-ul
<br />
Din 3 incercari = Castigi de 3 ori POT-ul
<br />
Din 4 incercari = Castigi de 2 ori POT-ul
<br />
Din 5 incercari = Pastrezi sau pierzi POT-ul
</p>
<button class="buton" onClick="javascript:window.location.href='test.php'; return false;">Incepe joc nou</button> <!--verifica adresa paginii php!-->
<br /><br />
<?php
$pot = $_POST['pot'];
$b = $_POST['b'];
$x = $_POST['x'];
$out = $_POST['out'];
$a = $_POST['number'];
if (event.keyCode == 13) {
document.formName.submit();
}
?>
<form action="test.php" name="pot" method="post"> <!--verifica adresa paginii php!-->
<?php session_start();
$_SESSION["pot"] = isset($_POST["pot"]) ? $_POST["pot"] : "";
$pot = isset($_SESSION["pot"]) ? $_SESSION["pot"] : "";
?>
<script>
function validateForm() {
var x = document.forms["pot"]["pot"].value;
if (x == null || x == "") {
alert("Completeaza POT-ul!");
return false;
}
}
</script>
<label>Completeaza POT-ul:</label>
<br />
<input class="inputcol" style="width: 100px; -webkit-appearance: none;" type="number" name="pot" id="pot" min="10" max="100" step="10" value="<?php echo $pot; ?>" placeholder="min 10 - max 100" autocomplete="off" />
<br />
<label>Alege numarul:</label>
<br />
<input class="inputcol" style="width: 100px; -webkit-appearance: none;" oninput="return validateForm()" autofocus="autofocus" type="number" name="number" id="number" min="1" max="100" autocomplete="off" />
<br />
<button class="buton" onClick="document.formName.submit(); return false;">Verifica</button>
<?php
if ($b == 0) {
$x = mt_rand(1,100);
$out = "";
}
?>
<input type="hidden" name="b" value=<?php echo ++$b;?> />
<input type="hidden" name="x" value=<?php echo $x;?> />
<br />
<?php
if ($b > 1)
{
if (($a != $x) AND (($b-1) < 6)) $out .= ($a < $x) ? "" . ($b-1) . " - Mai mare decat $a<br />" : "" . ($b-1) . " - Mai mic decat $a<br />";
?>
<br />
<input type="hidden" name="out" value="<?php echo $out;?>" />
<?php
echo $out ;
if (($a == $x) AND (($b-1) == 1)) {
echo "" . ($b-1) . " - <span class='succes' style='font-size: 24px;'><b>$a</b></span> este numarul corect<br /><span class='succes' style='font-size: 17px;'><b>Ai castigat Marele Premiu de " . ($pot * 10) . " puncte!<br /><br />Joc terminat</span><br />";
}
else if (($a == $x) AND (($b-1) == 2)) {
echo "" . ($b-1) . " - <span class='succes' style='font-size: 24px;'><b>$a</b></span> este numarul corect<br /><span class='succes' style='font-size: 17px;'><b>Ai castigat " . ($pot * 4) . " puncte!<br /><br />Joc terminat</b></span><br />";
}
else if (($a == $x) AND (($b-1) == 3)) {
echo "" . ($b-1) . " - <span class='succes' style='font-size: 24px;'><b>$a</b></span> este numarul corect<br /><span class='succes' style='font-size: 17px;'><b>Ai castigat " . ($pot * 3) . " puncte!<br /><br />Joc terminat</b></span><br />";
}
else if (($a == $x) AND (($b-1) == 4)) {
echo "" . ($b-1) . " - <span class='succes' style='font-size: 24px;'><b>$a</b></span> este numarul corect<br /><span class='succes' style='font-size: 17px;'><b>Ai castigat " . ($pot * 2) . " puncte!<br /><br />Joc terminat</b></span><br />";
}
else if (($a == $x) AND (($b-1) == 5)) {
echo "" . ($b-1) . " - <span class='succes' style='font-size: 24px;'><b>$a</b></span> este numarul corect<br /><span class='succes' style='font-size: 17px;'><b>Pastrezi POT-ul de $pot puncte!<br /><br />Joc terminat</b></span><br />";
}
else if (($a !== $x) AND (($b-1) == 5)) {
echo "<span class='succes' style='font-size: 24px;'><b>$x</b></span> era numarul corect<br /><span class='error' style='font-size: 17px;'><b>Ai pierdut POT-ul de $pot puncte<br /><br />Joc terminat</b></span><br />";
}
else if (($b-1) > 5) {
echo "<a href='test.php'><br />Incepe un joc nou!</a>";
}
}
?>
</form>
<br />
</div>