Re: Count Robots on Field, place in array
Maciej Hrynczyszyn <[email protected]> Thu, 6 Oct 2016 21:57:08 +0200
| Newsgroups | gmane.comp.java.robocode |
|---|---|
| Message-ID | <CAMDJrzWj_WUS32T=fV9nYj_q2+52YS+77cAuzL=Zg0LFKVNRvQ@mail.gmail.com> |
On 6 October 2016 at 05:45, Jesse Bevil <[email protected]> wrote: > So I'm looking to count the number of robots on the field. Not really sure > how I can accomplish that. I was thinking that if I make a multi-dimension > array and on each scanned robot save e.getName and e.getDistance and then > sort the array from lowest to highest distance, then select the target at > the top index of the array that I could improve survivability. I'm new to > RoboCode (about an hour ago), I am also new to Java (Fundamentals I class in > college, received an A, but doesn't really mean much without experience). > [...] Hi Jesse, I can't remember much of Robocode API but if you're only looking for the closest robot, you don't have to keep them all (in an array or other collection). You could just have two fields (String name; long distance oslt) and on each ::onScannedRobot(...) you could check if the distance to newly observed robot is smaller than smallest observed so far. If it is, update name and distance values. Reset the values between game ticks (turns, I guess). If you really want to look for a min value in an array/collection, preferred idiom these days would be to use Stream::min. Best regards, -- You received this message because you are subscribed to the Google Groups "robocode" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.