【问题标题】:Logic - Creating a schedule for a competition逻辑 - 为比赛制定时间表
【发布时间】:2021-11-24 06:51:32
【问题描述】:

这更像是一个逻辑问题,而不是一个编码问题,但对于大家的信息,我正在使用 Javascript。

我需要在遵守以下规则的 12 个团队之间以编程方式创建一个为期 14 周的时间表: - 每支球队至少与对方球队交手一次。 - 没有一支球队与另一支球队交手超过两次 -每队打14场比赛

我尝试循环遍历每个玩家,为每周创建比赛(游戏 1 - 玩家 1 与玩家 2,游戏 2 - 玩家 1 与玩家 3 等),然后检查当前玩家是否尚未玩在游戏中如此(游戏 1 - 跳过(玩家 2 已经在玩),游戏 2 - 玩家 2 对玩家 4(玩家 3 已经玩),游戏 3 - 玩家 2 对玩家 5)等等。所以它会为一个团队创建整个时间表,然后转移到下一个团队等等。

工作正常,但是当我添加逻辑以检查没有玩家玩另一个玩家超过两次时,它会中断并进入无限循环。

我尝试过每周进行并创建类似的比赛(Game1 - Ply1 vs Ply2,Game2 - Ply3 vs Ply4,Game3 等。检查玩家是否还没有玩游戏,但在添加预选赛时再次失败.

非常感谢任何建议或指出正确的方向!我可以将 UserID 存储在键值对或数组中,因此这些不是任何解决方案中的限制因素。

期望的最终结果是这样的:

Schedule:{
  Week1:{
    Game1:{
      Play1: somePlayer,
      Play2: someOtherPlayer
    },
    Game2:{
      Play1: somePlayer1,
      Play2: someOtherPlayer1
    },
    Game3:{
      Play1: somePlayer2,
      Play2: someOtherPlayer2
    }
    //etc. and so on with 6 games per week for 14 weeks.
  }
}

让我最接近的解决方案的基本框架(第一种方法):

let userArray = [1,2,3,4,5,6,7,8,9,10,11,12]
let schedule = {}

for(let x=0; x < userArray.length ; x++){
  for(let y = 0; y < 14; y++){
    //Check if schedule['WK'+y] exists and if not create it.
    //Check if userArray[x] is playing in any existing games in schedule['WK'+y]
    //Check if userArray[x+1] is playing in any existing games in schedule['WK'+y]
      //IF YES - TRY userArray[x+2] etc (loop back to 0 if x+y is >12)
      //IF NO - CREATE schedule['WK'+y]['Game'+x] = {Play1: userArray[x],Play2:userArray[x+whatever is accepted]
  }
}

【问题讨论】:

    标签: javascript loops recursion math logic


    【解决方案1】:

    (Game1 - Ply1 vs Ply2,Game2 - Ply3 vs Ply4,Game3 等)

    也许你想这样创作..

    let userArray = [1,2,3,4,5,6,7,8,9,10,11,12]
    let oneGame2Team = userArray.length/2
    
    let schedule = {}
    
    
    let i = 0
    for(let k = 1; k <= 14; k++){
        schedule[`Week${k}`] = {}
        let newWeek = schedule[`Week${k}`]
        for(let j = 0; j < oneGame2Team; j++) {
    
            let firstPlayer = `Play${userArray[i]}`
            let secondPlayer = `Play${userArray[i+=1]}`
            
            newWeek[`Game${j+1}`] = {}
            newWeek[`Game${j+1}`][firstPlayer] = `SomePlayer${j}`
            newWeek[`Game${j+1}`][secondPlayer] = `SomePlayer${j}`
    
            i++
    
        }
        i = 0
    }
    
    console.log(schedule)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2018-02-24
      • 2012-08-22
      • 2016-06-25
      相关资源
      最近更新 更多