echo"<a href='?type=1'>剪刀</a> ";
echo"<a href='?type=2'>石头</a> ";
echo"<a href='?type=3'>布</a> ";
echo"<a href='index.php'>耍赖</a>";
$types = [1=>'剪刀',2=>'石头',3=>'布',4=>'不出'];
$typeNum = 4;
$randNum = rand(1,3);
if(isset($_GET['type'])){
//设置上下限
$typeNum = $_GET['type'] > 3 ? 3 : $_GET['type'];
$typeNum = $_GET['type'] < 1 ? 1 : $_GET['type'];
}
echo '<br/>';
echo '你:'.$types[$typeNum];
echo '<br/>';
echo '系统:'.$types[$randNum];
echo '<br/>';
echo '结果:'.Win($randNum,intval($typeNum));
// 1>3>2>1>3
function Win($randNum,$type)
{
if($type === 4) return '你耍赖';
$wins = [1=>2,2=>3,3=>1];
if($wins[$type] === $randNum){
return '你输了';
}else if($randNum === $type){
return '平局';
}else{
return '你赢了';
}
}
以前刚刚学编程的时候,用C语言写过一个这样的猜拳小游戏!如今从事PHP,闲来无事把C翻译过来了!