PHP Code:
function onCreated()
{
// -- START EDIT
temp.objectList = allplayers; // List of players to process
temp.numberOfTeams = 2; // Number of teams
// -- END EDIT
temp.playerList = new[0];
// Generates a new list of objects we can work with
for (temp.object: temp.objectList) {
// Let's avoid adding those silly RemoteControls
if (temp.object.level.name != "")
continue;
temp.playerList.add(temp.object);
}
// Amount of players
temp.amountOfPlayers = temp.playerList.size();
// Array creation based on number of teams
temp.teams = new[temp.numberOfTeams];
temp.team = 0;
// Do we have enough players to make even teams?
if (temp.amountOfPlayers%temp.numberOfTeams)
temp.oddManOut = true;
// Let's add players to the teams!
while (!temp.teamsGenerated) {
// Grab a random player from the list...
temp.object = randomString(temp.playerList);
temp.objectIndex = temp.playerList.index(temp.object);
// Abort if an object wasn't found anymore
if (temp.objectIndex == -1)
return echo("[" @ this.name @ "]: Index of object returned -1!");
// Add the player to the team array
temp.teams[temp.team].add(temp.object);
// Remove the player from the player list
temp.playerList.delete(temp.objectIndex);
echo("[" @ this.name @ "]: Added '" @ temp.object @ "' to team '" @ temp.team @ "'!");
// Based on how many players are left, we may stop now.
temp.remainingPlayers = temp.playerList.size();
if (temp.remainingPlayers == 0)
break;
else if (temp.remainingPlayers == temp.numberOfTeams - 1 && temp.oddManOut)
break;
// If we do have more players to work with, change the team for the next one.
temp.team ++;
temp.team %= temp.numberOfTeams;
}
echo("[" @ this.name @ "]: All" SPC temp.numberOfTeams SPC "teams sorted evenly!" SPC temp.remainingPlayers SPC "player(s) were left out" SPC (temp.remainingPlayers > 0 ? ":(" : ":)"));
}
:p
I thought that as well, I was going to say something but i'm not super sure how the players are listed since everyone would have their own arrays I guess.
A way to ensure randomness would be to do something like:
PHP Code:
temp.pArr = players; temp.teams = {0,0}; temp.toggle = 0; while (pArr.size() > 0){ toggle =(toggle+1)%2; temp.rInd = int(random(0,pArr.size()); temp.teams[toggle].add(pArr[rInd]); pArr.remove(rInd); }
But meh...
|
Please, oh -PLEASE-, start naming your variables more logically. Stuff like "pArr" and "rInd" might make sense at the time of making, but when looking back at code you'll quickly come to realize how stupid you were by naming it that. It also makes it hard for anyone else to figure out what your code actually does.