2016-12-03 38 views
0

有人可以幫助,我的遊戲不會順序正確,我有無處不在的回聲線,所以我可以看序列。此時,它會從猜測表格(第二序列)跳轉到第一個表格(您設置遊戲的限制)。php果凍豆遊戲 - 不正確的排序

<? 
//$stage = 1; 
//$_SESSION['stage'] = 1; 
//if(!$_SESSION['stage']){$_SESSION['stage'] = 1;} 
if(!$_SESSION['stage']){$_SESSION['stage'] = $stage;} 


/* sequence control 
    $_SESSION['stage']++; 
    $_SESSION['stage'] = 1; 
    echo "stage at" . $_SESSION['stage']; 
*/ 

if($_SESSION['stage'] == 1){ 
    if(!$_SESSION['r']){ 
      $_SESSION['stage'] = 1; 
      firstForm(); 
      echo "<p>31</p>"; 
      //exit(); 
     }else{ 
     if($_POST['submit']){ 
      echo "<p>line 35</p>"; 
      if($_SESSION['stage'] == 1){ 
       echo "<p>line 37</p>"; 
       checkRange(); 
      }else{echo "<p>line 39</p>";$_SESSION['stage']++;} 
     } 
     if(!$_POST['submit']){ 
      echo "<p>42</p>"; 
      firstForm(); 
     } 
    } 
} 
if($_SESSION['stage'] >= 2){ 
    echo "48"; 
    if(!$_SESSION['r']){ 
      echo "49 stage at" . $_SESSION['stage']; 
      //guessForm(); 
     } 
    if(!$_POST['guess'] && $_POST['submit'] != "guess"){ 
     echo "<p>line 54</p>"; 
     if($_SESSION['stage'] == 2){ 
      echo "<p>line 56</p>"; 
      guessForm(); 
     }else{ 
      echo "<p>line 59</p>"; 
      checkGuess($_POST['guess']); 
     } 
    }else{ 
     echo "<p>line 63</p>"; 
     checkGuess($_POST['guess']); 
     if($_POST['guess']<$_SESSION['r']){ 
      echo "Guess was too low, try again."; 
      guessForm(); 
     }elseif($_POST['guess']>$_SESSION['r']){ 
      echo "Guess was too high, try again."; 
      guessForm(); 
     }else{ 
      echo "<h1>Correct Guess! Well done.</h1>"; 
      echo "<h2>It took you " . $_SESSION['counter'] . " guesses."; 
      restartForm(); 
      $_SESSION['r'] = null; 
      $_SESSION['counter'] = null; 
      $_SESSION['stage'] = 1; 
     } 
     echo "<p>line 77</p>"; 
     $_SESSION['stage']=2; 
     echo "<p>current Step: (".$_SESSION['stage'].")</p>"; 
    } 
    echo "<p>line 83</p>"; 
    $_SESSION['stage']=2; 
    echo "<p>current Step: (".$_SESSION['stage'].")</p>"; 
} 

echo "<p>current Step: (".$_SESSION['stage'].")</p>"; 


function checkRange(){ 
     if(!$_POST['jellyBeans']){ 
      echo "<h2>You did not enter a capacity.</h2>"; 
      firstForm(); 
      exit(); 
     }elseif(!is_numeric($_POST['jellyBeans'])){ 
      echo "<h2>Numbers only.</h2>"; 
      firstForm(); 
      exit(); 
     }elseif($_POST['jellyBeans'] <= 0){ 
      echo "<h2>Try a bigger number.</h2>"; 
      firstForm(); 
      exit(); 
     }elseif($_POST['jellyBeans'] > 10000){ 
      echo "<h2>Slow down partner, choose somthing smaller</h2>"; 
      firstForm(); 
      exit(); 
     }else{ 
      echo "<p>107</p>"; 
      echo "<p>random number is: " . $_SESSION['r'] = rand(0,$_POST['jellyBeans']) . "</P>"; 
      $_SESSION['stage']++; 
      echo "<p>current Step: (".$_SESSION['stage'].")</p>"; 
     } 
    } 

function firstForm(){ 
?> 
<form acton="<?=$_SERVER['php_self']?>" method="post"> 
    <p><label for="jellyBeans">How many jelly beans do you want to be in the game</label><input name="jellyBeans"value="<?=$_POST['jellyBeans']?>" ></p> 
    <p> 
     <button value="submit" name="submit" type="submit">Choose</button> 
    </p> 
    <p> 
     <button value="<? $_SESSION['stage'] = 1 ?>" name="submit" type="submit">Restart?</button> 
    </p> 
</form> 
<? } 

function guessForm(){ 
?> 
<form acton="<?=$_SERVER['php_self']?>" method="post"> 
    <p><label for="guess">Guess what random number was selected</label><input name="guess" type="text" ></p> 
    <p> 
     <button value="submit" name="submit" type="submit">Guess</button> 
    </p> 
    <p> 
     <button value="<? $_SESSION['stage'] = 1 ?>" name="submit" type="submit">Restart?</button> 
    </p> 
</form> 
<? } 

function checkGuess($val){ 
    echo var_dump($val); 
    if($val == ""){ 
     $error[] = "You did not enter a value."; 
    }else{ 
     if(!is_numeric($val)){ 
     $error[] = "The value you entered was not a number."; 
     } 
    } 
    if($error){ 
     foreach($error as $v){ 
      echo "<h2>".$v."</h2>"; 
     } 
     echo "<h3>Try again.</h3>"; 
     guessForm(); 
     exit(); 
    } 
} 

?> 

回答

0

只需看一眼..

你需要添加一個session_start();所以你的$ _SESSION的頁面加載之間記住。

附註:請在第一行使用完整的php標記<?php而不是<?,因爲它並非始終在所有服務器上啓用。

<?php 
session_start(); // ** Added 
// The Rest of your code //