這篇文章主要介紹了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. "<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"> <p align="center">剪刀石頭布,你出什嗎?<br/> <input type="radio" name="what" value="剪刀"/>剪刀<br/> <input type="radio" name="what" value="石頭"/>石頭<br/> <input type="radio" name="what" value="布"/>布<br/> </p> <p align="center"> <input type="submit" name="sub" value="開始!" /> <input type="reset" name="" value="重設" /> </p> </form></body></html>
運行如下:
以上就是本文的全部內容,希望對大家的學習有所協助。