您好,欢迎访问一九零五行业门户网

PHP小游戏之考考记忆力

产生几个随机数字,随机提问,总共10轮
windows命令行效果图
_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 => '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);
}
}
}
?>
其它类似信息

推荐信息