Javascript中随机足球比赛的生成算法
我正在编写一个Javascript代码来模拟足球比赛,有4支球队,2轮,每轮有3次旅程。各团队在每次旅程中面对面,如下所示:Javascript中随机足球比赛的生成算法,javascript,algorithm,Javascript,Algorithm,我正在编写一个Javascript代码来模拟足球比赛,有4支球队,2轮,每轮有3次旅程。各团队在每次旅程中面对面,如下所示: 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 = M
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);
第一次旅程:
第一队x第二队
第三队x第四队
第二次旅行
第二队x第三队
4队x 1队
每个团队都有随机生成的目标数,我知道最简单的方法是生成如下静态代码:
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);
等等
但是这样做会使代码非常长,如果有其他方法的话,我不希望这样做
问题是,我想做一些更具活力的事情,对各个球队进行排序,比较他们的进球数,以计算得分。我曾想过使用while循环,但我正在努力找到一种方法,以这种随机的方式组织团队相互对抗,而不会有团队自相残杀或在同一轮的两场比赛中出现的风险
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});
}
所以基本上我的问题是:我如何在不重复的情况下随机进行比赛,这样我就可以有两场比赛,如果所有4支球队都互相对抗(没有人对抗自己)?
此外,我还提到有两轮,每轮有三次行程。第二轮将与第一轮一样,只是顺序会颠倒,因为在第一轮主办比赛的球队将成为第二轮的访客。所以如果第一轮的第一次旅程是这样的:
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);
第一队x第二队
第三队x第四队
第二轮的第一次旅程如下:
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);
2队x 1队
4队x 3队
考虑到这一点,随机分配比赛会不会太麻烦?我的意思是,我必须存储匹配项,然后将其反转
我真的很感激任何帮助
另外这是一个快速帮助,希望您可以使用reduce或其他工具继续使用游戏阵列: 注意:kCombination取自
constteams=Array.from({length:4}).map({uu,i)=>({
姓名:我,,
分数:0,,
目标:0,
}));
函数kCombinations(set,k){
让我;
让j;
让梳子;
让头;
让尾梳;
如果(k>set.length | k{
常数[t1,t2]=匹配;
const t1Goals=Math.floor(Math.random()*5);
const t2Goals=Math.floor(Math.random()*5);
常量t1Points=t1Goals>t2Goals?3:t1Goals===t2Goals?1:0;
const t2Points=t2Goals>t1Goals?3:t1Goals==t2Goals?1:0;
返回[
{name:t1.name,goals:t1Goals,points:t1Points},
{name:t2.name,goals:t2Goals,points:t2Points},
];
});
我不知道这个kCombination,看起来很有趣!另外,非常感谢您抽出时间,我想我对如何继续下去有一个很好的想法!