石头,纸,剪刀游戏php [英] Rock, paper, scissors game php

查看:30
本文介绍了石头,纸,剪刀游戏php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在用 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';
        }
   }
}
?>

推荐答案

这是工作代码.第一页

<?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 页面代码

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

希望能帮到你.

这篇关于石头,纸,剪刀游戏php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