The memory of the test of PHP small game

Source: Internet
Author: User
Tags foreach array join range trim usleep

Generate a few random numbers, random questions, a total of 10 rounds

Windows command-line effect diagram

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

$this->_accuracy++;

}else{

$this->_display (Join ("", $this->_digitals). \ n The answer is: {$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 ',

=> ' unbelievable ',

);

$score = $this->_accuracy*10. "%\t\t{$grades [$this->_accuracy]}!\n";

$this->_display ("\ n \ nthe correct answer rate:". $score);

return true;

}

return false;

}

Private Function _mk_question ($arr) {

Sort ($arr);

$len = sizeof ($arr);

$questions = Array ();

$questions [' max '] = Array ($arr [$len-1], ' Maximum number ');

$questions [' min '] = array ($arr [0], ' smallest number ');

if ($len% 2) {

$questions [' mid '] = Array ($arr [Intval ($len/2)], ' intermediate number ');

}

if ($len >3) {

$questions [' second '] = Array ($arr [$len-2], ' second largest number ');

$questions [' finally_second '] = Array ($arr [1], ' second small number ');

}

Shuffle ($questions);

$tmp = Array_shift ($questions);

$this->_answer = $tmp [0];

$this->_display (Join ("", $this->_digitals), Range (5, 1));

$this->_display ("Enter {$tmp [1]} that has just appeared");

}

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

}

}

}

?>







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.