Заранее благодарен за вашу помощь в этом упражнении, правда, я не обнаружил, как его решить
Как динамически генерировать возможные столкновения между командами?
Имея следующие поля ввода
- дата начала
- команды
- поля
- дни для игры
Например, со следующими данными
const startDate = "03-08-2020";
const teams = ["A", "B", "C", "D", "E", "F"];
const fields = ["Field1", "Field2"];
const daysToPlay = ["Monday", "Wednesday"];
Теперь, исходя из этих данных, мне нужно динамически генерировать игровые дни с их соответствием играм, игровые поля должны меняться местами, как дни от даты к дате. Начиная со значения 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
В примере , дни соответствуют Monday (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 });
В это время установите дату начала для каждого дня
Спасибо