游戏生成器 - 同一支球队在同一轮中不应打超过两次



我还在学习JS,我正在尝试做这个游戏生成器,但我仍然有一个问题,同一支队伍不应该在同一回合中玩两次以上。如果有人能帮助我,我将不胜感激。谢谢!

这段代码要求用户选择有多少支球队参加比赛,然后要求用户输入球队的名字。


const btn = document.querySelector('#submitbtn')
const section = document.querySelector('#containertimes')
btn.addEventListener("click", function(event){
event.preventDefault()
const numTimes = parseInt (document.querySelector('#js-input-times').value)
section.innerHTML = ''
for(let i = 1; i <= numTimes; i++){
const input = document.createElement("input")
input.type = "text"
input.id = "nome-time"
input.name = "time-" + i
input.placeholder = "Nome do time " + i
input.required = true
section.appendChild(input)
}
const submitButton = document.createElement("button")
submitButton.type = "submit"
submitButton.innerText = "Gerar Jogos"
submitButton.addEventListener('click', gerarJogos)
section.appendChild(submitButton)
})
function shuffle(array) {
let currentIndex = array.length, randomIndex;
while (currentIndex != 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
}
function gerarJogos() {
const nomeTime = document.querySelectorAll("[name^='time-']")
let jogosPorRodada = []
for (let i = 0; i < nomeTime.length; i++) {
for (let j = i + 1; j < nomeTime.length; j++) {
jogosPorRodada[i] = jogosPorRodada[i] || []
jogosPorRodada[i].push(nomeTime[i].value +' x '+ nomeTime[j].value)
}
}
let jogosEmbaralhados = jogosPorRodada.map(function(jogos) {
return shuffle(jogos)
})
let resultado = ''
for (let i = 0; i < jogosEmbaralhados.length; i++) {
resultado += 'Rodada ' + (i+1) + '<br>' + jogosEmbaralhados[i].join('<br>') + '<br><br>'
}
section.innerHTML = resultado

}

我删除了洗牌功能,并修改了你的代码,以确保一个团队每轮只能玩一次,他们将能够与其他团队一起玩一次。它使用了一个循环,在轮数上迭代。在每一轮中,都有一个嵌套循环,迭代一半的团队数字进行配对。在每一轮之后,使用splice和pop来生成下一轮的新配对。

const btn = document.querySelector('#submitbtn')
const section = document.querySelector('#containertimes')
btn.addEventListener("click", function(event){
event.preventDefault()
const numTimes = parseInt(document.querySelector('#js-input-times').value)
section.innerHTML = ''
for(let i = 1; i <= numTimes; i++){
const input = document.createElement("input")
input.type = "text"
input.id = "team-name"
input.name = "team-" + i
input.placeholder = "Team name " + i
input.required = true
section.appendChild(input)
}
const submitButton = document.createElement("button")
submitButton.type = "submit"
submitButton.innerText = "Generate Games"
submitButton.addEventListener('click', generateGames)
section.appendChild(submitButton)
})
function generateGames() {
const teamName = document.querySelectorAll("[name^='team-']")
let teams = Array.from(teamName).map(team => team.value)
if (teams.length % 2 !== 0) {
teams.push('BYE')
}
let result = ''
let numRounds = teams.length - 1
for (let round = 0; round < numRounds; round++) {
result += 'Round ' + (round+1) + '<br>'
for (let i = 0; i < teams.length / 2; i++) {
if (teams[i] !== 'BYE' && teams[teams.length - i - 1] !== 'BYE') {
result += teams[i] + ' x ' + teams[teams.length - i - 1] + '<br>'
}
}
result += '<br>'
teams.splice(1, 0, teams.pop())
}
section.innerHTML = result
}
<!DOCTYPE html>
<html>
<head>
<title>Team Games Generator</title>
</head>
<body>
<h1>Team Games Generator</h1>
<form id="team-form">
<label for="js-input-times">Number of Teams:</label>
<input type="number" id="js-input-times" name="times" min="2" required>
<button type="submit" id="submitbtn">Generate Team Names</button>
</form>
<div id="containertimes"></div>
</body>
</html>

相关内容

  • 没有找到相关文章

最新更新