欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

石头剪刀布的php非循环写法

程序员文章站 2022-04-05 18:27:51
...
跳至
<?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();