Skip to content

Implement a feature random split players into two team with nearest position. #160

Description

@lifei

I want to implement a feature: random assign players into two team with nearest position.

For example, in the map big game hunter, there are eight positions, 1,3,5,6,7,9,11,12. If there're eight players, the teams should be 11,12,1,3 vs 5,6,7,9. the team center poiter can be at 12.5 vs 6.5. if there're six/four players, it should be following the rules above.

I have got some code from AI. I want to know if this code can be implement using the software?

// Define the list of player positions
const playerPositions = [1, 3, 5, 6, 7, 9, 11, 12];

// Define the two cluster centers
const clusterCenter1 = 12.5;
const clusterCenter2 = 6.5;

// Initialize the two groups
const group1 = [];
const group2 = [];

// Define the function to calculate circular distance
function circularDistance(point1, point2) {
    let directDistance = Math.abs(point1 - point2);
    return Math.min(directDistance, 12 - directDistance);
}

// Iterate through each player's position
for (let i = 0; i < playerPositions.length; i++) {
    const position = playerPositions[i];

    // Calculate the circular distances from the player to both cluster centers
    const distance1 = circularDistance(position, clusterCenter1);
    const distance2 = circularDistance(position, clusterCenter2);

    // Assign the player to the group with the closer cluster center
    if (distance1 < distance2) {
        group1.push(position);
    } else {
        group2.push(position);
    }
}

// Ensure the two groups have equal number of players
while (group1.length > group2.length) {
    let minDistance = Infinity;
    let moveIndex = -1;
    for (let i = 0; i < group1.length; i++) {
        const position = group1[i];
        const newDistance = circularDistance(position, clusterCenter2);
        if (newDistance < minDistance) {
            minDistance = newDistance;
            moveIndex = i;
        }
    }
    if (moveIndex !== -1) {
        const movedPosition = group1.splice(moveIndex, 1)[0];
        group2.push(movedPosition);
    }
}

while (group2.length > group1.length) {
    let minDistance = Infinity;
    let moveIndex = -1;
    for (let i = 0; i < group2.length; i++) {
        const position = group2[i];
        const newDistance = circularDistance(position, clusterCenter1);
        if (newDistance < minDistance) {
            minDistance = newDistance;
            moveIndex = i;
        }
    }
    if (moveIndex !== -1) {
        const movedPosition = group2.splice(moveIndex, 1)[0];
        group1.push(movedPosition);
    }
}

// Output the grouping results
console.log("Group 1:", group1);
console.log("Group 2:", group2);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions