Hi,
I am writing a script for a game that needs players to randomly be assigned a target (another player) in a loop so that each player is matched with someone other than who has them. See below:
Andy matched with Jan
Jan matched with Alex
Alex matched with Sue
Sue matched with Andy
My code retrieves name from the database in the order they sign up to play. I need to randomize the pairings for fairness.
Code: <?
$targets = array("Andy","Jan","Alex","Sue"); //names from DB in order of signup
?>
I make a copy of the array, shuffle it and check to make sure that two players are not paired with each other.
Code: <?
$targets = array("Andy","Jan","Alex","Sue"); //names from DB in order of signup
$targets_shuffled = $targets; //copy array
shuffle($targets_shuffled); //shuffle targets
?>
I am having trouble figuring out how to check for duplicate pairings and then reshuffling until it works. Originally I had looped through the arrays to check each pairing:
Code: <?
if($targets[$i] == $targets_shuffled[$i]){
echo "same!";
}
?>
Then I realized when one pair was matched incorrectly the whole pairing would need to be reshuffled. I tried to flag it, break out, and reshuffle, but could never get anything that worked.
Any ideas?
-Blake
Code currently:
Code: <?
shuffle($targets_shuffled);
echo "<br /><br />";
$retry = true;
$tries = 1;
while($retry == true){
echo "try: $tries<br />";
$retry = false;
$i = 0;
while($i < count($targets)){
if($targets[$i] == $targets_shuffled[$i]){
echo "found match!<br />";
$retry = true;
echo "breaking<br />";
break;
}
$i++;
}
$tries++;
}
?>
UDP Multicast question
Hey,
How to schedule the a job?
Hi,
Using loop to count number of entries
I'm writing a program that must ask user to type in numbers. After each entry, the program has to report the cumulative sum of the entries to date. The program should terminate when user enters 0.
New to PHP and just trying to understand a little code.
I hope I'm not annoying anyone or breaking the rules but I was wondering about this bit of code right here: " <?php$people = Array(Array('name' => 'Kalle', 'salt' =>
reading partial code from external site
Hi,I am trying to write a script for my website to get an article from an external website. The external website has the article in a set of tags <roottag> &
Parse XML
Good day,I use oempro software and try to parse XMLReturn but it's not working for me, simplexml_load_string return tons of errors. Maybe someone could help me?Code:
natcasesort works on one server but not on another
HiI have a problem that I was hoping that someone can help me with.I'm trying to use natcasesort() to sort an array. This works fine on my laptop (which I use for testing and which uses php 5.2.6),
ME54/ME54N Conditional check for Release of PR
Hi Gurus,
User information
Hi All,
PHP Upload issue
Hi guys,I have stumble across an interesting issue with my script and is doing my head in.A little background on the application.I've got a document repository site which uses Jupload to upload