PHP implementation of the guessing game code example sharing

Source: Internet
Author: User
Tags sprintf
There are two ways to play a game of guessing:

The first kind: Two people play, one side out of numbers, the other side guess. Figure out the number of people to think of a given digit number, the number can be repeated, can not let the guessing people know.

Guess the person can start to guess. Each guess a number, the number of people will say greater than or small out of the numbers.

The second kind: Two people play, one side out of numbers, the other side guess. The number of people should first think of a 4-digit number without repeating the number, can not let the guessing people know. Guess.

People can begin to guess. Each guess a number, the number of people will be based on this figure given a few a few B, where a before the number indicates the number of correct position, and

The number before B indicates the number of digits that are correct and are in the wrong position. If the correct answer is 5234, and the guessing person guesses 5346, then it is 1A2B, which has a 5 bit

The pair, recorded as 1 A, and 3 and 4 of the two numbers, and the position is not right, so the record is 2B, together is 1a2b. Then guess the person again according to the author's

A few A and a few B keep guessing until you guess.


Here is the PHP code implementation:

<?php//standard input stream and standard output stream $stdin = null, $stdout = null;/** * Initialize IO stream */function init () {global $stdin; global $stdout; $stdin = fopen (' Php://stdin ', ' R '); $stdout = fopen (' php://stdout ', ' W ');} /** * Close IO stream */function destroy () {global $stdin; global $stdout; if (Is_resource ($stdin)) {fclose ($stdin);} if (Is_resource ($stdout)) {fclose ($stdout);}} /** * Reads a row of data from the command line */function read () {global $stdin; $line = fgets ($stdin); return trim ($line, php_eol);//Remove line break}/** * output to command line Row Data */function Write ($line) {global $stdout;//Convert encoded if (Stripos (Php_os, ' Winnt ')!== false) {$line = Iconv (' UTF-8 ', ' GBK ', $ line);} Fwrite ($stdout, $line. PHP_EOL);}  /** * First Play * @param $count digits */function guess_the_number ($count = 2) {//randomly generate a $count number of bits $min = POW ($count-1); $max  = POW ($count)-1; $number = rand ($min, $max); init (); while (1) {Write (sprintf ('%s-bit digit), Q or quit exit: ', $count)); $readStr = read ();//Exit programif ($readStr = = ' Q ' | | $readStr = = ' quit ') {break;} $readInt = Intval ($reaDSTR), if ($readInt > $number) {write (' big '),} else if ($readInt < $number) {write (' small ');} else {write (' Congratulations, guess right! '); Write (' Input C continue to play '), $readStr = Read (), if ($readStr = = ' C ' | | $readStr = = ' Continue ') {$number = rand ($min, $m AX);} else {break;}}} Destroy ();} /** * Get a four-digit number without repeating number */function Getrandnumber () {$num = rand (1, 9); $array = Array_diff (Array (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) , [$num]); shuffle ($array); $subarr = Array_slice ($array, 0, 3); Take 3 more digits $str = implode ("', Array_merge ([$num], $subarr)); return intval ($STR);} /** * Second play */function Guess_the_number2 () {$number = Getrandnumber (); $len = 4;//four-digit init (); while (1) {Write (sprintf (' Plea SE input your number (%s-bit digit), Q or Quit exit: ', $len)); $readStr = read ();//Exit programif ($readStr = = ' Q ' | | $rea Dstr = = ' quit ') {break;} $readInt = Intval ($READSTR), if ($readInt = = $number) {Write (' Congratulations, guess right! '); Write (' Input C continue to play '), $readStr = Read (), if ($readStr = = ' C ' | | $readStr = = ' Continue ') {$number = Getrandnumber ();} else {break;}} else {//judge several a several b$readint = Str_pad ($readInt, $len, ' 0 ', str_pad_left);//four-bit top-up four-bit = Strval ($number); $readArr = str _split ($readInt, 1); Cast to Array$numarr = Str_split ($number, 1); $aval = 0; Several a$bval = 0; Several bfor ($i = 0; $i < $len; $i + +) {if ($readArr [$i] = = $NUMARR [$i]) {$aval ++;unset ($readArr [$i], $NUMARR [$i]);}} $bval = Count (Array_intersect ($READARR, $NUMARR)), Write (sprintf ('%SA%SB ', $aval, $bval));}} Destroy ();} if (Php_sapi = = ' cli ') {//Guess_the_number (1); Guess_the_number2 ();} else {echo ' please run under command line! '; Exit;}
Related Article

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.