Welcome Guest, Not a member yet? Register   Sign In
Implementing Lucky Draw Wheel game - HELP
#1

[eluser]joneslee[/eluser]
Hi all experts

I am learning PHP and CI and now struggle with the algorithm to implement the Lucky Draw Wheel Game. The game consists a wheel with many slices (like Wheel of Fortunes), the wheel will get turned and the player will get the prize written in the slice when the wheel completely stop and arrow point to that slice. There are prizes slices and no-prize slices. The game will randomly pick one result out of the wheel based on the winning probability. Here is an example prizes list:

- Ticket Voucher (Qty: 300 pieces)
- Nintendo DS (Qty: 100 pieces)
- PSP (Qty: 50 pieces)
- Apple MacPro (Qty: 5 pieces)
- SGI Tezro (Qty: 1 piece)

Based on the quantity, the game should give Ticket Voucher higher success rate than other prizes. Can someone suggest me an good approach?

Many thanks,
#2

[eluser]TheFuzzy0ne[/eluser]
That's quite a tough problem. Obviously, you don't want to just give everything away straight away, and you need to make sure that you don't give the same thing away more times than the number of items you actually have. Likewise, you don't want to go weeks or months without any prizes being given out.

To be honest, I think you're best approach is to simply run a draw for each of the prizes, so there'd be 300 draws for ticket vouchers, 100 draws for Nintendo DS and so on. This might not be practical, but in my humble opinion it's more logical.
#3

[eluser]Evil Wizard[/eluser]
I suppose you could create a super array of the prize slices and select the result accordingly
Code:
$arrPrizes = array(
            'prize_1'    =>    array(    'prize'    =>    'Ticket Voucher',
                            'qty'    =>    300),
            'prize_2'    =>    array(    'prize'    =>    'Nintendo DS',
                            'qty'    =>    100),
            'prize_3'    =>    array(    'prize'    =>    'PSP',
                            'qty'    =>    50),
            'prize_4'    =>    array(    'prize'    =>    'Apple MacPro',
                            'qty'    =>    5),
            'prize_5'    =>    array(    'prize'    =>    'SGI Tezro',
                            'qty'    =>    1)
        );
$arrPrizeWheel = array();
foreach($arrPrizes as $strPrize => $arrPrize) {
    $intPrizes = intval($arrPrize['qty'] -1);
    $arrPrizeSlice = array_fill(1, intval($arrPrize['qty']), $arrPrize['prize']);
    $arrPrizeWheel = array_merge($arrPrizeWheel, $arrPrizeSlice);
}
print_r($arrPrizeWheel);
That builds the prize wheel array, all you have to do then is select a random array index, that's the prize won, then pop/unset the element, sort the array to recalculate the array indexs for the next pick.

Hope that helps a little
#4

[eluser]Dam1an[/eluser]
you will also want to add a load of empty slots in there for when they don't win anything
#5

[eluser]Evil Wizard[/eluser]
he never mentioned that :O lol but all he needs to do is add an extra prize to array
Code:
$arrPrizes = array(
...
            'prize_6'    =>    array(    'prize'    =>    'No Win',
                                            'qty'    =>    500)
        );
and when the slice selection is made all that is needed is to check if the value is not equal to 'No Win'
#6

[eluser]Dam1an[/eluser]
[quote author="joneslee" date="1244613984"]There are prizes slices and no-prize slices.[/quote]

Didn't mention it eh? Wink
#7

[eluser]Evil Wizard[/eluser]
oooops must have missed that hehe but the wheel is made of the slices, so I suppose adding "no win" slices to the wheel just like adding winning slices and worry about if the user actually got a prize after the selection is made
#8

[eluser]Dam1an[/eluser]
And I just thought of another thing
Although in evils example, you remove the prize, it'll just reset on a page refresh

An easy way around this is to just serialize the array and write it to a file/database
Then just unserialize the array next time you need to spin the wheel
#9

[eluser]Evil Wizard[/eluser]
Actually that would be a given or every time the page was requested the super prize array would get reinitialised, I did think about the session and serialize but forgot to add it into the post lol, I was trying to get the right command to populate the array Wink
#10

[eluser]Dam1an[/eluser]
I wasn't criticising you're solution, I was adding to it Wink




Theme © iAndrew 2016 - Forum software by © MyBB