PHP-如何获取;所有两位数字均为“数字”;它们的加法给出了一个特定的数字
我创建了一个小智力游戏,需要猜测1到18之间的数字,这两个数字的组合给出了这个数字,例如:PHP-如何获取;所有两位数字均为“数字”;它们的加法给出了一个特定的数字,php,loops,Php,Loops,我创建了一个小智力游戏,需要猜测1到18之间的数字,这两个数字的组合给出了这个数字,例如: 1: 10 (1+0 = 1) 2: 11 (1+1 = 2) 3: 12 (1+2 = 3), 21 (2+1 = 3) 4: 22 (2+2 = 4), 31 (3+1= 4), 13 (1+3 = 4) . . . . . . 如何获取以18结尾的数字列表中的每个数字?如果您只是想创建一个游戏,允许用户输入一个数字,并将其与计算机生成的数字进行比较 for($target = 1; $target
1: 10 (1+0 = 1)
2: 11 (1+1 = 2)
3: 12 (1+2 = 3), 21 (2+1 = 3)
4: 22 (2+2 = 4), 31 (3+1= 4), 13 (1+3 = 4)
.
.
.
.
.
.
如何获取以18结尾的数字列表中的每个数字?如果您只是想创建一个游戏,允许用户输入一个数字,并将其与计算机生成的数字进行比较
for($target = 1; $target < 19; $target++) {
echo "$target: ";
for($i = 10; $i<100; $i++) {
$parts = str_split((string) $i);
if (array_sum($parts) == $target) {
echo "$i ";
}
}
echo "\n";
}
<?php
function sum($carry, $num){
$carry += $num;
return $carry;
}
function game($computer, $guess){
if($computer === array_reduce(str_split($guess), 'sum')){
return 'win';
}
return 'lose';
}
// force computer to choose 9
$computer = 9; $guess = 18;
echo game($computer, $guess);
// random 1-18 computer choice - will show win when computer picks 4
$computer = rand(1, 18); $guess = 22;
echo game($computer, $guess);
?>
你也可以这样做:
<?php
class GuessingGame{
public $computerFunc;
public function __construct($computerFunc = null){
$this->computerFunc = $computerFunc;
}
public function guess($guess){
$user = array_reduce(str_split($guess), function($carry, $num){
$carry += $num;
return $carry;
});
if(call_user_func($this->computerFunc) === $user){
return true;
}
return false;
}
}
function computerFunc(){
return rand(1, 18);
}
$game = new GuessingGame('computerFunc');
$test = $game->guess(22) ? 'win' : 'lose';
function guarantee(){
return 9;
}
$game->computerFunc = 'guarantee';
$test2 = $game->guess(18) ? 'win' : 'lose';
$test .= ' ----- '.$test2 ;
echo $test;
?>
要创建这种类型的集合,可以使用嵌套的for循环,每个循环从零迭代到最大值,在本例中为9,因为您希望得到的所有加法对都是两位数
for ($x = 0; $x <= 9; $x++) {
for ($y = 0; $y <= 9; $y++) {
$result[$x + $y][] = $x . $y;
}
}
使用多个循环是对处理能力的浪费,因为它将执行不必要/无用的迭代。对于每一个游戏,您将确定一个随机数,供您的玩家“处理”。使用简单减法的单循环确定所有正确答案。如果每次都要生成此查找数组,请不要使用暴力使用单循环和数学逻辑。(实际上,您可以/应该在没有任何明显内存损失的情况下硬编码整个查找数组;我假设这是一个学术/个人编码挑战。) 代码:() p、 如果你想构建完整的查找数组,使用@Don'tPanic的精益技术(没有浪费的迭代)。然后将结果硬编码到脚本中——不再需要生成正确的结果
for ($x = 1; $x <= 9; ++$x) {
for ($y = 0; $y <= 9; ++$y) {
$result[$x + $y][] = "$x$y";
}
}
var_export($result);
对于($x=1;$x Hello Welcome),请阅读以下文章,这可能会帮助您获得所需的答案:我不太理解这个示例。右侧的数字是如何选取的?是计算机在猜测还是播放器?“两个数字的组合”-那么是什么两个数字加在一起形成这个数字呢?我很困惑。另外,你需要帮助的是什么-我想我不太理解“如何获得以18结尾的数字列表中的每个数字”同样。请编辑您的问题以澄清这些细节,以便更多的人能够帮助您。Hello@Toastrackenigma,计算机给玩家一个数字,例如:4,并说:输入所有一个或两个数字的数字,他们的数字加起来就是数字4。因此玩家应该思考并输入:22(2+2=4)、13(1+3=4)、31(3+1=4)哦,我现在明白了-这很酷!因此,本质上,您要做的是使用PHP为编号18
生成所有这些数字-例如,它应该返回99
?
foreach ($result as $sum => $addends) {
echo "$sum: " . implode(' ', $addends) . "\n";
}
$random_target = rand(1,18);
echo "Find all two digit numbers whose individual digits add up to: " , $random_target , "\n\n";
for ($x = 1; $x < 10; ++$x) { // the tens digit
if (($diff = $random_target - $x) >= 0 && $diff < 10 ) { // if ones digit is only "1 digit" it qualifies
$result[] = "$x$diff";
}
}
var_export($result);
array (
0 => '49',
1 => '58',
2 => '67',
3 => '76',
4 => '85',
5 => '94',
)
for ($x = 1; $x <= 9; ++$x) {
for ($y = 0; $y <= 9; ++$y) {
$result[$x + $y][] = "$x$y";
}
}
var_export($result);