douye4254 2016-11-15 04:51
浏览 29
已采纳

摇滚,纸,剪刀游戏php

So, I am creating a Rock, Paper, Scissor game in php. I am creating two webpages. The first webpage will contain three radio buttons for rock, paper, scissor and one submit button. The first page will send the information to the second page. The second page is the computer. The computer will randomly choose between rock, paper, scissor.

This is what I have right now. It is just not sending the information in the right way.

FirstPage:

<?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="button" name="user_choice" value="Submit" title="Submit" /> <br /><br />
          
            </form> ';
          }
?>

SecondPage:

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

</div>
  • 写回答

1条回答 默认 最新

  • dqq3623 2016-11-15 05:07
    关注

    Here is working code. 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> ';
              }
    ?>
    

    game.php page code

    <?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';
            }
       }
    }
    ?>
    

    Hope it will help you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?