Reputation: 183
I'm working on a Javascript code that simulates a football tournament, with 4 teams, and 2 round, each round has 3 journeys. The teams face each other in each journey, like this:
First journey: Team 1 x Team 2 Team 3 x Team 4
Second journey Team 2 x Team 3 Team 4 x Team 1
Each team has a randomly generated number of goals, and I know the easiest way would be to make a static code like this:
T1 = [];
T2 = [];
T3 = [];
T3 = [];
let pointsT1 = 0;
let pointsT2 = 0;
let pointsT3 = 0;
let pointsT4 = 0;
//First Journey
let goalsT1 = Math.floor(Math.random() * 10);
let goalsT2 = Math.floor(Math.random() * 10);
if (goalsT1 > goalsT2){
pointsT1 += 3;
} else {
if (goalsT1 === goalsT2){
pointsT1++;
pointsT2++;
} else {
pointsT2 += 3;
}
}
T1.push({goalsT1,pointsT1});
T2.push({goalsT2,pointsT2});
console.log(T1);
console.log(T2);
//Second Journey
let goalsT3 = Math.floor(Math.random() * 10);
let goalsT4 = Math.floor(Math.random() * 10);
if (goalsT3 > goalsT4){
pointsT3 += 3;
} else {
if (goalsT3 === goalsT4){
pointsT3++;
pointsT4++;
} else {
pointsT4 += 3;
}
}
T3.push({goalsT3,pointsT3});
T4.push({goalsT4,pointsT4});
console.log(T3);
console.log(T4);
And so on.
But doing so would make the code very long, and I don't want that, if there's any other way of doing so.
The issue is that I want to make something more dynamic, to sort the teams against each other, and compare the number of goals they scored in order to calculate the points the scored. I thought of using a while loop, but I'm struggling to find a way to organize the teams against each other in this random way, without the risk of having a team against itself, or showing up in both games during the same round.
while(numberJourneys > 3){
let goalsT1 = Math.floor(Math.random() * 10);
let goalsT2 = Math.floor(Math.random() * 10);
let goalsT3 = Math.floor(Math.random() * 10);
let goalsT4 = Math.floor(Math.random() * 10);
if (goalsT1 > goalsT2 || goalsT1 > goalsT3 || goalsT1 > goalsT4){
pointsT1 += 3;
} else {
if (goalsT2 > goalsT1 || goalsT2 > goalsT3 || goalsT2 > goalsT4){
pointsT2 += 3;
} else {
if (goalsT3 > goalsT1 || goalsT3 > goalsT2 || goalsT3 > goalsT4){
pointsT3 += 3;
} else {
if (goalsT4 > goalsT1 || goalsT4 > goalsT2 || goalsT4 > goalsT3){
pointsT4 += 3;
} else {
if (goalsT1 === goalsT2 || goalsT1 === goalsT3 || goalsT1 === goalsT4){
pointsT1++;
} else {
if (goalsT2 === goalsT1 || goalsT2 === goalsT3 || goalsT2 === goalsT4){
pointsT2++;
} else {
if(goalsT3 === goalsT1 || goalsT3 === goalsT2 || goalsT3 === goalsT4){
pointsT3++;
} else {
if(goalsT4 === goalsT1 || goalsT4 === goalsT2 || goalsT4 === goalsT3){
pointsT4++;
}
}
}
}
}
}
}
}
T1.push({goalsT1,pointsT1});
T2.push({goalsT2,pointsT2});
T3.push({goalsT3,pointsT3});
T4.push({goalsT4,pointsT4});
}
So basically my question is: how can I randomize the matches without repeating so I can have 2 matches were all 4 teams play against each other (without there being someone against itself)? Also, I mentioned there were 2 rounds, each one with 3 journeys. The second round would be just like the first one, except the order would be reversed, because the team that hosted the game in the 1st round becomes the visitor in the 2nd round. So if the 1st journey of the 1st round was like this:
Team 1 x Team 2
Team 3 x Team 4
The 1st journey of the 2nd round would be like this:
Team 2 x Team 1
Team 4 x Team 3
With this in mind, would it be too troublesome to randomize the matches? I mean, I have to store the matches and reverse them later.
I truly appreciate any kind of help!
Also
Upvotes: 1
Views: 1252
Reputation: 402
This is a quick help, hope you can continue working on the games array using a reduce or something: Note: the kCombination is taken from here
const teams = Array.from({ length: 4 }).map((_, i) => ({
name: i,
points: 0,
goals: 0,
}));
function kCombinations(set, k) {
let i;
let j;
let combs;
let head;
let tailcombs;
if (k > set.length || k <= 0) {
return [];
}
if (k === set.length) {
return [set];
}
if (k === 1) {
combs = [];
for (i = 0; i < set.length; i++) {
combs.push([set[i]]);
}
return combs;
}
combs = [];
for (i = 0; i < set.length - k + 1; i++) {
head = set.slice(i, i + 1);
tailcombs = kCombinations(set.slice(i + 1), k - 1);
for (j = 0; j < tailcombs.length; j++) {
combs.push(head.concat(tailcombs[j]));
}
}
return combs;
}
const games = kCombinations(teams, 2).map((match) => {
const [t1, t2] = match;
const t1Goals = Math.floor(Math.random() * 5);
const t2Goals = Math.floor(Math.random() * 5);
const t1Points = t1Goals > t2Goals ? 3 : t1Goals === t2Goals ? 1 : 0;
const t2Points = t2Goals > t1Goals ? 3 : t1Goals === t2Goals ? 1 : 0;
return [
{ name: t1.name, goals: t1Goals, points: t1Points },
{ name: t2.name, goals: t2Goals, points: t2Points },
];
});
Upvotes: 1