当前位置: 移动技术网 > IT编程>开发语言>PHP > php实现的网页版剪刀石头布游戏示例

php实现的网页版剪刀石头布游戏示例

2017年12月12日  | 移动技术网IT编程  | 我要评论

本文实例讲述了php实现的网页版剪刀石头布游戏。分享给大家供大家参考,具体如下:

<?php
/*
 * created on 2016-11-25
 *
 */
 if (isset($_post['sub'])) {
  $what = $_post['what'];
  //需要输入的数组
  $my_array = array("剪刀","石头","布");
  //获胜规则
  $guize = array(array("剪刀","布"),array("布","石头"),array("石头","剪刀"));
  //取数组中的随机值
  $rand_keys = array_rand($my_array);
  $computer = $my_array[$rand_keys];
  echo "你的".$what."vs ".$computer. "<br/>";
    $input = array($computer,$what);
    //将输入的what和电脑随机产生的值构造成一个数组,再判断在不在获胜规则中
    if (!(in_array($what,$my_array))) {
      echo "请输入 剪刀、石头、布";
      header("location:index.php");
    }
    if ($computer == $what) {
      echo "噢,平手";
    }elseif (in_array($input,$guize)) {
      echo "电脑胜";
    }else {
      echo "你赢咯~";
    }
 }
?>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"
"http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>剪刀石头布</title>
</head>
<body>
 <form action="" method="post">
 <div align="center">剪刀石头布,你出什么?<br/>
 <input type="radio" name="what" value="剪刀"/>剪刀<br/>
 <input type="radio" name="what" value="石头"/>石头<br/>
 <input type="radio" name="what" value="布"/>布<br/>
 </div>
 <p align="center">
 <input type="submit" name="sub" value="开始!" />
 <input type="reset" name="" value="重置" />
 </p>
 </form>
</body>
</html>

运行效果图如下:

更多关于php相关内容感兴趣的读者可查看本站专题:《php数据结构与算法教程》、《php数组(array)操作技巧大全》、《php字符串(string)用法总结》、《php程序设计算法总结》、《php数学运算技巧总结》、《php排序算法总结》、《php常用遍历算法与技巧总结》、《php正则表达式用法总结》、《php运算与运算符用法总结》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网