Home > php教程 > PHP源码 > php剪刀石头布编程思路

php剪刀石头布编程思路

PHP中文网
Release: 2016-05-25 16:59:58
Original
2566 people have browsed it

1. [代码][PHP]代码 

<?php
/*
本代码由python视频培训班黄哥所写。
python核心编程6-14习题,用php写一遍。
在linux下终端运行 php test.php
本代码在mac下测试运行无误。
总计:这个代码是根据本人所写python代码修改过来的
学会一种编程语言,再学第二种,就很容易,为啥?
编程思路是一样的。
*/
$my_array = array("石头","剪刀","布");
$guize = array(array("石头","剪刀"),array("剪刀","布"),array("布","石头"));
//上面2个变量定义一个需要输入的数组,和一个获胜规则的二维数组
// var_dump($guize);
$rand_keys = array_rand($my_array);
$computer = $my_array[$rand_keys];
//取数组中随机值

echo $computer . "\n";




// echo $person;
while (True)
{  
    echo "请输入: 石头  剪刀  布\n";
    $person = trim(fgets(STDIN)) ;
    $input = array($computer,$person);
    //将输入的$person和电脑随机产生的值构造一个数组
    //再判断在不在获胜规则数组中

    if (!(in_array($person,$my_array)))
    {
       echo "只能输入&#39;剪刀、石头,布,请重新输入&#39;";
       continue;

    }
   

    if ($computer == $person )
    {
        echo "平手\n";
        
    }
    else if (in_array($input,$guize)) {
        echo "电脑胜\n";
        
    }
    else
    {
      echo "人获胜\n";
      break;
    }
}
?>
Copy after login

                   

                   

Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template