当前位置:  开发笔记 > 开发工具 > 正文

PHP小游戏之考考记忆力

Peoplebelievewhattheywanttobelieve!换电脑了,转移、保存资料中这是以前写着玩的,没啥注释。。。。。。产生几个随机数字,随机提问,总共10轮windows命令行效果图<?php...">

  产生几个随机数字,随机提问,总共10轮

  windows命令行效果图

\

 

  

  substr(php_sapi_name(), 0, 3) !== &#39;cli&#39; AND die("Please run in cli mode");

  new Php_Cli_Game;

  class Php_Cli_Game{

  private $_digitals = array();

  private $_answer = &#39;&#39;;

  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("√ Good job!\n");

  $this->_accuracy++;

  }else{

  $this->_display(join(" ", $this->_digitals) . "\n答案是:{$this->_answer}\n\n");

  }

  if($this->_level > 10){

  $grades = array(

  0 => &#39;Stupid&#39;,

  1 => &#39;Stupid&#39;,

  2 => &#39;Stupid&#39;,

  3 => &#39;Disappointing&#39;,

  4 => &#39;Not so bad&#39;,

  5 => &#39;Not so bad&#39;,

  6 => &#39;Not bad&#39;,

  7 => &#39;Impossible&#39;,

  8 => &#39;Impossible&#39;,

  9 => &#39;Unbelievable&#39;,

  10 => &#39;Unbelievable&#39;,

  );

  $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[&#39;max&#39;] = array($arr[$len-1], &#39;最大数字&#39;);

  $questions[&#39;min&#39;] = array($arr[0], &#39;最小数字&#39;);

  if($len % 2){

  $questions[&#39;mid&#39;] = array($arr[intval($len/2)], &#39;中间数字&#39;);

  }

  if($len>3){

  $questions[&#39;second&#39;] = array($arr[$len-2], &#39;第二大数字&#39;);

  $questions[&#39;finally_second&#39;] = array($arr[1], &#39;第二小数字&#39;);

  }

  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 = &#39;&#39;, $steps = &#39;&#39;){

  $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);

  }

  }

  }

  ?>


推荐阅读
author-avatar
欢乐文艺女青年
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有