php教程 php手册 PHP小游戏之考考记忆力

PHP小游戏之考考记忆力

Jun 21, 2016 am 08:52 AM
array gt quot this

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

  windows命令行效果图

 

  

  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("√ 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);

  }

  }

  }

  ?>



본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

화웨이 GT3 Pro와 GT4의 차이점은 무엇입니까? 화웨이 GT3 Pro와 GT4의 차이점은 무엇입니까? Dec 29, 2023 pm 02:27 PM

화웨이 GT3 Pro와 GT4의 차이점은 무엇입니까?

수정: Windows 11에서 캡처 도구가 작동하지 않음 수정: Windows 11에서 캡처 도구가 작동하지 않음 Aug 24, 2023 am 09:48 AM

수정: Windows 11에서 캡처 도구가 작동하지 않음

iPhone에서 App Store 오류에 연결할 수 없는 문제를 해결하는 방법 iPhone에서 App Store 오류에 연결할 수 없는 문제를 해결하는 방법 Jul 29, 2023 am 08:22 AM

iPhone에서 App Store 오류에 연결할 수 없는 문제를 해결하는 방법

C#에서 Array.Sort 함수를 사용하여 배열 정렬 C#에서 Array.Sort 함수를 사용하여 배열 정렬 Nov 18, 2023 am 10:37 AM

C#에서 Array.Sort 함수를 사용하여 배열 정렬

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决

PHP array_merge_recursive() 함수를 사용하는 간단하고 명확한 방법 PHP array_merge_recursive() 함수를 사용하는 간단하고 명확한 방법 Jun 27, 2023 pm 01:48 PM

PHP array_merge_recursive() 함수를 사용하는 간단하고 명확한 방법

PHP에서 array_combine 함수를 사용하여 두 배열을 연관 배열로 결합하는 방법 PHP에서 array_combine 함수를 사용하여 두 배열을 연관 배열로 결합하는 방법 Jun 26, 2023 pm 01:41 PM

PHP에서 array_combine 함수를 사용하여 두 배열을 연관 배열로 결합하는 방법

watch4pro가 더 나은가요, 아니면 gt인가요? watch4pro가 더 나은가요, 아니면 gt인가요? Sep 26, 2023 pm 02:45 PM

watch4pro가 더 나은가요, 아니면 gt인가요?

See all articles