firedealer Posted August 2, 2010 Share Posted August 2, 2010 I developed this game and I couldn't figure how to make random only on available shot glasses but it would still random with unavailable shot glasses. I simple give up. I plan to share this php once I nailed it down but apparently, not successful. Now, I'm share with you anyway. If you can solve the problem and please let us know! http://pastebin.com/F3N05ExK [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/209616-php-random-problem-pick-your-poison-game/ Share on other sites More sharing options...
jcbones Posted August 2, 2010 Share Posted August 2, 2010 Try this: <?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Pick Your Poison</title> <style type="text/css"> * { margin: 0; padding: 0; } a, a:visited { text-decoration: none; color: #333333; } a:hover, a:active { text-decoration: underline; color: #000099; } h1 { font-family: serif; font-size: 35pt; margin: 10px; } .shot { border: 1px #F00 dashed; color: #00F; height: 250px; width: 200px; margin: 20px; margin-left: 50px; float: left; font-size: 30px; } .red { color: #F00; } .green { color: #0F0; } .grey { color: #CCC; } </style> </head> <?php for($i = 1; $i < 9; $i++) { $shot[$i] = 'available'; } //De-bug uncomment the next line. //echo 'start: <pre>'; print_r($shot); echo '</pre>'; if(isset($_POST['refresh'])) { unset($_SESSION['active']); } //De-bug, uncomment the next line: //echo 'Sesson: <pre>'; print_r($_SESSION); echo '</pre>'; if(isset($_SESSION['active'])) { for($i = 1; $i <9; $i++) { $shot[$i] = $_SESSION["s{$i}"]; } } //De-bug uncomment the next line: //echo 'Shot: <pre>'; print_r($shot); echo '</pre>'; //echo 'POST: <pre>'; print_r($_POST); echo '</pre>'; // To resolve "Notice: Undefined index: shot01-06" for($i = 1; $i < 9; $i++) { $shot[$i] = (!isset($_POST["shot0{$i}"])) ? $shot[$i] : $_POST["shot0{$i}"]; } $random = (!isset($_POST['random'])) ? 'false' : $_POST['random']; //De-bug uncomment the next line: //echo 'Shot: <pre>'; print_r($shot); echo '</pre>'; $_SESSION['active'] = 1; for($i = 1; $i < 9; $i++) { $_SESSION["s{$i}"] = $shot[$i]; } $result_poison = "red"; $result_safe = "green"; $result_out = "grey"; foreach($shot as $k => $v) { if($v == 'available') { $p[$k] = 1; } } if ($random == "true" && in_array('available',$shot)) { $poison = array_rand($p); } else { $poison = NULL; } ////////////// // SHOT 1-8 // ////////////// for ($i = 1; $i < 9; $i++) { if ($shot[$i] == "available") { if ($random == "true" && $poison == $i) { $result0[$i] = $result_poison; } else { $result0[$i] = $result_safe; } } elseif ($shot[$i] == "unavailable") { if ($random == "true" && $poison == $i) { $result0[$i] = $result_out; $poison = mt_rand(1,; } else { $result0[$i] = $result_out; } } else { $result0[$i] = NULL; } ; } ?> <body> <div align="center"> <h1>Pick Your Poison</h1><br /> <?php echo $poison ?><br /> <?php /* <p><a href="?shot01=<? echo $shot01; ?>&shot02=<? echo $shot02; ?>&shot03=<? echo $shot03; ?>&shot04=<? echo $shot04; ?>&shot05=<? echo $shot05 ?>&shot06=<? echo $shot06; ?>&shot07=<? echo $shot07; ?>&shot08=<? echo $shot08; ?>&random=true">Random</a></p> */ ?> <form action="" method="post"> <input type="hidden" name="random" value="true" /> <input type="submit" value="Random" /> </form> <span class="shot"><span class="number <?php echo $result0[1]; ?>">Shot Glass<br />#1</span><br /><br /> <form action="" method="post"> <input type="hidden" name="shot01" value="unavailable" /> <input type="submit" value="Out" /> </form><br /> <form action="" method="post"> <input type="hidden" name="shot01" value="available" /> <input type="submit" value="In" /> </form> </span> <span class="shot"><span class="number <?php echo $result0[2]; ?>">Shot Glass<br />#2</span><br /><br /> <form action="" method="post"> <input type="hidden" name="shot02" value="unavailable" /> <input type="submit" value="Out" /> </form><br /> <form action="" method="post"> <input type="hidden" name="shot02" value="available" /> <input type="submit" value="In" /> </form> </span> <span class="shot"><span class="number <?php echo $result0[3]; ?>">Shot Glass<br />#3</span><br /><br /> <form action="" method="post"> <input type="hidden" name="shot03" value="unavailable" /> <input type="submit" value="Out" /> </form><br /> <form action="" method="post"> <input type="hidden" name="shot03" value="available" /> <input type="submit" value="In" /> </form> </span> <span class="shot"><span class="number <?php echo $result0[4]; ?>">Shot Glass<br />#4</span><br /><br /> <form action="" method="post"> <input type="hidden" name="shot04" value="unavailable" /> <input type="submit" value="Out" /> </form><br /> <form action="" method="post"> <input type="hidden" name="shot04" value="available" /> <input type="submit" value="In" /> </form> </span><br /> <div style="clear:both"></div> <span class="shot"><span class="number <?php echo $result0[5]; ?>">Shot Glass<br />#5</span><br /><br /> <form action="" method="post"> <input type="hidden" name="shot05" value="unavailable" /> <input type="submit" value="Out" /> </form><br /> <form action="" method="post"> <input type="hidden" name="shot05" value="available" /> <input type="submit" value="In" /> </form> </span> <span class="shot"><span class="number <?php echo $result0[6]; ?>">Shot Glass<br />#6</span><br /><br /> <form action="" method="post"> <input type="hidden" name="shot06" value="unavailable" /> <input type="submit" value="Out" /> </form><br /> <form action="" method="post"> <input type="hidden" name="shot06" value="available" /> <input type="submit" value="In" /> </form> </span> <span class="shot"><span class="number <?php echo $result0[7]; ?>">Shot Glass<br />#7</span><br /><br /> <form action="" method="post"> <input type="hidden" name="shot07" value="unavailable" /> <input type="submit" value="Out" /> </form><br /> <form action="" method="post"> <input type="hidden" name="shot07" value="available" /> <input type="submit" value="In" /> </form> </span> <span class="shot"><span class="number <?php echo $result0[8]; ?>">Shot Glass<br />#8</span><br /><br /> <form action="" method="post"> <input type="hidden" name="shot08" value="unavailable" /> <input type="submit" value="Out" /> </form><br /> <form action="" method="post"> <input type="hidden" name="shot08" value="available" /> <input type="submit" value="In" /> </form> </span><br /> <div style="clear:both"></div> <form action="" method="post"> <input type="submit" name="refresh" value="Refresh" /> </form> </div> Quote Link to comment https://forums.phpfreaks.com/topic/209616-php-random-problem-pick-your-poison-game/#findComment-1094383 Share on other sites More sharing options...
firedealer Posted August 3, 2010 Author Share Posted August 3, 2010 Thanks! I need to study $_SESSION more, I suspect $_SESSION is most important part for almost any project. The in_array() function is new to me, there are so many functions! Again, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/209616-php-random-problem-pick-your-poison-game/#findComment-1094843 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.