[英]Array with the same multiple values
I coded this snippet: 我编写了以下代码段:
$a = array('X', 'X', 'X', 'O', 'O', 'O');
$rk = array_rand($a, 3);
$l = $a[$rk[0]].$a[$rk[1]].$a[$rk[2]];
if($l == 'OOO' || $l == 'XXX'){
echo $l . 'Winner';
} else {
echo = $l . 'Loser';
}
In the array you will notice multiples of the same values this is because three are chosen randomly and if matched you win (its a basic game). 在数组中,您会注意到相同值的倍数,这是因为随机选择了三个,并且如果匹配,您将获胜(这是基本游戏)。
My question is how can it be coded without having to add multiples of the same value in the array? 我的问题是,如何在不必在数组中添加相同值的倍数的情况下进行编码?
Update: Answer to Yanick Rochon question 更新:回答Yanick Rochon问题
as it stands the array is: 就目前而言,数组是:
$a = array('X', 'X', 'X', 'O', 'O', 'O');
is is possible somehow to have it as just 有可能以某种方式拥有它
$a = array('X', 'O');
and still have 3 values returned? 仍然有3个值返回?
With a basic rand: 基本兰特:
<?php
$a = array('X', 'O', 'R');
$size = count($a)-1;
$l = $a[rand(0,$size)].$a[rand(0,$size)].$a[rand(0,$size)];
if($l == 'OOO' || $l == 'XXX'){
echo $l . ' Winner';
} else {
echo $l . ' Loser';
}
?>
Version with XXX or OOO or RRR win: 带有XXX或OOO或RRR版本的版本:
<?php
$a = array('X', 'O', 'R');
$size = count($a)-1;
$l = $a[rand(0,$size)].$a[rand(0,$size)].$a[rand(0,$size)];
if($l === 'OOO' || $l === 'XXX' || $l === 'RRR'){
echo $l . ' Winner';
} else {
echo $l . ' Loser';
}
?>
To procedurally create an entire program, here is a code snippet. 要按程序创建整个程序,请使用以下代码段。 You can test it here .
您可以在这里进行测试。
function randomArray($multiple, $length = 4) {
if ($length <= $multiple) {
throw new Exception('Length must be greater than multiple');
}
// define possible characters
$possible = 'ABCEFGHIJKLMNPQRSTUVWXYZ';
$value = str_repeat(substr($possible, mt_rand(0, strlen($possible)-1), 1), $multiple);
$i = strlen($value);
// add random characters to $value until $length is reached
while ($i < $length) {
// pick a random character from the possible ones
$char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
// we don't want this character if it's already in the string
if (!strstr($value, $char)) {
$value .= $char;
$i++;
}
}
// done!
$value = str_split($value);
shuffle($value);
return $value;
}
// return a random selection from the given array
// if $allowRepick is true, then the same array value may be picked multiple times
function arrayPick($arr, $count, $allowRepick = false) {
$value = array();
if ($allowRepick) {
for ($i = 0; $i < $count; ++$i) {
array_push($value, $arr[mt_rand(0, count($arr) - 1)]);
}
} else {
shuffle($arr);
$value = array_slice($arr, 0, $count);
}
return $value;
}
// generate an array with 4 values, from which 3 are the same
$values = randomArray(3, 4);
// pick any 3 distinct values from the array
$choices = arrayPick($values, 3, false);
// convert to strings
$valuesStr = implode('', $values);
$choicesStr = implode('', $choices);
echo 'Value : ' . $valuesStr . "\n";
//echo 'Choice : ' . $choicesStr . "\n";
if (preg_match('/^(.)\1*$/', $choicesStr)) {
echo $choicesStr . ' : Winner!';
} else {
echo $choicesStr . ' : Loser!';
}
Note : the function randomArray
will fail if $length - $multiple > count($possible)
. 注意 :如果
$length - $multiple > count($possible)
randomArray
$length - $multiple > count($possible)
函数randomArray
将失败。 For example, this call randomArray(1, 27)
will fail, and the script will run indefinitely. 例如,此调用
randomArray(1, 27)
将失败,并且脚本将无限期运行。 Just so you know. 请注意。 :)
:)
You mean something like this? 你的意思是这样的吗?
$letter = false;
$winner = true;
foreach($rk as $key)
{
if($letter === false) $letter = $a[$key];
elseif($a[$key] != $letter) $winner = false;
}
if($winner) echo 'Winner';
else echo 'Loser';
$num = 3;
$unique_vals = array('X','O');
$a = array();
foreach ($unique_vals as $val)
{
for ($i = 0; $i < $num; $i++)
{
$a[] = $val;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.