lilush Posted August 7, 2012 Share Posted August 7, 2012 I am trying to make a hanging man game. I have ten words with 5 letters each. the program is supposed to choose one word out of the ten randomly and the user has to guess the letters and replace the underlines that appear on screen. the correct letters stay on screen. there is also a picture of the hanging man and with each mistake a body part appears- they get 6 tries or they fail. This is my code so far, it's a mess because I don't really know what i'm doing... (my ten words are in hebrew at the top) I'll really appreciate some help <?php session_start(); if ( !isset($_SESSION['word']) ) { $wordList="שולחן וילון מדפסת גיטרה פסנתר דרקון תרמיל בקבוק חולצה תמונה"; } ?> <html dir="rtl"> <body> <?php if(isset($_POST["exit"])) { ?> תודה ולהתראות! <br> <form action="game.php" method="post"><br><br> <input type="submit" value="חזור למשחק" /> </form> <?php } elseif(isset($_POST["guessletter"])) { if(!isset($_SESSION["totalGuess"])) { $_SESSION["totalGuess"] = 0; } $totalGuess= 0; } $maxError=6; $wordList= rand(1,10); $pickedWord= $wordList if (!isset($_SESSION["word"])) ?> איש תלוי <br><br> <img src="0.jpg"/> <form action="game.php" method="post" > <font size="+6"><?php echo "_ _ _ _ _"; ?></font><br><br> <input type="text" name="guessletter" /> <input type="submit" name="guess" value= "נחש"/> <br><br> <input type="submit" name="exit" value= "יציאה"/> </form> </body> </html> mod edit: meaningful title and put ending php code tag after the end of the code (edit2: unfortunately, the php code tag processing displays the foreign language characters as entities.) Quote Link to comment https://forums.phpfreaks.com/topic/266784-hangman-game-was-hello/ Share on other sites More sharing options...
hakimserwa Posted August 7, 2012 Share Posted August 7, 2012 why dont you make your example code English? Quote Link to comment https://forums.phpfreaks.com/topic/266784-hangman-game-was-hello/#findComment-1367615 Share on other sites More sharing options...
lilush Posted August 7, 2012 Author Share Posted August 7, 2012 ok- in english: <?php session_start(); if ( !isset($_SESSION['word']) ) { $wordList="table curtain printer guitar piano dragon pack bottle shirt picture"; } ?> <html> <body> <?php if(isset($_POST["exit"])) { ?> thank you and goodbey! <br> <form action="game.php" method="post"><br><br> <input type="submit" value="חזור למשחק" /> </form> <?php } elseif(isset($_POST["guessletter"])) { if(!isset($_SESSION["totalGuess"])) { $_SESSION["totalGuess"] = 0; } $totalGuess= 0; } $maxError=6; $wordList= rand(1,10); $pickedWord= $wordList if (!isset($_SESSION["word"])) ?> hanging man <br><br> <img src="0.jpg"/> <form action="game.php" method="post" > <font size="+6"><?php echo "_ _ _ _ _"; ?></font><br><br> <input type="text" name="guessletter" /> <input type="submit" name="guess" value= "guess"/> <br><br> <input type="submit" name="exit" value= "exit"/> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/266784-hangman-game-was-hello/#findComment-1367628 Share on other sites More sharing options...
scootstah Posted August 7, 2012 Share Posted August 7, 2012 Are you having trouble with something? Quote Link to comment https://forums.phpfreaks.com/topic/266784-hangman-game-was-hello/#findComment-1367635 Share on other sites More sharing options...
lilush Posted August 7, 2012 Author Share Posted August 7, 2012 yeah I don't know how to get the program to randomly pick one of the words in the word bank Quote Link to comment https://forums.phpfreaks.com/topic/266784-hangman-game-was-hello/#findComment-1367636 Share on other sites More sharing options...
scootstah Posted August 7, 2012 Share Posted August 7, 2012 First, you should make the word list an array - it just makes more sense. $wordList = array( 'table', 'curtain', 'printer', 'guitar', 'piano', 'dragon', 'pack', 'bottle', 'shirt', 'picture' ); Then, you can use array_rand to pick a random word. Quote Link to comment https://forums.phpfreaks.com/topic/266784-hangman-game-was-hello/#findComment-1367637 Share on other sites More sharing options...
lilush Posted August 7, 2012 Author Share Posted August 7, 2012 ok- I turned it could you tell me how I use array_rand()? I'm sorry I'm really new at this Quote Link to comment https://forums.phpfreaks.com/topic/266784-hangman-game-was-hello/#findComment-1367638 Share on other sites More sharing options...
Barand Posted August 7, 2012 Share Posted August 7, 2012 $wordlist = array( 'table' , 'curtain' , 'printer' , 'guitar' , 'piano' , 'dragon' , 'pack' , 'bottle' , 'shirt' , 'picture' ); for ($i = 0; $i < 10; ++$i) { $randomword = $wordlist[array_rand($wordlist)]; // get random word echo $randomword . '<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/266784-hangman-game-was-hello/#findComment-1367641 Share on other sites More sharing options...
Drummin Posted August 7, 2012 Share Posted August 7, 2012 Does it need the FOR loop? <?php $wordList = array( 'table', 'curtain', 'printer', 'guitar', 'piano', 'dragon', 'pack', 'bottle', 'shirt', 'picture' ); $wordkey=array_rand($wordList); $word=$wordList[$wordkey]; echo "$word"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/266784-hangman-game-was-hello/#findComment-1367642 Share on other sites More sharing options...
scootstah Posted August 7, 2012 Share Posted August 7, 2012 No, I think he was just demonstrating that it is in fact random. Quote Link to comment https://forums.phpfreaks.com/topic/266784-hangman-game-was-hello/#findComment-1367643 Share on other sites More sharing options...
Drummin Posted August 8, 2012 Share Posted August 8, 2012 For the fun of it. <?php session_start(); $wordList = array( 'table', 'curtain', 'printer', 'guitar', 'piano', 'dragon', 'pack', 'bottle', 'shirt', 'picture' ); if (!isset($_SESSION['word']) || isset($_POST['reset'])){ $wordkey=array_rand($wordList); $word=$wordList[$wordkey]; $_SESSION['word']=$word; $_SESSION["totalGuess"]=0; $_SESSION["matches"]=array(); }else{ $word=$_SESSION['word']; } $wordlength = strlen($word); $totalGuess = (!isset($_SESSION["totalGuess"])? 0 : $_SESSION["totalGuess"]); //create array of letters in word $word_array = str_split($word); //Check post against word_array if (isset($_POST['guess']) && $totalGuess<=$wordlength){ $letter = trim($_POST['guessletter']); $matchkey = (in_array($letter,$word_array) ? array_search($letter,$word_array) : ''); for($i=0;$i<=($wordlength-1);$i++){ if (isset($_SESSION["matches"][$matchkey]) && isset($_SESSION["matches"][$i]) && $_SESSION["matches"][$matchkey]==$_SESSION["matches"][$i]){ $_SESSION["matches"][$i]=$_SESSION["matches"][$matchkey]; }elseif($word_array[$i]==$letter){ $_SESSION["matches"][$i]="$letter"; } } $_SESSION["totalGuess"]++; header("location: game.php"); exit; } ksort($_SESSION["matches"]); //style display section $reset=""; if ($totalGuess==$wordlength || count($_SESSION["matches"])==$wordlength){ if (count($_SESSION["matches"])==$wordlength){ foreach($word_array as $key => $l){ if ($_SESSION["matches"][$key]!=$word_array[$key]){ $reset="<br />Reset for new game"; $color="#ff0000"; $fail= true; } } $color="#006600"; $reset=(!isset($fail) ? "<br /><span style=\"color:#006600;\">YOU WON</span><br />Reset for new game" : "<br />Reset for new game"); }else{ $color="#ff0000"; $reset="<br />Reset for new game"; } }else{ $color="#000000"; } //display section $display = "<span style=\"color:$color; font-size:28px;\">"; for($d=0;$d<=($wordlength-1);$d++){ $display .= (isset($_SESSION["matches"][$d]) ? "{$_SESSION["matches"][$d]}" : "_")." "; } $display .= "</span>.$reset"; $display .= "<br />$totalGuess out of $wordlength"; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>What's that word</title> </head> <body> <center> <form action="game.php" method="post" > <?php echo "$display"; ?><br /><br /> <input type="text" name="guessletter" /> <input type="submit" name="guess" value= "נחש"/> <br /><br /> <input type="submit" name="reset" value="יציאה"/> </form> </center> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/266784-hangman-game-was-hello/#findComment-1367716 Share on other sites More sharing options...
lilush Posted August 8, 2012 Author Share Posted August 8, 2012 WOW Thank you!!! How do I get the images of the hanging man to appear whenever someone makes a mistake? I have 8 images the first one is 0 mistake so it is displayd on the start and with each mistake a body part appears (image mistake 2, mistake 3 etc) Quote Link to comment https://forums.phpfreaks.com/topic/266784-hangman-game-was-hello/#findComment-1367722 Share on other sites More sharing options...
Drummin Posted August 8, 2012 Share Posted August 8, 2012 Can you make a static grid (table?) of your images? Really the way it is now, ANY missed letter is a fail. So code would need to be changed to use 8 errors to loose. Quote Link to comment https://forums.phpfreaks.com/topic/266784-hangman-game-was-hello/#findComment-1367728 Share on other sites More sharing options...
Drummin Posted August 8, 2012 Share Posted August 8, 2012 Something like this should work. <?php session_start(); $wordList = array( 'table', 'curtain', 'printer', 'guitar', 'piano', 'dragon', 'pack', 'bottle', 'shirt', 'picture' ); if (!isset($_SESSION['word']) || isset($_POST['reset'])){ $wordkey=array_rand($wordList); $word=$wordList[$wordkey]; $_SESSION['word']=$word; $_SESSION["totalGuess"]=0; $_SESSION["matches"]=array(); }else{ $word=$_SESSION['word']; } $wordlength = strlen($word); $totalGuess = (!isset($_SESSION["totalGuess"])? 0 : $_SESSION["totalGuess"]); //create array of letters in word $word_array = str_split($word); //Check post against word_array //if (isset($_POST['guess']) && $totalGuess<=$wordlength){ if (isset($_POST['guess'])){ $letter = trim($_POST['guessletter']); $matchkey = (in_array($letter,$word_array) ? array_search($letter,$word_array) : ''); for($i=0;$i<=($wordlength-1);$i++){ if (isset($_SESSION["matches"][$matchkey]) && isset($_SESSION["matches"][$i]) && $_SESSION["matches"][$matchkey]==$_SESSION["matches"][$i]){ $_SESSION["matches"][$i]=$_SESSION["matches"][$matchkey]; }elseif($word_array[$i]==$letter){ $_SESSION["matches"][$i]="$letter"; } } $_SESSION["totalGuess"]++; header("location: game.php"); exit; } ksort($_SESSION["matches"]); //style display section $reset=""; $miss_total = $_SESSION["totalGuess"] - count($_SESSION["matches"]); if ($miss_total>={ $color="#ff0000"; $reset="<br />Reset for new game"; }else{ if (count($_SESSION["matches"])==$wordlength){ foreach($word_array as $key => $l){ if ($_SESSION["matches"][$key]!=$word_array[$key]){ $reset="<br />Reset for new game"; $color="#ff0000"; $fail= true; } } $color="#006600"; $reset=(!isset($fail) ? "<br /><span style=\"color:#006600;\">YOU WON</span><br />Reset for new game" : "<br />Reset for new game"); }else{ $color="#000000"; } } //display section $man = "<div style=\"width:100%;\"> <div style=\"margin:10px auto; width:30%;\"> <div style=\"float:left; width:25%; text-align:center;\"> <table border=0 cellpadding=0 cellspacing=0 width=50%> <tr> <td>".($miss_total>=7 ? "<img src=\"images/man7.jpg\" border=\"0\" width=\"15\" height=\"15\" />" : " ")."</td> <td>".($miss_total>=8 ? "<img src=\"images/man8.jpg\" border=\"0\" width=\"15\" height=\"15\" />" : " ")."</td> </tr> <tr> <td>".($miss_total>=5 ? "<img src=\"images/man5.jpg\" border=\"0\" width=\"15\" height=\"15\" />" : " ")."</td> <td>".($miss_total>=6 ? "<img src=\"images/man6.jpg\" border=\"0\" width=\"15\" height=\"15\" />" : " ")."</td> </tr> <tr> <td>".($miss_total>=3 ? "<img src=\"images/man3.jpg\" border=\"0\" width=\"15\" height=\"15\" />" : " ")."</td> <td>".($miss_total>=4 ? "<img src=\"images/man4.jpg\" border=\"0\" width=\"15\" height=\"15\" />" : " ")."</td> </tr> <tr> <td>".($miss_total>=1 ? "<img src=\"images/man1.jpg\" border=\"0\" width=\"15\" height=\"15\" />" : " ")."</td> <td>".($miss_total>=2 ? "<img src=\"images/man2.jpg\" border=\"0\" width=\"15\" height=\"15\" />" : " ")."</td> </tr> </table> </div> <div style=\"float:left; width:50%; text-align:center;\">"; $display = "<span style=\"color:$color; font-size:28px;\">"; for($d=0;$d<=($wordlength-1);$d++){ $display .= (isset($_SESSION["matches"][$d]) ? "{$_SESSION["matches"][$d]}" : "_")." "; } $display .= "</span>.$reset"; //$display .= "</div></div>"; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>What's that word</title> </head> <body> <?php echo "$man"; ?> <form action="game.php" method="post" > <?php echo "$display"; ?><br /><br /> <input type="text" name="guessletter" /> <input type="submit" name="guess" value= "נחש"/> <br /><br /> <input type="submit" name="reset" value="יציאה"/> </form> </div> </div> <div style="float:left; width:25%;"></div> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/266784-hangman-game-was-hello/#findComment-1367733 Share on other sites More sharing options...
lilush Posted August 8, 2012 Author Share Posted August 8, 2012 Is there a way to use this $_SESSION["presentedTryNum"] += 1; and each time it counts a diffrent image would appear? and again- thank you so much for your help Quote Link to comment https://forums.phpfreaks.com/topic/266784-hangman-game-was-hello/#findComment-1367739 Share on other sites More sharing options...
Drummin Posted August 8, 2012 Share Posted August 8, 2012 You already have several sessions counting. $miss_total = $_SESSION["totalGuess"] - count($_SESSION["matches"]); So $_SESSION["totalGuess"] is going up with each answer and you would format the code much like I did using the $miss_total variable for showing each image. Quote Link to comment https://forums.phpfreaks.com/topic/266784-hangman-game-was-hello/#findComment-1367828 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.