乱码乱a∨中文字幕,在线免费激情视频,亚洲欧美久久夜夜潮,国产在线网址

  1. <sub id="hjl7n"></sub>

    1. <sub id="hjl7n"></sub>

      <legend id="hjl7n"></legend>

      當(dāng)前位置:首頁 >  站長 >  編程技術(shù) >  正文

      PHP實(shí)現(xiàn)猜數(shù)游戲

       2021-01-02 14:04  來源: 腳本之家   我來投稿 撤稿糾錯(cuò)

        阿里云優(yōu)惠券 先領(lǐng)券再下單

      這篇文章主要為大家詳細(xì)介紹了PHP實(shí)現(xiàn)猜數(shù)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

      本文實(shí)例為大家分享了PHP實(shí)現(xiàn)猜數(shù)游戲的具體代碼,供大家參考,具體內(nèi)容如下

      猜數(shù)游戲有兩種玩法:

      第一種: 兩個(gè)人玩,一方出數(shù)字,一方猜。出數(shù)字的人要想好一個(gè)指定位數(shù)的數(shù),數(shù)字可重復(fù),不能讓猜的人知道。

      猜的人就可以開始猜。每猜一個(gè)數(shù),出數(shù)者就要說大過或小過出的數(shù)。

      第二種: 兩個(gè)人玩,一方出數(shù)字,一方猜。出數(shù)字的人要先想好一個(gè)沒有重復(fù)數(shù)字的4位數(shù),不能讓猜的人知道。猜的人就可以開始猜。每猜一個(gè)數(shù),出數(shù)者就要根據(jù)這個(gè)數(shù)字給出幾A幾B,其中A前面的數(shù)字表示位置正確的數(shù)的個(gè)數(shù),而B前的數(shù)字表示數(shù)字正確而位置不對(duì)的數(shù)的個(gè)數(shù)。如正確答案為5234,而猜的人猜5346,則是1A2B,其中有一個(gè)5的位置對(duì)了,記為1A,而3和4這兩個(gè)數(shù)字對(duì)了,而位置沒對(duì),因此記為2B,合起來就是1A2B。接著猜的人再根據(jù)出題者的幾A幾B繼續(xù)猜,直到猜中為止。

      下面是PHP代碼實(shí)現(xiàn):

      <?php
      // 標(biāo)準(zhǔn)輸入流和標(biāo)準(zhǔn)輸出流
      $stdin = null;
      $stdout = null;

      /**
      * 初始化 IO 流
      */
      function init() {
      global $stdin;
      global $stdout;

      $stdin = fopen('php://stdin', 'r');
      $stdout = fopen('php://stdout', 'w');
      }

      /**
      * 關(guān)閉 IO 流
      */
      function destroy() {
      global $stdin;
      global $stdout;

      if(is_resource($stdin)) {
      fclose($stdin);
      }
      if(is_resource($stdout)) {
      fclose($stdout);
      }
      }

      /**
      * 從命令行讀取一行數(shù)據(jù)
      */
      function read() {
      global $stdin;

      $line = fgets($stdin);
      return trim($line, PHP_EOL); //去除換行符
      }

      /**
      * 向命令行輸出一行數(shù)據(jù)
      */
      function write($line) {
      global $stdout;
      // 轉(zhuǎn)換編碼
      if(stripos(PHP_OS, 'winnt') !== false) {
      $line = iconv('UTF-8', 'GBK', $line);
      }
      fwrite($stdout, $line . PHP_EOL);
      }

      /**
      * 第一種玩法
      * @param $count 位數(shù)
      */
      function guess_the_number($count = 2) {
      // 隨機(jī)生成一個(gè) $count 位數(shù)
      $min = pow(10, $count - 1);
      $max = pow(10, $count) - 1;
      $number = rand($min, $max);
      init();
      while(1) {
      write(sprintf('Please input your number (%s-bit digit), q or quit exit: ', $count));
      $readStr = read();
      // exit program
      if($readStr == 'q' || $readStr == 'quit') {
      break;
      }
      $readInt = intval($readStr);
      if($readInt > $number) {
      write('大了');
      } else if($readInt < $number) {
      write('小了');
      } else {
      write('恭喜你,猜對(duì)了!');
      write('Input c continue to play');
      $readStr = read();
      if($readStr == 'c' || $readStr == 'continue') {
      $number = rand($min, $max);
      } else {
      break ;
      }
      }
      }
      destroy();
      }

      /**
      * 得到一個(gè)沒有重復(fù)數(shù)字的四位數(shù)
      */
      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); //再取 3 個(gè)數(shù)字
      $str = implode('', array_merge([$num], $subarr));
      return intval($str);
      }

      /**
      * 第二種玩法
      */
      function guess_the_number2() {
      $number = getRandNumber();
      $len = 4; //四位數(shù)
      init();
      while(1) {
      write(sprintf('Please input your number (%s-bit digit), q or quit exit: ', $len));
      $readStr = read();
      // exit program
      if($readStr == 'q' || $readStr == 'quit') {
      break;
      }
      $readInt = intval($readStr);
      if($readInt == $number) {
      write('恭喜你,猜對(duì)了!');
      write('Input c continue to play');
      $readStr = read();
      if($readStr == 'c' || $readStr == 'continue') {
      $number = getRandNumber();
      } else {
      break ;
      }
      } else {
      // 判斷 幾A幾B
      $readInt = str_pad($readInt, $len, '0', STR_PAD_LEFT); //不足四位的補(bǔ)足四位
      $number = strval($number);
      $readArr = str_split($readInt, 1); // cast to array
      $numArr = str_split($number, 1);
      $aval = 0; // 幾A
      $bval = 0; // 幾B
      for($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;
      }

      以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

      來源:腳本之家

      鏈接:https://www.jb51.net/article/203208.htm

      申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!

      相關(guān)標(biāo)簽
      php教程

      相關(guān)文章

      熱門排行

      信息推薦