<?php /** * Rock Paper Scissors * by inuxor */ function rps() { $rps = array("石头", "剪刀", "布"); $keys = array_keys($rps); shuffle($keys); $computer = $keys[0]; echo "请输入: 石头 剪刀 布\n"; $keyin = trim(fgets(STDIN)); $person = array_search($keyin, $rps); if($person === false){ echo "输入错误!!\n"; rps(); }else{ echo "电脑出的是:" . $rps[$computer] . "\n"; $action = $computer - $person; if($action == 0){ echo "平局\n"; }else if($action == -2 || $action == 1){ echo "你赢\n"; }else{ echo "电脑赢\n"; } echo "再来一次?(y/n)\n"; $yorn = trim(fgets(STDIN)); if($yorn == 'y'){ rps(); }else if($yorn == 'n'){ echo "Bye\n"; }else{ echo "全当是再来一次了,再来!\n"; rps(); } } } echo "开始猜拳游戏\n"; rps();