javascript - 如何动态生成团队之间可能的遭遇?

标签 javascript

感谢您提前在此练习中提供的帮助,实际上我还没有发现如何解决它

如何动态生成团队之间可能的遭遇?

具有以下输入字段

  • 开始日期
  • 团队
  • 字段
  • 游玩天数

例如以下数据

const startDate = "03-08-2020";
const teams = ["A", "B", "C", "D", "E", "F"];
const fields = ["Field1", "Field2"];
const daysToPlay = ["Monday", "Wednesday"];

现在,我需要根据这些数据动态生成比赛日及其比赛匹配,比赛 field 必须像从日期到日期的天数一样互换。从startDate值开始

输出示例如下

const output = [
    {
        number: 1,
        date: "03-08-2020", // Monday
        field: "Field1",
        matches: [
            ["A", "B"],
            ["C", "D"],
            ["E", "F"],
        ],
    },
    {
        number: 2,
        date: "05-08-2020", // Wednesday
        field: "Field2",
        matches: [
            ["A", "C"],
            ["B", "E"],
            ["C", "F"],
        ],
    },
];

这样,根据团队之间唯一可能遭遇的次数。

更新0

  • 所有球队都必须在每个日期进行比赛
  • 团队数量始终为偶数
  • 当每支球队都与其他所有球队交手后,锦标赛结束,

更新1

我遵循奥利弗的建议,但在最后一组比赛中我得到了一场比赛,这里我期待像之前那样的两场比赛

const teams = ["A", "B", "C", "D"];

const getCombinations = (data) => {
  let output = [];

  for (let i = 0; i < teams.length - 1; i++) {
    for (let j = i + 1; j < teams.length; j++) {
      output = [...output, [data[i], data[j]]];
    }
  }

  return output;
};

const getMatches = (data) => {
  let matches = [];
  let i = 0;

  while (data.length) {
    for (const [index, entry] of data.entries()) {
      if (index === 0) {
        matches.push([entry]);

        data.splice(index, 1);

        continue;
      }

      const [team1, team2] = entry;
      const idx = matches[i].findIndex(
        (value) => value.includes(team1) || value.includes(team2)
      );

      if (idx !== -1) {
        continue;
      }

      matches[i].push(entry);
      data.splice(index, 1);
    }

    i++;
  }

  return matches;
};

const combinations = getCombinations(teams);
const matches = getMatches(combinations);

console.log(matches);

更新2

修复之前的更新

const teams = ["A", "B", "C", "D"];

const getCombinations = (data) => {
  let output = [];

  for (let i = 0; i < teams.length - 1; i++) {
    for (let j = i + 1; j < teams.length; j++) {
      output = [...output, [data[i], data[j]]];
    }
  }

  return output;
};

const getMatches = (data) => {
  let matches = [];
  let i = 0;

  while (data.length) {
    for (const [index, entry] of data.entries()) {
      if (data.length === 1) {
        matches[i - 1].push(entry);
        data.splice(index, 1);
        break;
      }

      if (index === 0) {
        matches.push([entry]);

        data.splice(index, 1);
        continue;
      }

      const [team1, team2] = entry;
      const idx = matches[i].findIndex(
        (value) => value.includes(team1) || value.includes(team2)
      );

      if (idx !== -1) {
        continue;
      }

      matches[i].push(entry);
      data.splice(index, 1);
    }

    i++;
  }

  return matches;
};

const combinations = getCombinations(teams);

console.log(combinations);

const matches = getMatches(combinations);

console.log(matches);

更新3

我快到了

我在执行与日期相关的任务时遇到问题,如何获得正确的日期。为了使解决方案更容易,我设法通过星期几的数字而不是日期的名称来更改游戏天数的输入,这样

Sunday 0
...
Saturday 6

在示例中,日期对应于星期一 (1) 和星期三 (3)

感谢您的评论

const startDate = "2020-08-03";
const matchDays = [1, 3];
const fields = ["Field 1", "Field 2"];
const teams = ["A", "B", "C", "D"];

const getCombinations = (data) => {
  let output = [];

  for (let i = 0; i < teams.length - 1; i++) {
    for (let j = i + 1; j < teams.length; j++) {
      output = [...output, [data[i], data[j]]];
    }
  }

  return output;
};

