Reputation: 738
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
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?
Upvotes: 0
Views: 75
Reputation: 53521
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)
. For example, this call randomArray(1, 27)
will fail, and the script will run indefinitely. Just so you know. :)
Upvotes: 1
Reputation: 1690
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:
<?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';
}
?>
Upvotes: 1
Reputation: 4326
$num = 3;
$unique_vals = array('X','O');
$a = array();
foreach ($unique_vals as $val)
{
for ($i = 0; $i < $num; $i++)
{
$a[] = $val;
}
}
Upvotes: 0
Reputation: 1258
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';
Upvotes: 0