猜数游戏有两种玩法:
第一种:两个人玩,一方出数字,一方猜。出数字的人要想好一个指定位数的数,数字可重复,不能让猜的人知道。
猜的人就可以开始猜。每猜一个数,出数者就要说大过或小过出的数。
第二种:两个人玩,一方出数字,一方猜。出数字的人要先想好一个没有重复数字的4位数,不能让猜的人知道。猜的
人就可以开始猜。每猜一个数,出数者就要根据这个数字给出几a几b,其中a前面的数字表示位置正确的数的个数,而
b前的数字表示数字正确而位置不对的数的个数。如正确答案为5234,而猜的人猜5346,则是1a2b,其中有一个5的位
置对了,记为1a,而3和4这两个数字对了,而位置没对,因此记为2b,合起来就是1a2b。接着猜的人再根据出题者的
几a几b继续猜,直到猜中为止。
下面是php代码实现:
<?php
// 标准输入流和标准输出流
$stdin = null;
$stdout = null;
/**
* 初始化 io 流
*/
function init() {
global $stdin;
global $stdout;
$stdin = fopen('php://stdin', 'r');
$stdout = fopen('php://stdout', 'w');
}
/**
* 关闭 io 流
*/
function destroy() {
global $stdin;
global $stdout;
if(is_resource($stdin)) {
fclose($stdin);
}
if(is_resource($stdout)) {
fclose($stdout);
}
}
/**
* 从命令行读取一行数据
*/
function read() {
global $stdin;
$line = fgets($stdin);
return trim($line, php_eol); //去除换行符
}
/**
* 向命令行输出一行数据
*/
function write($line) {
global $stdout;
// 转换编码
if(stripos(php_os, 'winnt') !== false) {
$line = iconv('utf-8', 'gbk', $line);
}
fwrite($stdout, $line . php_eol);
}
/**
* 第一种玩法
* @param $count 位数
*/
function guess_the_number($count = 2) {
// 随机生成一个 $count 位数
$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('恭喜你,猜对了!');
write('input c continue to play');
$readstr = read();
if($readstr == 'c' || $readstr == 'continue') {
$number = rand($min, $max);
} else {
break ;
}
}
}
destroy();
}
/**
* 得到一个没有重复数字的四位数
*/
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 个数字
$str = implode('', array_merge([$num], $subarr));
return intval($str);
}
/**
* 第二种玩法
*/
function guess_the_number2() {
$number = getrandnumber();
$len = 4; //四位数
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('恭喜你,猜对了!');
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); //不足四位的补足四位
$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;
}
以上就是php实现猜数游戏的代码实例分享的详细内容。
