2016-11-15 14 views
-5

だから、私はPHPでロック、ペーパー、シザーゲームを作成しています。私は2つのWebページを作成しています。最初のWebページには、ロック、ペーパー、はさみ、および送信ボタンの3つのラジオボタンが含まれています。最初のページは2番目のページに情報を送信します。 2番目のページはコンピュータです。コンピュータは、岩、紙、はさみの中からランダムに選択します。PHPのゲームロック、紙、はさみ

これは私が今持っているものです。それは正しい方法で情報を送信していないだけです。

First page: 
 
<?php 
 
session_start(); //session start 
 
    if(!isset($_SESSION['username']))  //if session not found redirect to homepage 
 
    { 
 
    header('location:login.php'); 
 
    } 
 
    else{ 
 
     echo '<form action="game.php" method="post" /> 
 
      <input type="radio" name="user_choice" value="Rock" title="Rock" />Rock <br /><br /> 
 
      <input type="radio" name="user_choice" value="Paper" title="Paper" />Paper <br /><br /> 
 
      <input type="radio" name="user_choice" value="Scissors" title="Scissors" />Scissors <br /><br /> 
 
      <input type="submit" name="form_submit" value="submit"/> 
 
      </form> '; 
 
      } 
 
?> 
 

 
Second Page: 
 
<?php 
 
session_start(); //session start 
 
//if session not found redirect to homepage 
 
if(!isset($_SESSION['username'])) { 
 
    header('location:login.php'); 
 
} elseif { 
 
    if($_POST['user_choice']) { 
 
     $user_choice = $_POST['user_choice']; 
 
     $Choosefrom= array('Rock', 'Paper', 'Scissors'); 
 
     $Choice= rand(0,2); 
 
     $Computer=$Choosefrom[$Choice]; 
 
     if($user_choice == $Computer) { 
 
      echo 'Player: '.$user_choice.' CPU: '.$Computer.'. Result: Win'; 
 
     } else { 
 
      echo 'Player: '.$user_choice.' CPU: '.$Computer.'. Result: Lose'; 
 
     } 
 
    } 
 
} 
 
?>

+5

私はhttp://stackoverflow.com/questions/同じ質問を考えています40602274/rock-paper-scissors-game-php –

+0

それは同じではありません。それを確認してください – AAV

+1

2つの質問では何が違うのですか? –

答えて

1

解決ゲームのロジック、これにしようと、(ロジックたぶん参考)

<html> 
     <body> 
      <?php 
       error_reporting(E_ALL); 
       session_start(); 

       $_SESSION['username'] = true;// to bypass Login page (remove it once you test it) 

       if(!isset($_POST['user_choice'])){ 
        if(!isset($_SESSION['username'])){ 
         header('location:login.php'); 
        } 
        else{ 
         echo "First page:"; 
         $_SESSION['secondPage'] = true; 
         echo '<form action=htmlspecialchars($_SERVER["PHP_SELF"]); method="post" /> 
         <input type="radio" name="user_choice" value="Rock" title="Rock" />Rock <br /><br /> 
         <input type="radio" name="user_choice" value="Paper" title="Paper" />Paper <br /><br /> 
         <input type="radio" name="user_choice" value="Scissors" title="Scissors" />Scissors <br /><br /> 
         <input type="submit" name="form_submit" value="submit"/> 
         </form> '; 
        } 
       } 
      ?> 
      <?php 
       if(!isset($_SESSION['username'])) { 
        header('location:login.php'); 
       } else { 
        if(isset($_POST['user_choice'])) { 
         echo "Second Page:<br><br>"; 
         $CPUChoice = array('Rock', 'Paper', 'Scissors'); 
         shuffle($CPUChoice); 
         //echo "CPU Select". $CPUChoice[0];exit; 

         $CPU = $CPUChoice[0]; 
         $User = $_POST['user_choice']; 

         echo 'Player: '.$User.' <br>CPU: '.$CPU; 

         if($User === $CPU){ 
          echo '<br>Result: Tie!'; 
         } 
         else if($User === "Rock"){ 
          if($CPU === "Scissors") { 
           echo '<br>Result: User wins'; 
          } else { 
           echo '<br>Result: CPU wins'; 
          } 
         } 
         else if($User === "Paper") { 
          if($CPU === "Rock") { 
           echo '<br>Result: User wins'; 
          }else { 
           if($CPU === "Scissors") { 
            echo '<br>Result: Computer wins'; 
           } 
          } 
         } 
         else if($User === "Scissors") { 
          if($CPU === "Rock") { 
           echo '<br>Result: CPU wins'; 
          } else { 
           if($CPU === "Paper") { 
            echo '<br>Result: User wins'; 
           } 
          } 
         } 
        } 
       } 
      ?> 
     </body> 
    </html> 
+0

ありがとうございました! – AAV

関連する問題