Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Object distribution algorithm

$
0
0
Hi guys, I trying to write an algorithm to evenly distribute some objects:

I have 6 "Person" objects, each one has 2 "Card" objects that are equal; what I want to do is take each one of those "Cards" (12 in total) and distribute between those 6 "Person" objects with the one condition that a "Person" cant receive his own "Card" objects.

This is what a got so far:

ArrayList<Person> personArray = new ArrayList();
 private void sort(){

        ArrayList<Person> copy = (ArrayList)personArray.clone();

        for (Person fe : personArray) {
            Collections.shuffle(copy);
            for (Person fe1 : copy) {

                if(!fe1.equals(fe)&&fe1.cLeft!=0){

                    if(fe.getoF().size()<2){

                        fe.getoF().add(fe1.getC());
                        fe1.cLeft--;

                    }
                }
            }
        }
}



Where getOf() is a method to obtain the array that will contain the dsitributed "Card" objects and cLeft is a counter that start in 2 and decreases each time a "Card" is distributed.

The problem with this aproach is that the last "Person" on the array could only be left with his own cards.

Any help is appreciated!

Viewing all articles
Browse latest Browse all 51036

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>