Joc Ghiceste numarul
Discutii despre script-uri si coduri PHP-MySQL, precum si lucru cu XML in PHP.
-
adrian_ady
- Mesaje:80
Joc Ghiceste numarul
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>
MarPlo
Mesaje:4343
Salut
Ai putea sa faci ce butonul Verifica sa nu fie afisat cand numarul este aflat.
Cu un "echo" sa afiseze butonul doar daca numarul nu e ghicit, cam asa:
Cod: Selectaţi tot
<?php
echo (isset($a) && isset($x) && $a == $x) ? '' : '<button class="buton" onClick="document.formName.submit(); return false;">Verifica</button>';
?>
adrian_ady
Mesaje:80
Multumesc, am facut proba si e OK.
Am eliminat cateva aiureli, cu siguranta mai sunt si altele insa tin sa adaug varianta finala, 100% optimizata (cu parerea de rau ca valoarea $x nu poate fi 'hidden' in sursa paginii).
Am eliminat adresele paginii din linkuri deci in prezent scriptul este universal, functioneaza indiferent de pagina in care este implementat, pentru cazul in care cineva va avea nevoie.
Cod: Selectaţi tot
<div style="display: block;"
<div style="display: inline-block; vertical-align: top; margin-right: 30px;">
<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 3 ori POT-ul
<br />
Din 3 incercari = Castigi de 2 ori POT-ul
<br />
Din 4 incercari = Pastrezi sau pierzi POT-ul
</p>
</div>
<div style="display: inline-block; vertical-align: top;">
<br />
<button class="buton" onClick="window.location.href=window.location.href">Reseteaza</button>
<br /><br />
<?php
$pot = $_POST['pot'];
$b = $_POST['b'];
$x = $_POST['x'];
$tot = $_POST['tot'];
$out = $_POST['out'];
$a = $_POST['number'];
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" name="formular" method="post">
<label>Alege POT-ul:</label>
<br />
<?
if ($b < 1) {
?>
<select class="inputcol" style="padding: 5px 6px 5px 2px; width: 100px;" name="pot">
<option value="100" selected>100 maxim</option>
<option value="90">90</option>
<option value="80">80</option>
<option value="70">70</option>
<option value="60">60</option>
<option value="50">50</option>
<option value="40">40</option>
<option value="30">30</option>
<option value="20">20</option>
<option value="10">10 minim</option>
</select>
<?
}
else {
?><input type="hidden" name="pot" value="<?php echo $pot; ?>" />
<input class="inputcol" style="width: 100px; -webkit-appearance: none;" value="<?php echo $pot; ?>" disabled /><?
}
?>
<br />
<label>Completeaza numarul:</label>
<br />
<?
if ($b == 0) {
echo '<input class="inputcol" style="width: 100px;" placeholder="min 1 - max 100" type="number" name="number" min="1" max="100" autocomplete="off" required />';
}
else if (($b > 0) AND ($b < 4) AND ($a != $x)) {
echo '<input class="inputcol" style="width: 100px;" autofocus="autofocus" type="number" name="number" min="1" max="100" autocomplete="off" required />';
}
else {
echo '<input class="inputcol" style="width: 100px;" disabled />';
}
if ($b < 4)
echo (isset($a) && isset($x) && ($a == $x)) ? '' : '<br /><button class="buton" onClick="document.formular.submit(), return false;">Verifica</button>';
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 />
<?
if ($b > 1)
{
if (($a != $x) AND (($b-1) < 5)) $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;?>" />
<?
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'><b>Ai castigat " . ($pot * 10) . " puncte!</b></span>";
}
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'><b>Ai castigat " . ($pot * 3) . " puncte!</b></span>";
}
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'><b>Ai castigat " . ($pot * 2) . " puncte!</b></span>";
}
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'><b>Pastrezi POT-ul de $pot puncte!</b></span>";
}
else if (($a !== $x) AND (($b-1) == 4)) {
echo "<span class='succes' style='font-size: 24px;'><b>$x </b></span> era numarul corect<br /><span class='error'><b>Ai pierdut POT-ul de $pot puncte</b></span>";
}
else if (($b-1) > 4) {
echo "<span class='succes' style='font-size: 24px;'><b>$x </b></span> era numarul corect<br /><span class='error'><b>Ai pierdut POT-ul de $pot puncte</b></span>";
}
}
?>
</form>
<br />
<?
if (($a == $x) OR (($a !== $x) AND (($b-1) >= 4))) {
?><button class="buton" onClick="window.location.href=window.location.href">Incepe joc nou</button><br /><br /><?
}
?>
</div>
</div>