Guest User

Stackoverlow PHP Hangman help

a guest
Aug 13th, 2012
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.53 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. ?>
  5.  
  6. <html >
  7.      
  8.     <head>
  9.         <meta charset="utf-8" />
  10.     </head>
  11.    
  12.     <body onload="document.getElementById('HangManInput').focus()">
  13.  
  14.   <?php
  15.            
  16. if(isset($_SESSION['win_amount']) == false){
  17.     $win_amount = 0;
  18.     $_SESSION['win_amount'] = $win_amount;
  19. }
  20.            
  21. if (isset($_POST['send_letter']) && $_SESSION['gameEnded'] == false) {
  22.     if (isset($_POST['HangManLetter'])){
  23.             $win_amount = $_SESSION['win_amount'];
  24.             $error_amount = $_SESSION['error_amount'];
  25.             $unGuessedWord = $_SESSION['unGuessedWord'];
  26.             $theChosenWord = $_SESSION['theChosenWord'];
  27.             $wordGuessed = $_SESSION['wordGuessed'];
  28.        
  29.             if($error_amount < 7){
  30.            
  31.                 $guessWordArray[0] = mb_substr($unGuessedWord, 8, 1, "UTF-8");
  32.                 $guessWordArray[1] = mb_substr($unGuessedWord, 6, 1, "UTF-8");
  33.                 $guessWordArray[2] = mb_substr($unGuessedWord, 4, 1, "UTF-8");
  34.                 $guessWordArray[3] = mb_substr($unGuessedWord, 2, 1, "UTF-8");
  35.                 $guessWordArray[4] = mb_substr($unGuessedWord, 0, 1, "UTF-8");
  36.                 $letterOccured = false;
  37.            
  38.                 if(mb_substr($theChosenWord, 0, 1, "UTF-8") == $_POST['HangManLetter']){                    
  39.                     $guessWordArray[4] = mb_substr($theChosenWord, 0, 1, "UTF-8");
  40.                     $wordGuessed++;
  41.                     $_SESSION['wordGuessed'] = $wordGuessed;
  42.                     $letterOccured = true;
  43.                 }
  44.          
  45.                 if(mb_substr($theChosenWord, 1, 1, "UTF-8") == $_POST['HangManLetter']){            
  46.                     $guessWordArray[3] = mb_substr($theChosenWord, 1, 1, "UTF-8");
  47.                     $wordGuessed++;
  48.                     $_SESSION['wordGuessed'] = $wordGuessed;
  49.                     $letterOccured = true;
  50.                 }
  51.            
  52.                 if(mb_substr($theChosenWord, 2, 1, "UTF-8") == $_POST['HangManLetter']){
  53.                     $guessWordArray[2] = mb_substr($theChosenWord, 2, 1, "UTF-8");
  54.                     $wordGuessed++;
  55.                     $_SESSION['wordGuessed'] = $wordGuessed;
  56.                     $letterOccured = true;
  57.                 }
  58.            
  59.                 if(mb_substr($theChosenWord, 3, 1, "UTF-8") == $_POST['HangManLetter']){
  60.                     $guessWordArray[1] = mb_substr($theChosenWord, 3, 1, "UTF-8");
  61.                     $wordGuessed++;
  62.                     $_SESSION['wordGuessed'] = $wordGuessed;
  63.                     $letterOccured = true;
  64.                 }
  65.            
  66.                 if(mb_substr($theChosenWord, 4, 1, "UTF-8") == $_POST['HangManLetter']){
  67.                     $guessWordArray[0] = mb_substr($theChosenWord, 4, 1, "UTF-8");
  68.                     $wordGuessed++;
  69.                     $_SESSION['wordGuessed'] = $wordGuessed;
  70.                     $letterOccured = true;
  71.                 }
  72.            
  73.                 if($letterOccured == false){
  74.                     $error_amount++;
  75.                     $_SESSION['error_amount'] = $error_amount;
  76.                     if ($error_amount == 7) {
  77.                         header("Location: Lost.php");
  78.                         unset($_SESSION['theChosenWord']);
  79.                         unset($_SESSION['unGuessedWord']);
  80.                         $_SESSION['gameEnded'] = true;
  81.                         $_SESSION['unGuessedWord'] = $theChosenWord;
  82.                     }
  83.                 }
  84.            
  85.                 $unGuessedWord = "".$guessWordArray[4]." ".$guessWordArray[3]." ".
  86.                                 $guessWordArray[2]." ".$guessWordArray[1]." ".$guessWordArray[0]."";
  87.                 $_SESSION['unGuessedWord'] = $unGuessedWord;
  88.            
  89.                 if ($wordGuessed == 5){
  90.                     $win_amount++;            
  91.                     $_SESSION['win_amount'] = $win_amount;
  92.                     header("Location: Win.php");
  93.                     unset($_SESSION['theChosenWord']);
  94.                     unset($_SESSION['unGuessedWord']);
  95.                     $_SESSION['gameEnded'] = true;
  96.                     $_SESSION['unGuessedWord'] = $theChosenWord;
  97.                 }
  98.             }        
  99.            
  100.     }
  101.  }
  102.  
  103. else{
  104.     $wordsArray = $wordsArray = array('מעופף','לומדת','הכרזה','מלשין','מאפיה','חתונה','מחביא','מסתכל','תגובה','חתימה');
  105.     $numOfWord = rand(0,9);
  106.     $theChosenWord = $wordsArray[$numOfWord];
  107.     $error_amount = 0;
  108.     $wordGuessed = 0;
  109.     $_SESSION['wordGuessed'] = $wordGuessed;
  110.     $_SESSION['theChosenWord'] = $theChosenWord;
  111.     $_SESSION['error_amount'] = $error_amount;
  112.     $_SESSION['gameEnded'] = false;
  113.     $unGuessedWord = "_ _ _ _ _";
  114.     $_SESSION['unGuessedWord'] = $unGuessedWord;
  115. }
  116. ?>
  117.  
  118.        
  119.             <div>            <!-- למחוק לפני הגשה -->
  120.                 <?php echo $_SESSION['theChosenWord']; ?>
  121.             </div>
  122.        
  123.             <div style="position:relative; left:100px; border:1px; border-color:Black;
  124.                 border-style:solid; width:400px; height:340px; font-family:David;">
  125.                 <form action="" method="post" onsubmit="return true;">
  126.                    
  127.                     <div id="HangManTitle" style="font-size:32px; position:absolute; top:5px; width:200px; text-align:center; left:100px">
  128.                         האיש התלוי
  129.                     </div>
  130.                    
  131.                     <div style="font-size:22px; position:absolute; top:50px; text-align:center; width:200px; left:70px;">
  132.                         <input type="text" maxlength="1" size="1" id="HangManInput" name="HangManLetter"/>
  133.                         <input type="hidden" value="true" name="send_letter"/>
  134.                         :הכנס אות
  135.                         <br></br>  <input type="submit" value="נחש" name="send_letter_button" /> <br/><br />
  136.                         :המילה  <br></br>
  137.                         <?php echo $_SESSION['unGuessedWord']; ?>
  138.                     </div>
  139.                  
  140.                     <div id="HangManImage" style="position:absolute; top:170px; left:240px; border:2px;">
  141.                     <?php
  142.                    
  143.                     if($error_amount==0){
  144.                     ?>
  145.                         <div id="HangManImage" style="position:absolute;">
  146.                             <img src="0.jpg">
  147.                             <?php echo "\n".$_SESSION['error_amount'].":טעויות"; ?>
  148.                         </div>  
  149.                     <?php      
  150.                     }
  151.                    
  152.                     if($error_amount==1){
  153.                     ?>
  154.                         <div id="HangManImage1" style="position:absolute;">
  155.                             <img src="1.jpg">
  156.                             <?php echo "\n".$_SESSION['error_amount']. ":טעויות"; ?>
  157.                         </div>
  158.                     <?php      
  159.                     }
  160.                    
  161.                     if($error_amount==2){
  162.                     ?>
  163.                         <div id="HangManImage2" style="position:absolute;">
  164.                             <img src="2.jpg">
  165.                             <?php echo "\n".$_SESSION['error_amount']. ":טעויות"; ?>
  166.                         </div>
  167.                     <?php      
  168.                     }
  169.                    
  170.                     if($error_amount==3){
  171.                     ?>
  172.                         <div id="HangManImage3" style="position:absolute;">
  173.                             <img src="3.jpg">
  174.                             <?php echo "\n".$_SESSION['error_amount']. ":טעויות"; ?>
  175.                         </div>
  176.                     <?php      
  177.                     }
  178.                    
  179.                     if($error_amount==4){
  180.                     ?>
  181.                         <div id="HangManImage4" style="position:absolute;">
  182.                             <img src="4.jpg">
  183.                             <?php echo "\n".$_SESSION['error_amount']. ":טעויות"; ?>
  184.                         </div>
  185.                     <?php      
  186.                     }
  187.                    
  188.                     if($error_amount==5){
  189.                     ?>
  190.                         <div id="HangManImage5" style="position:absolute;">
  191.                             <img src="5.jpg">
  192.                             <?php echo "\n".$_SESSION['error_amount']. ":טעויות"; ?>
  193.                         </div>
  194.                     <?php      
  195.                     }
  196.                    
  197.                     if($error_amount==6){
  198.                     ?>
  199.                         <div id="HangManImage6" style="position:absolute;">
  200.                             <img src="6.jpg">
  201.                             <?php echo "\n".$_SESSION['error_amount']. ":טעויות"; ?>
  202.                         </div>
  203.                     <?php      
  204.                     }
  205.                    
  206.                     if($error_amount==7){
  207.                     ?>
  208.                         <div id="HangManImage7" style="position:absolute;">
  209.                             <img src="7.jpg">
  210.                             <?php echo "\n".$_SESSION['error_amount']. ":טעויות"; ?>
  211.                         </div>
  212.                     <?php      
  213.                     }
  214.                     ?>
  215.                    
  216.                     </div>
  217.                
  218.                 </form>
  219.                
  220.                 <button onclick="location.href='Exit_Page.php'" style="position:absolute; right:300px; top:300px;" action="Exit_Page.php" method="post">
  221.                     יציאה
  222.                 </button>
  223.             </div>
  224.            
  225.            
  226.         </body>
  227.    
  228. </html>
Advertisement
Add Comment
Please, Sign In to add comment