tangar Posted September 8, 2017 Share Posted September 8, 2017 (edited) Hello! I'm rookie. Please help me to solve problem with storing stuff in hidded fields for 'Guess a number' program: https://hastebin.com/vomasuvere.xml Why this... if ($choice == 'big'){ print $guess[1]=($guess[0] / 2); } elseif ($choice == 'small') { print $guess[2]=($guess[0] + 25); ...doesn't work? p.s. I know that it's the worst decision to use arrays and hidden fields in such program; but I've made decision to make it like this and can't drop it Please help! p.p.s. This is my live stream, how I made this program - https://www.youtube.com/watch?v=lLQ4J2JkO60 Edited September 8, 2017 by tangar Quote Link to comment https://forums.phpfreaks.com/topic/304915-guess-a-number-help-with-hidden-fields-and-arrays/ Share on other sites More sharing options...
archive Posted September 8, 2017 Share Posted September 8, 2017 Guess your code might have been a better title. A more detailed description of the problem might get you a better answer. You're missing a closing brace in your code for a start. Why would you want to show yourself coding when it doesn't work? Quote Link to comment https://forums.phpfreaks.com/topic/304915-guess-a-number-help-with-hidden-fields-and-arrays/#findComment-1550996 Share on other sites More sharing options...
Solution ginerjm Posted September 8, 2017 Solution Share Posted September 8, 2017 When you say it doesn't work, how do you know? Are you getting error messages? Is php error checking turned on (see my signature)? 1 Quote Link to comment https://forums.phpfreaks.com/topic/304915-guess-a-number-help-with-hidden-fields-and-arrays/#findComment-1550997 Share on other sites More sharing options...
tangar Posted September 9, 2017 Author Share Posted September 9, 2017 (edited) ginerjm, thank you very much! I though I had all errors on, but it seems NOTICE was off. With this: error_reporting(E_ALL); ini_set('display_errors', '1'); I saw notices: Notice: Undefined index: $guess in C:\OpenServer\domains\localhost\php\17.php on line 6 Notice: Undefined index: $choice in C:\OpenServer\domains\localhost\php\17.php on line 7Is your number 50? So I understand that got troubles with forms. And after I worked some time, I noticed that I got silly error: $guess = $_POST['$guess']; It has to be: $guess = $_POST['guess']; So, what I got now, it works as intended: https://hastebin.com/dedoqixahu.http Thank you, guys! Edited September 9, 2017 by tangar Quote Link to comment https://forums.phpfreaks.com/topic/304915-guess-a-number-help-with-hidden-fields-and-arrays/#findComment-1551004 Share on other sites More sharing options...
ignace Posted September 9, 2017 Share Posted September 9, 2017 Here's the example code of your game. I will leave it up to you to improve it and make sure it does not repeat a number. <?php // game variables $game_is_started = false; $game_is_won = false; $game_is_lost = false; $game_pc_guesses = []; $game_pc_range = []; $game_last_guess = null; $game_number_low = 1; $game_number_high = 10; $game_allow_guesses = 5; $game_total_guesses = 0; $game_p1_win = 0; // computer $game_p2_win = 0; // player // end game variables if ($_SERVER['REQUEST_METHOD'] === 'POST') { $game_number_low = intval($_POST['number_low']); $game_number_high = intval($_POST['number_high']); $game_allow_guesses = intval($_POST['allow_guesses']); $game_p1_win = intval($_POST['p1_win']); $game_p2_win = intval($_POST['p2_win']); if ($game_number_high < $game_number_low) { list($game_number_low, $game_number_high) = [ $game_number_high, $game_number_low, ]; } $game_is_started = true; if (array_key_exists('start', $_POST)) { $game_last_guess = mt_rand($game_number_low, $game_number_high); } else if (array_key_exists('last_result', $_POST)) { if (array_key_exists('pc_guesses', $_POST)) { $game_pc_guesses = array_map('intval', $_POST['pc_guesses']); } else { $game_pc_guesses = []; } $game_last_guess = intval($_POST['last_guess']); $game_total_guesses = intval($_POST['total_guesses']) + 1; array_push($game_pc_guesses, $game_last_guess); if ($game_total_guesses > $game_allow_guesses) { $game_is_lost = true; $game_p2_win = $game_p2_win + 1; } else { switch ($_POST['last_result']) { case 'bigger': $game_last_guess = ($game_last_guess === $game_number_high || ($game_last_guess + 1 === $game_number_high)) ? mt_rand($game_number_low, $game_number_high) : mt_rand($game_last_guess + 1, $game_number_high); break; case 'smaller': $game_last_guess = ($game_last_guess === $game_number_low || ($game_last_guess - 1 === $game_number_low)) ? mt_rand($game_number_low, $game_number_high) : mt_rand($game_number_low, $game_last_guess - 1); break; case 'won': $game_is_won = true; $game_p1_win = $game_p1_win + 1; break; } } } } ?> <h1>Computer: <?= $game_p1_win ?>, Player: <?= $game_p2_win ?></h1> <?php if ($game_is_started && !($game_is_won || $game_is_lost)): ?> <form action="#" method="post"> <fieldset> <input type="hidden" name="is_started" value="<?= $game_is_started ?>"> <input type="hidden" name="number_low" value="<?= $game_number_low ?>"> <input type="hidden" name="number_high" value="<?= $game_number_high ?>"> <input type="hidden" name="allow_guesses" value="<?= $game_allow_guesses ?>"> <input type="hidden" name="total_guesses" value="<?= $game_total_guesses ?>"> <input type="hidden" name="last_guess" value="<?= $game_last_guess ?>"> <input type="hidden" name="p1_win" value="<?= $game_p1_win ?>"> <input type="hidden" name="p2_win" value="<?= $game_p2_win ?>"> <?php foreach ($game_pc_guesses as $guess): ?> <input type="hidden" name="pc_guesses[]" value="<?= $guess ?>"> <?php endforeach ?> <p>Is your number <?= $game_last_guess ?>?</p> <button type="submit" name="last_result" value="bigger">No, my number is bigger!</button> <button type="submit" name="last_result" value="won">Yup, that's my number!</button> <button type="submit" name="last_result" value="smaller">No, my number is smaller!</button> </fieldset> </form> <?php else: ?> <?php if ($game_is_won): ?> <h2>I win! ^^</h2> <?php elseif ($game_is_lost): ?> <h2>You win!</h2> <?php endif ?> <form action="#" method="post"> <fieldset> <input type="hidden" name="p1_win" value="<?= $game_p1_win ?>"> <input type="hidden" name="p2_win" value="<?= $game_p2_win ?>"> <h2>Hello, do you wanna play <?= $game_is_won ? 'another' : 'a' ?> game?</h2> <p> Yes, find my number between <input type="number" name="number_low" value="<?= $game_number_low ?>"> and <input type="number" name="number_high" value="<?= $game_number_high ?>">. </p> <p>You have <input type="number" name="allow_guesses" value="<?= $game_allow_guesses ?>"> guesses.</p> <input type="submit" name="start" value="Play!" style="font-size:big"> </fieldset> </form> <?php endif ?> 1 Quote Link to comment https://forums.phpfreaks.com/topic/304915-guess-a-number-help-with-hidden-fields-and-arrays/#findComment-1551053 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.