本文实例讲述了PHP实现的网页版剪刀石头布游戏。分享给大家供大家参考,具体如下:
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. "
"; $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 "你赢咯~"; } } ?>剪刀石头布
dio" name="what" value="剪刀"/>剪刀
dio" name="what" value="石头"/>石头
dio" name="what" value="布"/>布
"; $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 "你赢咯~"; } } ?>
dio" name="what" value="石头"/>石头
dio" name="what" value="布"/>布