Peoplebelievewhattheywanttobelieve! After changing the computer, transfer and save the information, which was previously written and played, without comments ...... Generate several random numbers and ask questions randomly. There are 10 windows command lines in total & lt ;? Php... "> <LINKhref =" http: // www. php10
Generate several random numbers and ask questions randomly. a total of 10 rounds
Windows command line
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 answer: {$ this-> _ answer} \ n ");
}
If ($ this-> _ level> 10 ){
$ Grades = array (
0 => 'topid ',
1 => 'topid ',
2 => 'topid ',
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 {$ grades [$ this-> _ accuracy]}! \ N ";
$ This-> _ display ("\ n 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], 'minimum number ');
If ($ len % 2 ){
$ Questions ['mid '] = array ($ arr [intval ($ len/2)], 'intermediate number ');
}
If ($ len> 3 ){
$ Questions ['second'] = array ($ arr [$ len-2], 'second number ');
$ Questions ['finally _ second'] = array ($ arr [1], 'second digit ');
}
Shuffle ($ questions );
$ Tmp = array_shift ($ questions );
$ This-> _ answer = $ tmp [0];
$ This-> _ display (join ("", $ this-> _ digitals), range (5, 1 ));
$ This-> _ display ("Enter {$ 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 );
}
}
}
?>