- //index.php
- <html>
- <meta http-equiv="content-type" content="text/html" />
- <head>
- <title>猜情寻</title>
- </head>
- <body>
- <form method="post" action="play.php">
- <div align="center">剪刀石头布,你出什么?<input type="text" name="what" value="" />* 剪刀、石头、布</div>
- <p align="center"><input type="submit" name="sub" value="开始!" /><input type="reset" name="" value="重置" /></p>
- </form>
- </body>
- </html>
- //php代码
- <?php
- 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 $computer. "\\n";
- $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 "你赢咯~";
- }
- }
- ?>
- //该片段来自于http://www.codesnippet.cn/detail/030420149211.html
来源: http://www.codesnippet.cn/detail/030420149211.html