Imoen Posted December 4, 2009 Share Posted December 4, 2009 I am making this program. In one list it should have players of a game and their coordinates, in another list it should have coordinates of new places to go to. Each player should be checked to see the distance from their coordinates to the coordinates of the list. If it's over 100 then it should be ignored. Then each person should be paired with coordinates using some logic, like if they could be paired with 2 options but someone else can only be paired with 1 of those options then it will pair the one with only that option to it. Here's a sample code with some values put in for testing it. It should result with player1 pair with 300|300, player2 pair with 200|200, and player3 pair with 100|100. $player_info[] = array('player1', '050', '050'); $player_info[] = array('player2', '150', '150'); $player_info[] = array('player3', '250', '250'); $village_info[] = array('100', '100'); $village_info[] = array('200', '200'); $village_info[] = array('300', '300'); foreach($player_info as $p_info) { foreach($village_info as $v_info) { $dis_check = getDistance($v_info[0], $v_info[1], $p_info[1], $p_info[2]); if($dis_check < 100) { $times[] = array($dis_check, $p_info[0], $v_info[0] . "|" . $v_info[1]); } } } array_multisort($times); function getDistance($targets_x, $targets_y, $base_x, $base_y) { $field = sqrt(pow($targets_x - $base_x, 2) + pow($targets_y - $base_y, 2) ); $fields = round($field); return $fields; } I am lost at what to do to get there though. Quote Link to comment Share on other sites More sharing options...
Zane Posted December 4, 2009 Share Posted December 4, 2009 I'm still lost at what your question is. Is there something wrong with your code? Are you looking for design techniques? What is your logic... in a verbose sense.? I have a sneaking suspision that you need to research this function array_merge() Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 5, 2009 Share Posted December 5, 2009 Your example doesn't jibe with your description. You state Each player should be checked to see the distance from their coordinates to the coordinates of the list. If it's over 100 then it should be ignored Then you state that in the example code player 1 should be paired with (300, 300). But, player 1's current position is 50, 50 which is ~353 units away. Aside from that problem, I think you are going to have a hard time getting the "perfect" solution. What should happen if there are three players which all have the same two available options? Which two get one of the two and what do you do about the third player? Assuming that when there is a comflict sach as the above that one player will not get a matching, here is the logic I would follow: Iterrate through the players finding ones with only one available match and make that match. When that is done, some players which had multiple possible matches may only have one available since some were used up on the first pass. So, repeat the process above until you get through one complete pass with no players with only one available match. Then do a pass looking for players with 2 or 1 available matches and assign one. Run that again and again until there are none left. Then do for players with 3-1 possible matches and so on. The problem with all of that is that it would not necessarily always do the optimal selection and you may get some players that do not get a match when they could of when matched int he "optimal" manner. Here's an example: Player / Possible matches Bob : A, B Tim : B, C Jim : A, C It is possible to match each of them to unique values such as Bob:A, Tim:B, Jim:C But, if you were to match Bob with A and Tim with C, you would have no available matches for Jim. Again, I think trying to get the perfect solution would be very complex as it would have to look at all conceivable pairings before determining the best matches. Quote Link to comment Share on other sites More sharing options...
Imoen Posted December 5, 2009 Author Share Posted December 5, 2009 My code doesn't do what I need it to do, it just gets the arrays ready to be paired up. My example wasn't the best, basically I was meaning that instead of going for the first pairing it should look to see if by using that paring it will mean that another can't make a pair at all. I was thinking that it would work to get all of the distances figured out, then to sort the array looking at how many times a player comes up with a possible match. If the player only comes up once then they should be paired with those coordinates first so that other players will more possibilities can just be paired with the others. The units distance is 100 based off the value that is returned from the getDistance function, I'm not totally certain how the people who made the game came up with the calculations but that's the formula used, it give the right numbers when compared to the ones from the game. I looked at array_merge, that seems to be making 2 arrays become one without pairing anything together. mjdamato has it it right as far as what I'm asking, I'm just not sure how it can be done in php. Quote Link to comment Share on other sites More sharing options...
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.