无限制地随机化数组,然后在 Javascript 中将其平均拆分

Randomizing array with no limit, then splitting it up equally in Javascript

本文关键字:拆分 Javascript 随机化 数组 然后 无限制      更新时间:2023-09-26
var players = [];
var totalplayers = 0
var team1 = [];
var team2 = [];
var commands = {
"teams.join": {
    name: "Join random team.",
    description: "Anybody who joins will be put to a team",
    process: function(bot, msg, suffix) {   
        players.push(msg.sender);
        bot.sendMessage(msg.channel, players);
        bot.sendMessage(msg.channel, msg.sender + " has been added to the random team selection.");
        totalplayers += 1;
        bot.sendMessage(msg.channel, totalplayers)
    },
},
"teams.random": {
    name: "Random team selection.",
    desciption: "Displays all players in random team selection in a random team.",
    process: function(bot, msg, suffix) {
        var playcount = 0;
        bot.sendMessage(msg.channel, "tp: " + totalplayers); // Check
        bot.sendMessage(msg.channel, "i: " + playcount); // Check
        for (playcount = 0; playcount < totalplayers; playcount++) { 
            //bot.sendMessage(msg.channel, "Looping?") // Check
            var Rteam = players[Math.floor(Math.random() * players.length)];
            //bot.sendMessage(msg.channel, Rteam); // Check
            if (playcount = 0 || 2 || 4 || 6 || 8) {
                team1.push(Rteam);
                bot.sendMessage(msg.channel, "isEven = true"); // Check
                playcount + 1;
            } else if (playcount = 1 || 3 || 5 || 7 || 9) {
                team2.push(Rteam);
                bot.sendMessage(msg.channel, "isEven = false"); // Check
                playcount + 1;
            } 
        playcount + 1;  
        var roll = players.splice(Rteam, 1);
        var yourNumber = roll[totalplayers];
        //i += 1;
        } 
        bot.sendMessage(msg.channel, "Team 1: " + team1);
        bot.sendMessage(msg.channel, "Team 2: " + team2);
    },  
}

teams.join工作正常,但我包括显示teams的整个部分。 teams.random应该采用players数组,随机化数组中的玩家(用户(位置,然后分发它们,以便players[0]在团队 1 上,players[1]在团队 2(依此类推(上,直到没有更多的用户。从本质上讲,它是获取一个列表并将其随机分成两组。我在测试时意识到的主要事情是playcount不会增加(我已经尝试了 For、While 和 Do-till 循环无济于事。

这是

不正确的:

if (playcount = 0 || 2 || 4 || 6 || 8) {

有两个问题:首先,你使用的是=,这是赋值,而它应该==进行比较。其次,你不能用这种方式||与多个元素进行比较,你需要为每个项目单独进行比较。所以它应该是:

if (playcount == 0 || playcount == 2 || playcount == 4 || playcount == 6 || playcount == 8) {

但是如果你想知道playcount是否均匀,你可以做:

if (playcount % 2 == 0)

您不需要将else if用于其他测试,只需使用 else ,因为只有两种可能性。

包含以下内容的行:

playcount + 1;

什么都不要做。您根本不需要它,因为您在for()标头中递增playcount

顺便说一下,您的代码中没有任何内容可以阻止您多次选择同一名球员,并将他们添加到不同的团队。

我同意 barmar 对 OP 原始代码的评论。但为了完整起见,这里有一个示例代码,可以将团队分成两半,并将玩家随机放置在每个团队中。

var players = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    team1 = [],
    team2 = [],
    i;
while (players.length > 0) {
    i = Math.floor(Math.random() * players.length);
    if (players.length % 2 === 0) {
        team1.push(players[i]);
    } else {
        team2.push(players[i]);
    }
    players.splice(i, 1);
}
console.log(team1, team2);

输出:

[10, 4, 1, 6, 5] [3, 8, 2, 9, 7]

在浏览器控制台中运行代码几次,您将在每个数组中获得不同的随机数。