Home >php教程 >PHP源码 >石头剪刀布的php非循环写法

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

PHP中文网
PHP中文网Original
2016-05-25 17:00:031501browse

跳至

<?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 == &#39;y&#39;){
			rps();
		}else if($yorn == &#39;n&#39;){
			echo "Bye\n";
		}else{
			echo "全当是再来一次了,再来!\n";
			rps();
		}
	}


}

echo "开始猜拳游戏\n";
rps();

                   

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn