php - Check through an array and identify matching numbers -
i working on scratch card game user purchases scratch card me/my store , can redeem said card scratching off numbers , matching 3 pictures win prize
like physical scratch cards want prizes pre configured know when prize won, users dont, can limit prizes, not true random, random in terms of wins prize..
i know how setup prizes need check each data value in array ensure there not 3 matching numbers, unless have won..
so far have (i neaten , shorten code down soon)
$numbers = array( 0 => rand(0,9), 1 => rand(0,9), 2 => rand(0,9), 3 => rand(0,9), 4 => rand(0,9), 5 => rand(0,9), 6 => rand(0,9), 7 => rand(0,9), 8 => rand(0,9) );
which randomly generates 9 numbers ,
echo "<table>"; $i=0; for($x=0;$x<3;$x++){ echo "<tr>"; for($y=0;$y<3;$y++){ echo "<td>" . $numbers[$i] . "</td>"; $i++; } echo "</tr>"; }
sorts them 3x3 table
i know there command in_array() sort through array looking values dont want run endless loop checking each value see if match, because it's labor intensive , messy well.
i'm looking suggestion might have can sort through array , see if 3 numbers exist, if do, , they're not suppost (ie user hasn't won prize) 1 should changed new number.
so thank in advance guys forward suggestions
luke
**** edit **
+more info
to pick winners use database query such this
$stock = $conn -> query("select distinct * codes available = 1 , 0 = floor(rand()*chance) order rand()");
each code have set chance win , if matches shown, depending on code won prize show
$stock = $conn -> query("select distinct * codes available = 1 , win = 1);
'win' table in database each time plays game goes down 1
so each code have table win when hits 1 pop out in next game
these 2 ways can think of doing think work, both ways allow me control when , if wins second solution more accurate way of doing
how not changing number if triple 1 occurs, preventing triple 1 occurs.
function createscratchrange() { $result = array(); $options = array_merge(range(0,9), range(0,9)); ($i = 0; $i < 9; $i++) { $key = array_rand($options, 1); $result[] = $options[$key]; unset($options[$key]); } return $result; }
and might want able create winners:
function createwinner() { $winningnumber = rand(0,9); $result = array(); $possiblenumbers = range(0,9); unset($possiblenumbers[$winningnumber]); $options = array_merge($possiblenumbers,$possiblenumbers); ($i = 0; $i < 9; $i++) { $key = array_rand($options, 1); $result[] = $options[$key]; unset($options[$key]); } // looks random now.. no winning numbers... lets stick them in there. $keys = array_rand($result, 3); foreach($keys $key) { $result[$key] = $winningnumber; } return $result; }
and testing:
var_dump(createscratchrange()); // never give 3 identical numbers in array. var_dump(createwinner()); // give 3 identical numbers, never 2 sets of 3 identical numbers