corruptcity || skalman Posted August 22, 2012 Posted August 22, 2012 Hi I am currently creating a horse racing mod for my game and I'm trying to have it that each time you bet it shows you which horse finished in the top 3 and where your horse that you bet on finished. I decided to use array which would hold the ID and the random number that is generated this works for 1st and 2nd but I'm having problems trying to do the 3rd. Been stuck a while so I'm posting on here, here is the code so far. $horse = array(); for($i=1;$i<=7;$i++) { $rand1 = (int)rand(1,50); $rand2 = (int)rand(1,50); $rand3 = (int)rand(1,50); $total = (int)(($rand1 + $rand2 + $rand3) / 3); $horse[$i] = $total; //this bit is just there while i create it echo"$total<br>"; } //works out which horse won if($horse[1] > $horse[2] && $horse[1] > $horse[3] && $horse[1] > $horse[4] && $horse[1] > $horse[5] && $horse[1] > $horse[6] && $horse[1] > $horse[7]) { echo"horse 1 wins<br>"; $winner = 1; $sec = array('1' => '$horse[2]', '2' => '$horse[3]', '3' => '$horse[4]', '4' => '$horse[5]', '5' => '$horse[6]', '6' => '$horse[7]',); } elseif($horse[2] > $horse[2] && $horse[2] > $horse[3] && $horse[2] > $horse[4] && $horse[2] > $horse[5] && $horse[2] > $horse[6] && $horse[2] > $horse[7]) { echo"horse 2 wins<br>"; $winner = 2; $sec = array('1' => '$horse[1]', '2' => '$horse[3]', '3' => '$horse[4]', '4' => '$horse[5]', '5' => '$horse[6]', '6' => '$horse[7]',); } elseif($horse[3] > $horse[2] && $horse[3] > $horse[3] && $horse[3] > $horse[4] && $horse[3] > $horse[5] && $horse[3] > $horse[6] && $horse[3] > $horse[7]) { echo"horse 3 wins<br>"; $winner = 3; $sec = array('1' => '$horse[1]', '2' => '$horse[2]', '3' => '$horse[4]', '4' => '$horse[5]', '5' => '$horse[6]', '6' => '$horse[7]',); } elseif($horse[4] > $horse[2] && $horse[4] > $horse[3] && $horse[4] > $horse[4] && $horse[4] > $horse[5] && $horse[4] > $horse[6] && $horse[4] > $horse[7]) { echo"horse 4 wins<br>"; $winner = 4; $sec = array('1' => '$horse[1]', '2' => '$horse[2]', '3' => '$horse[3]', '4' => '$horse[5]', '5' => '$horse[6]', '6' => '$horse[7]',); } elseif($horse[5] > $horse[2] && $horse[5] > $horse[3] && $horse[5] > $horse[4] && $horse[5] > $horse[5] && $horse[5] > $horse[6] && $horse[5] > $horse[7]) { echo"horse 5 wins<br>"; $winner = 5; $sec = array('1' => '$horse[1]', '2' => '$horse[2]', '3' => '$horse[3]', '4' => '$horse[4]', '5' => '$horse[6]', '6' => '$horse[7]',); } elseif($horse[6] > $horse[2] && $horse[6] > $horse[3] && $horse[6] > $horse[4] && $horse[6] > $horse[5] && $horse[6] > $horse[6] && $horse[6] > $horse[7]) { echo"horse 6 wins<br>"; $winner = 6; $sec = array('1' => '$horse[1]', '2' => '$horse[2]', '3' => '$horse[3]', '4' => '$horse[4]', '5' => '$horse[5]', '6' => '$horse[7]',); } else { echo"horse 7 wins<br>"; $winner = 7; $sec = array('1' => '$horse[1]', '2' => '$horse[2]', '3' => '$horse[3]', '4' => '$horse[4]', '5' => '$horse[5]', '6' => '$horse[6]',); } //works out which horse finishd 2 if($sec[1] > $sec[2] && $sec[1] > $sec[3] && $sec[1] > $sec[4] && $sec[1] > $sec[5] && $sec[1] > $sec[6] && $sec[1] > $sec[7]) { echo"horse 1 finished second<br>"; $second = 1; $third = array('1' => '$horse[2]', '2' => '$horse[3]', '3' => '$horse[4]', '4' => '$horse[5]', '5' => '$horse[6]', '6' => '$horse[7]',); } elseif($sec[2] > $sec[2] && $sec[2] > $sec[3] && $sec[2] > $sec[4] && $sec[2] > $sec[5] && $sec[2] > $sec[6] && $sec[2] > $sec[7]) { echo"horse 2 finished second<br>"; $second = 2; $third = array('1' => '$horse[1]', '2' => '$horse[3]', '3' => '$horse[4]', '4' => '$horse[5]', '5' => '$horse[6]', '6' => '$horse[7]',); } elseif($sec[3] > $sec[2] && $sec[3] > $sec[3] && $sec[3] > $sec[4] && $sec[3] > $sec[5] && $sec[3] > $sec[6] && $sec[3] > $sec[7]) { echo"horse 3 finished second<br>"; $second = 3; $third = array('1' => '$horse[1]', '2' => '$horse[2]', '3' => '$horse[4]', '4' => '$horse[5]', '5' => '$horse[6]', '6' => '$horse[7]',); } elseif($sec[4] > $sec[2] && $sec[4] > $sec[3] && $sec[4] > $sec[4] && $sec[4] > $sec[5] && $sec[4] > $sec[6] && $sec[4] > $sec[7]) { echo"horse 4 finished second<br>"; $second = 4; $third = array('1' => '$horse[1]', '2' => '$horse[2]', '3' => '$horse[3]', '4' => '$horse[5]', '5' => '$horse[6]', '6' => '$horse[7]',); } elseif($sec[5] > $sec[2] && $sec[5] > $sec[3] && $sec[5] > $sec[4] && $sec[5] > $sec[5] && $sec[5] > $sec[6] && $sec[5] > $sec[7]) { echo"horse 5 finished second<br>"; $second = 5; $third = array('1' => '$horse[1]', '2' => '$horse[2]', '3' => '$horse[3]', '4' => '$horse[4]', '5' => '$horse[6]', '6' => '$horse[7]',); } elseif($sec[6] > $sec[2] && $sec[6] > $sec[3] && $sec[6] > $sec[4] && $sec[6] > $sec[5] && $sec[6] > $sec[6] && $sec[6] > $sec[7]) { echo"horse 6 finished second<br>"; $second = 6; $third = array('1' => '$horse[1]', '2' => '$horse[2]', '3' => '$horse[3]', '4' => '$horse[4]', '5' => '$horse[5]', '6' => '$horse[7]',); } else { echo"horse 7 finished second<br>"; $second = 7; $third = array('1' => '$horse[1]', '2' => '$horse[2]', '3' => '$horse[3]', '4' => '$horse[4]', '5' => '$horse[5]', '6' => '$horse[6]',); } //find out who finished third if($third[1] > $third[2] && $third[1] > $third[3] && $third[1] > $third[4] && $third[1] > $third[5] && $third[1] > $third[6] && $third[1] > $third[7]) { echo"horse 1 finished second<br>"; $third = 1; $forth = array('1' => '$horse[2]', '2' => '$horse[3]', '3' => '$horse[4]', '4' => '$horse[5]', '5' => '$horse[6]', '6' => '$horse[7]',); } elseif($third[2] > $third[2] && $third[2] > $third[3] && $third[2] > $third[4] && $third[2] > $third[5] && $third[2] > $third[6] && $third[2] > $third[7]) { echo"horse 2 finished second<br>"; $third = 2; $forth = array('1' => '$horse[1]', '2' => '$horse[3]', '3' => '$horse[4]', '4' => '$horse[5]', '5' => '$horse[6]', '6' => '$horse[7]',); } elseif($third[3] > $third[2] && $third[3] > $third[3] && $third[3] > $third[4] && $third[3] > $third[5] && $third[3] > $third[6] && $third[3] > $third[7]) { echo"horse 3 finished second<br>"; $third = 3; $forth = array('1' => '$horse[1]', '2' => '$horse[2]', '3' => '$horse[4]', '4' => '$horse[5]', '5' => '$horse[6]', '6' => '$horse[7]',); } elseif($third[4] > $third[2] && $third[4] > $third[3] && $third[4] > $third[4] && $third[4] > $third[5] && $third[4] > $third[6] && $third[4] > $third[7]) { echo"horse 4 finished second<br>"; $third = 4; $forth = array('1' => '$horse[1]', '2' => '$horse[2]', '3' => '$horse[3]', '4' => '$horse[5]', '5' => '$horse[6]', '6' => '$horse[7]',); } elseif($third[5] > $third[2] && $third[5] > $third[3] && $third[5] > $third[4] && $third[5] > $third[5] && $third[5] > $third[6] && $third[5] > $third[7]) { echo"horse 5 finished second<br>"; $third = 5; $forth = array('1' => '$horse[1]', '2' => '$horse[2]', '3' => '$horse[3]', '4' => '$horse[4]', '5' => '$horse[6]', '6' => '$horse[7]',); } elseif($third[6] > $third[2] && $third[6] > $third[3] && $third[6] > $third[4] && $third[6] > $third[5] && $third[6] > $third[6] && $third[6] > $third[7]) { echo"horse 6 finished second<br>"; $third = 6; $forth = array('1' => '$horse[1]', '2' => '$horse[2]', '3' => '$horse[3]', '4' => '$horse[4]', '5' => '$horse[5]', '6' => '$horse[7]',); } else { echo"horse 7 finished second<br>"; $third = 7; $forth = array('1' => '$horse[1]', '2' => '$horse[2]', '3' => '$horse[3]', '4' => '$horse[4]', '5' => '$horse[5]', '6' => '$horse[6]',); } the problem I'm having is that im not sure how i would work out who finished 3rd and so on is how am i gonna know which horse 2 remove from the array for the next array if I don't know what the random numbers are going to be before hand. if anyone has a better/easier solution or advice on how to do this that would be much welcome cheers skalman Quote
Octarine Posted August 22, 2012 Posted August 22, 2012 (edited) Why not simplify it - all you need in effect is three random numbers (or horses) from seven ... For example; using range() and shuffle() Edited August 23, 2012 by Octarine Quote
corruptcity || skalman Posted August 22, 2012 Author Posted August 22, 2012 ok thanks for the help Oct but i was then thinking about going all the way and working out where each horse finished but after seeing your suggestion that shouldn't be a problem to do Quote
Octarine Posted August 22, 2012 Posted August 22, 2012 Examine the contents of the array after the shuffle; that will yield full placement for each nag. Quote
corruptcity || skalman Posted August 22, 2012 Author Posted August 22, 2012 yeah ive got it sorted thanks m8 your a life saver Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.