2016-12-03 16 views
0

誰かが助けてくれますか、私のゲームは正しくシーケンスされていません。どこでもエコーラインxがありますので、シーケンスを見ることができます。この時点では、推測形式(2番目のシーケンス)から最初のフォーム(ゲームの制限を設定する)にジャンプします。ただ、一目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 //