const getMatches = (data) => {
  let matches = [];
  let i = 0;

  while (data.length) {
    for (const [index, entry] of data.entries()) {
      if (data.length === 1) {
        matches[i - 1].push(entry);
        data.splice(index, 1);
        break;
      }

      if (index === 0) {
        matches.push([entry]);

        data.splice(index, 1);
        continue;
      }

      const [team1, team2] = entry;
      const idx = matches[i].findIndex(
        (value) => value.includes(team1) || value.includes(team2)
      );

      if (idx !== -1) {
        continue;
      }

      matches[i].push(entry);
      data.splice(index, 1);
    }

    i++;
  }

  return matches;
};

const getGameDays = (data) => {
  const options = {
    year: "numeric",
    month: "2-digit",
    day: "2-digit",
  };

  return data.map((entry, index) => {
    return {
      number: index + 1,
      date:
        index === 0
          ? new Date(startDate).toLocaleDateString("es-ES", options)
          : new Date(startDate).toLocaleDateString("es-ES", options), // Here I need to move on every target day of week in matchDays
      field:
        fields.length === 1
          ? fields[0]
          : index === 0
          ? fields[0]
          : index % 2 === 0
          ? fields[0]
          : fields[1],
      matches: [...entry],
    };
  });
};

const combinations = getCombinations(teams);

console.log(combinations);

const matches = getMatches(combinations);

console.log(matches);

const gameDays = getGameDays(matches);

console.dir(gameDays, { depth: null, color: true });

此时设置每天的开始日期

谢谢

最佳答案

似乎是 https://github.com/dankogai/js-combinatorics 的工作.

在这里,您必须创建大小为 2 的所有组合(new Combination(teams, 2);)。现在您已经获得了所有组合(如果您有重赛,您可以使用 new Combination(teams.reverse(), 2); 创建它们。

现在持有一个teamsPlayedToday的(空)数组,从组合中选择第一个条目,将其添加到当日比赛中,将球队添加到上述数组中并将其从组合列表中删除.

在下一步中,从组合中选择下一个条目,检查 teamsPlayedToday 列表中是否提到了任何球队,如果是,则跳到下一个组合,直到找到没有球队的组合在今天的列表中。

当您到达终点时,您就有了第一天的计划。每天重复上述步骤,直到您的组合列表为空。

从给定的开始日期,放入new Date()和方法getDay()您应该能够找到 startDate 的值。

如果此草图的任何内容不起作用,请用您的具体代码以及您的期望和得到的内容提出一个新问题。

获取有效日期的更新

要从给定的开始日期和允许的工作日获取玩的天数列表,您可以使用如下内容:

// Taken from https://stackoverflow.com/a/19691491/1838048
function addDays(date, days) {
  var result = new Date(date);
  result.setDate(result.getDate() + days);
  return result;
}

// Proof-of-concept without argument checks (could lead to endless loops!)
function getMatchingDays(startDate, allowedDays, maxCount) {
  const matchingDays = [];
  let current = startDate;

  while (matchingDays.length < maxCount) {
    if(allowedDays.find(day => day === current.getUTCDay())){
      matchingDays.push(current);
    }

    current = addDays(current, 1);
  }

  return matchingDays;
}

// Given start values
const startDate = "2020-08-03";
const daysToPlay = [1, 3];
// Could come from your matches.length array
const numberOfMatches = 13;

const result = getMatchingDays(new Date(startDate), daysToPlay, numberOfMatches);
console.log(result);

关于javascript - 如何动态生成团队之间可能的遭遇?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63224352/

相关文章:

javascript - 如何使用 {can.route} 监听空白路线并使用react?

javascript - Angular:使用 @Input() 传递数据

javascript - 语法错误: Token '21' is an unexpected token at column 12 of the expression [2013-08-28 21:10:14] starting at [21:10:14]

javascript - 如何在没有 JavaScript 的情况下生成 Rails 应用程序?

javascript - 如何在 Firefox 中保存全屏状态?

javascript - mouseleave 的条件逻辑

javascript - 在 React 中调用 onClick 函数

javascript - 如何从一个 JavaScript 文件加载另一个文件?

javascript - 识别 IE10 与 IE8 中特殊字符的事件

javascript - 有什么方法可以覆盖浏览器呈现页面的方式吗?