仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1689|回复: 18
打印 上一主题 下一主题

[学习教程] PHP教程之PHP小游戏之考考记忆力

[复制链接]
简单生活 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-16 00:22:37 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
如果你单纯是为了做网站赚钱,我想你还是别学php的好,去学ASP,JSP好了,毕竟它们有实力雄厚的公司去支持它们。      发生几个随机数字,随机发问,总共10轮
    windows号令行后果图
   

   
    <?php
    substr(php_sapi_name(), 0, 3) !== 'cli' AND die("Please run in cli mode");
    new Php_Cli_Game;
    class Php_Cli_Game{
    private $_digitals = array();
    private $_answer = '';
    private $_level = 0;
    private $_accuracy = 0;
    const _SIZE = 30;
    public function __construct(){
    $this->_level = 1;
    $this->_accuracy = 0;
    do{
    $this->_init();
    $str = trim(fgets(STDIN));
    if(in_array($str, array("q", "e")) $this->_check_answer($str)){
    break;
    }
    }while(true);
    }
    private function _init(){
    $this->_digitals = array_fill(0, self::_SIZE, " ");
    $range = array_rand(range(0, ++$this->_level * 5), $this->_level);
    $tmp=array();
    foreach($range AS $k => $v){
    do{
    $rnd = rand(0, self::_SIZE-1);
    }while($this->_digitals[$rnd] !== " ");
    $tmp[] = $this->_digitals[$rnd] = $v;
    }
    $this->_mk_question($tmp);
    }
    private function _check_answer($answer){
    if($answer === trim($this->_answer)){
    $this->_display("&radic; Good job!\n");
    $this->_accuracy++;
    }else{
    $this->_display(join(" ", $this->_digitals) . "\n谜底是:{$this->_answer}\n\n");
    }
    if($this->_level > 10){
    $grades = array(
    0 => 'Stupid',
    1 => 'Stupid',
    2 => 'Stupid',
    3 => 'Disappointing',
    4 => 'Not so bad',
    5 => 'Not so bad',
    6 => 'Not bad',
    7 => 'Impossible',
    8 => 'Impossible',
    9 => 'Unbelievable',
    10 => 'Unbelievable',
    );
    $score = $this->_accuracy*10 . "%\t\t{$grades[$this->_accuracy]}!\n";
    $this->_display("\n\n答题准确率:" . $score);
    return true;
    }
    return false;
    }
    private function _mk_question($arr){
    sort($arr);
    $len = sizeof($arr);
    $questions = array();
    $questions['max'] = array($arr[$len-1], '最大数字');
    $questions['min'] = array($arr[0], '最小数字');
    if($len % 2){
    $questions['mid'] = array($arr[intval($len/2)], '两头数字');
    }
    if($len>3){
    $questions['second'] = array($arr[$len-2], '第二大数字');
    $questions['finally_second'] = array($arr[1], '第二小数字');
    }
    shuffle($questions);
    $tmp = array_shift($questions);
    $this->_answer = $tmp[0];
    $this->_display(join(" ", $this->_digitals), range(5, 1));
    $this->_display("输出方才呈现过的{$tmp[1]}:");
    }
    private function _display($str = '', $steps = ''){
    $len = strlen($str);
    if(is_array($steps)){
    foreach($steps AS $k => $v){
    fwrite(STDOUT, $str . "\r");
    usleep(ceil(1000000/($k+1)));
    fwrite(STDOUT, str_repeat(" ", $len) . "\r");
    usleep(ceil(1000000/$this->_level));
    if($k==$this->_level-1){
    break;
    }
    }
    fwrite(STDOUT, "\n");
    }
    else{
    fwrite(STDOUT, $str);
    }
    }
    }
    ?>
如果你单纯是为了做网站赚钱,我想你还是别学php的好,去学ASP,JSP好了,毕竟它们有实力雄厚的公司去支持它们。
简单生活 该用户已被删除
沙发
 楼主| 发表于 2015-3-5 15:45:42 | 显示全部楼层
做为1门年轻的语言,php一直很努力。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-22 10:35

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表