以独特的组合抛掷玩家

用户1199595
var players = [
{ id: 1, name : 'player1'},
{ id: 2, name : 'player2'},
{ id: 3, name : 'player3'},
{ id: 4, name : 'player4'},
{ id: 5, name : 'player5'},
{ id: 6, name : 'player6'},
{ id: 7, name : 'player7'},
{ id: 8, name : 'player8'},
{ id: 9, name : 'player9'},
{ id: 10, name : 'player10'},
{ id: 11, name : 'player11'},
{ id: 12, name : 'player12'},
{ id: 13, name : 'player13'},
{ id: 14, name : 'player14'},
{ id: 15, name : 'player15'},
{ id: 16, name : 'player16'}]

我想与2位玩家折腾游戏,而2位玩家。因此,一轮是2比2的4场比赛。

一个球员永远不能与已经和他玩过的球员成为队友。

我想构建一个随机化所有游戏的功能。

所以我想要这样的东西,但是所有的游戏都在做。

然后他们一次播放4个游戏,然后切换玩家并开始4个新游戏。

games = [{ team1: [{ id: 1, name : 'player1'},{ id: 2, name : 'player2'}], team2 :[{ id: 3, name : 'player3'},{ id: 4, name : 'player4'}] }]
保罗

为了获得最大可能的游戏回合数的所有组合(每个玩家彼此玩一次),我使用https://math.stackexchange.com/a/3094469作为灵感。

// From: https://stackoverflow.com/a/12646864/9487478
const shuffleArray = (array) => {
  let shuffledArray = [...array];
  for (let i = shuffledArray.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]];
  }
  return shuffledArray;
}

// Note: The number of players needs to be even.
let createGames = (playerArr) => {
  let players = [...playerArr];
  const teamsPerRound = [], 
        rounds = mod = players.length - 1,
        gamesPerRound = 4,
        // Helper for checking how often a player is confronted with another player.
        confrontations = Array(players.length).fill().map(x => Array(players.length).fill(0));        
    
  // Inspired by: https://math.stackexchange.com/a/3094469
  // Create as many unique teams as possible, whereas within a round every player occurs exactly once.
  for (let i = 0; i < rounds; i++) {
    let team = [[
      players.length - 1, 
      (players.length + i) % mod
    ]];
    for (let k = 1; k < (players.length / 2); k++) {
      team.push([
        (players.length + i + k) % mod,
        (players.length + i - k) % mod
      ]);
    }
    teamsPerRound.push(team);
    console.log(`Teams-Round ${i+1}`, JSON.stringify(team));
  }

  
  // Now that we have teams, we can create the games. Let's shuffle the teams per round before to ensure it's more random.
  const games = shuffleArray(teamsPerRound).map(teams => {
    let roundMatches = [];
    teams = shuffleArray(teams);
    for (let i = 0; i < teams.length/2; i++) {
      let first = teams[i], second = teams[teams.length - 1 - i];

      roundMatches.push({
        team1: first.map(x => ({...players[x]})),
        team2: second.map(x => ({...players[x]}))
      })
      
      // Helper for checking how often a player is confronted with another player.
      first.forEach(x => second.forEach(y => (confrontations[x][y]++, confrontations[y][x]++)));
    }
    return roundMatches;
  });
    
  confrontations.forEach((x,i) => console.log(`Confrontations (playerIndex: ${i})`, JSON.stringify(x), x.reduce((acc, val) => acc += val)));

  return games;
}

var players = [
  { id: 1, name : 'player1'},
  { id: 2, name : 'player2'},
  { id: 3, name : 'player3'},
  { id: 4, name : 'player4'},
  { id: 5, name : 'player5'},
  { id: 6, name : 'player6'},
  { id: 7, name : 'player7'},
  { id: 8, name : 'player8'},
  { id: 9, name : 'player9'},
  { id: 10, name : 'player10'},
  { id: 11, name : 'player11'},
  { id: 12, name : 'player12'},
  { id: 13, name : 'player13'},
  { id: 14, name : 'player14'},
  { id: 15, name : 'player15'},
  { id: 16, name : 'player16'}
];

const games = createGames(players);

console.log("Matches", games);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python挑战:给定范围内质数的独特组合

来自分类Dev

获取六个玩家的所有组合

来自分类Dev

独特组合的算法

来自分类Dev

数字到独特的字符组合

来自分类Dev

名+姓+生日组合是否足够独特?

来自分类Dev

Powershell阵列中的独特组合-无重复组合

来自分类Dev

C语言中数字的独特组合

来自分类Dev

独特的字符串组合

来自分类Dev

无论位置如何,都能找到独特的组合

来自分类Dev

MySQL中的独特价值组合

来自分类Dev

集合中所有可能的独特组合

来自分类Dev

通过XSD的属性和元素的独特组合

来自分类Dev

独特元素的组合,无需重复

来自分类Dev

独特的航班组合与总乘客

来自分类Dev

熊猫计算的独特游戏玩家

来自分类Dev

计算符合特定条件的独特组合

来自分类Dev

从Array [String]列中获得项目对的独特组合

来自分类Dev

甲骨文:如何获得两组独特的组合

来自分类Dev

具有多种属性的独特组合

来自分类Dev

MongooseJS独特组合索引

来自分类Dev

串珠项链的独特组合

来自分类Dev

在两个独特的列组合上进行完全外部联接

来自分类Dev

10个问题的独特组合

来自分类Dev

集合中所有可能的独特组合

来自分类Dev

PHP查找顺序良好的独特组合

来自分类Dev

独特的从头到尾组合,可变循环后的所有持续时间

来自分类Dev

laravel 迁移 - 列类型 longText 的独特组合

来自分类Dev

在 Array swift 4 中总结独特的项目组合

来自分类Dev

独特的组合