This site requires JavaScript, please enable it in your browser!
Greenfoot back
habidex
habidex wrote ...

2014/11/20

Moving different actors (actors a) to a location closely around new actor b

1
2
habidex habidex

2014/11/23

#
Actually, a file containing names of students was read and used to populate number of students into the world. I want to distribute the students randomly around different groups. So I need to have control over each individual actor in order to allocate it to a group. I have already populated the number of group and the students too are populated below the list of groups. I just want the students to start moving in a random motion to get attached to a group depending on the number of students available and the number of groups
danpost danpost

2014/11/24

#
habidex wrote...
So I need to have control over each individual actor in order to allocate it to a group.
I sounds like you just need to randomly assign each student a group such that they are spread out among the groups as evenly as possible. You can get a list of students with 'getWorld().getObjects(Student.class)' and similar for getting a list of all the groups. From the size of the lists, you can get how many students will be in each group and how many groups will have an extra one, if any (the remainder after dividing the number of groups into the number of students). You can shuffle both the group and student lists and then remove from the student list however many extra students you will need and create a separate list of those. Finally loop through the list of groups and assign the first however many student (the minimum number of students in any group) in the students list to the groups in the groups list. While doing that, assign one extra student to that group, if any are there and have not been assigned. It would be something like this (I will code it for the world class):
public void distributeStudentsAmongGroups()
{
    java.util.List<Object> groups = getObjects(Group.class);
    java.util.List<Object> students = getObjects(Student.class);
    int quotient = students.size()/groups.size();
    int remainder = students.size()%groups.size();
    java.util.Collections.shuffle(groups);
    java.util.Collections.shuffle(students);
    for (int g=0; g<groups.size(); g++)
    {
        Group group = (Group)groups.get(g);
        for (int n=0; n<quotient; n++)
            ((Student)students.get(g*quotient+n+remainder)).assignGroup(group);
        if (g < remainder) ((Student)students.get(g)).assignGroup(group);
    }
}
That should at least get all students assigned to a randomly chosen group as evenly as possible.
You need to login to post a reply.
1
2