Coderrrr
Coderrrr

Reputation: 230

Check through an array and identify matching numbers

I am working on a scratch card game where by a user purchases a scratch card from me/my store and can then redeem said card by scratching off numbers and matching 3 pictures to win a prize

Like physical scratch cards I want the prizes to be pre configured so that I know when a prize will be won, but obviously the users dont, this is so I can limit the prizes, so it is not true random, but will be random in terms of who wins the prize..

I know how I will setup the prizes but I need to check each data value in an array to ensure that there is not 3 matching numbers, unless they have actually won..

So far I have this (I will neaten it up and shorten the 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 just randomly generates 9 numbers and

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 into a 3x3 table

I know there is the command in_array() which will sort through the array looking for certain values but I dont want to run an endless loop checking each value to see if I get a match, because it's labor intensive and would be messy as well.

I'm looking for a suggestion anyone might have so I can sort through the array and see if any 3 numbers exist, if they do, and they're not suppost to (ie the user hasn't actually won a prize) then one should be changed to a new number.

So thank you in advance for your help guys I look forward to your suggestions

Luke

**** EDIT **

+More info

To pick the winners I will use a database query such as this

$stock = $conn -> query("SELECT DISTINCT * FROM codes WHERE Available = 1 AND 0 = FLOOR(RAND()*Chance) ORDER BY RAND()");

Each code will have a set chance to win and if it matches up it will be shown, depending on the code won a certain prize will show

$stock = $conn -> query("SELECT DISTINCT * FROM codes WHERE Available = 1 AND Win = 1);

'Win' will be a table in the database which each time someone plays the game it goes down by 1

So each code will have the table Win which when it hits 1 it will pop out the in the next game

These are two ways I can think of doing it which I think will work, both ways roughly allow me to control when and if someone wins but the second solution is obviously a more accurate way of doing it

Upvotes: 1

Views: 216

Answers (1)

nvanesch
nvanesch

Reputation: 2600

how about not changing a number if a triple one occurs, but preventing a triple one occurs.

function createScratchRange()
{
    $result = array();
    $options = array_merge(range(0,9), range(0,9));

    for ($i = 0; $i < 9; $i++)
    {
        $key =  array_rand($options, 1);
        $result[] = $options[$key];
        unset($options[$key]);
    }

    return $result;
}

and you might want to be able to create winners:

function createWinner()
{
    $winningNumber = rand(0,9);

    $result = array();
    $possibleNumbers = range(0,9);
    unset($possibleNumbers[$winningNumber]);
    $options = array_merge($possibleNumbers,$possibleNumbers);

    for ($i = 0; $i < 9; $i++)
    {
        $key =  array_rand($options, 1);
        $result[] = $options[$key];
        unset($options[$key]);
    }

    // looks random now.. but no winning numbers... Lets just stick them in there.
    $keys = array_rand($result, 3);
    foreach($keys as $key)
    {
        $result[$key] = $winningNumber;
    }

    return $result;
}

and for testing:

var_dump(createScratchRange());  // this will never give 3 identical numbers in the array.
var_dump(createWinner());  // this will ALWAYS give 3 identical numbers, but never 2 sets of 3 identical numbers

Upvotes: 2

Related Questions