首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有固定匹配的随机组

具有固定匹配的随机组
EN

Stack Overflow用户
提问于 2021-06-22 20:00:03
回答 2查看 70关注 0票数 0

我随机分组进行一对一辩论,16个人被分成8个小组,他们将被分配8个随机话题和随机边(AFF& NEG)。下面是我用来生成随机团队的部分,比赛将在上午和下午举行,所以每轮8。出于某种原因,我们需要其中的两个总是处于匹配状态。我似乎不能以一种合乎逻辑的方式做到这一点。也就是说,林书豪总是和萨米·辛格辩论,双方可以改变。提前谢谢。

代码语言:javascript
复制
var team = [
"Sandra Tom*",
  "Jerry Lin*",
  "Josh Renaud*",
  "Katie Bostian",
  "Sammy Singh",
  "Nader Shehayed*",
  "Joseph Tu*",
  "James Kim"
];
var text = "";

function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    var randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    var temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}


function shuffler() {
  shuffle(team);
  document.getElementById("player-0").innerHTML = team[0];
  document.getElementById("player-1").innerHTML = team[1];
  document.getElementById("player-2").innerHTML = team[2];
  document.getElementById("player-3").innerHTML = team[3];
  document.getElementById("player-4").innerHTML = team[4];
  document.getElementById("player-5").innerHTML = team[5];
  document.getElementById("player-6").innerHTML = team[6];
  document.getElementById("player-7").innerHTML = team[7];
}

// var myInterval = setInterval(shuffler, 50);
// clearInterval(myInterval);

document.getElementById("random").addEventListener("click", shuffler);

window.addEventListener("keypress", checkKeyPressed, false);
 
function checkKeyPressed(e) {
    if (e.charCode == "32") {
      document.getElementById("random").addEventListener("click", shuffler);    
    }
}
代码语言:javascript
复制
body {
  background-color: #232323;
}

#team {
  box-shadow: 0px 0px 50px rgba(255,255,255,.25);
}

#team td {
  height: 75px;
  width: 300px;
  text-align: center;
  font-family: Helvetica, Arial, san-serif;
  text-transform: uppercase;
  font-weight: bold;
  background-color: #000000;
  color: #ffffff;
  font-size: 24px;
  padding: 0px;
}

#team .title {
  height: 30px;
  font-size: 16px;
}

#team .blue {
  background-color: dodgerblue;
}

#team .red {
  background-color: tomato;
}

#team .green {
  background-color: olive;
}

#team .orange {
  background-color: orange;
}

#team .grey {
  background-color: #686868;
}

.fade {
   opacity: 1;
   transition: opacity .1s ease-in-out;
   -moz-transition: opacity .1s ease-in-out;
   -webkit-transition: opacity .1s ease-in-out;
   }
代码语言:javascript
复制
  <table align="center" id="team" cellpadding="0" cellspacing="0">
    <tr>
      <td colspan="2" class="title">
        Team 1
      </td>
    </tr>
    <tr>
      <td id="player-0" class="blue"></td>
      <td id="player-1" class="blue"></td>
    </tr>
    <tr>
      <td colspan="2" class="title">
        Team 2
      </td>
    </tr>
    <tr>
      <td id="player-2" class="red"></td>
      <td id="player-3" class="red"></td>
    </tr>
    <tr>
      <td colspan="2" class="title">
        Team 3
      </td>
    </tr>
    <tr>
      <td id="player-4" class="green"></td>
      <td id="player-5" class="green"></td>
    </tr>
    <tr>
      <td colspan="2" class="title">
        Team 4
      </td>
    </tr>
    <tr>
      <td id="player-6" class="orange"></td>
      <td id="player-7" class="orange"></td>
    </tr>
  </table>
  <table width="600" align="center">
    <tr>
      <td colspan="2" align="center">
        <button id="random">RANDOM</button>
      </td>
    </tr>
  </table>

更新:附加的代码片段。来澄清这个问题。我们希望林书豪总是和萨米·辛格辩论。8个主题分为上午和下午,16个人分为8个小组,所有这些都是独一无二的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-06-22 20:57:51

定义两个总是在同一个团队中的人,然后修改洗牌后的团队列表。在下面查看我的解决方案:

代码语言:javascript
复制
var team = [
"Sandra Tom*",
  "Jerry Lin*",
  "Josh Renaud*",
  "Katie Bostian",
  "Sammy Singh",
  "Nader Shehayed*",
  "Joseph Tu*",
  "James Kim"
];
var text = "";

function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    var randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    var temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

function hack(array, p1, p2){
  var p1Index = array.indexOf(p1);
  var p2Index = array.indexOf(p2);

  if(p1Index % 2 === 0){
    var temp = array[p1Index+1]
    array[p1Index+1] = array[p2Index]
  } else {
    var temp = array[p1Index-1]
    array[p1Index-1] = array[p2Index]
  }

  array[p2Index] = temp
  return array
}

function shuffler() {
  var shuffled = shuffle(team)
  var hacked = hack(shuffled, "Jerry Lin*", "Sammy Singh")
  var final = hacked

  document.getElementById("player-0").innerHTML = final[0];
  document.getElementById("player-1").innerHTML = final[1];
  document.getElementById("player-2").innerHTML = final[2];
  document.getElementById("player-3").innerHTML = final[3];
  document.getElementById("player-4").innerHTML = final[4];
  document.getElementById("player-5").innerHTML = final[5];
  document.getElementById("player-6").innerHTML = final[6];
  document.getElementById("player-7").innerHTML = final[7];
}

// var myInterval = setInterval(shuffler, 50);
// clearInterval(myInterval);

document.getElementById("random").addEventListener("click", shuffler);

window.addEventListener("keypress", checkKeyPressed, false);
 
function checkKeyPressed(e) {
    if (e.charCode == "32") {
      document.getElementById("random").addEventListener("click", shuffler);
    }
}
代码语言:javascript
复制
body {
  background-color: #232323;
}

#team {
  box-shadow: 0px 0px 50px rgba(255,255,255,.25);
}

#team td {
  height: 75px;
  width: 300px;
  text-align: center;
  font-family: Helvetica, Arial, san-serif;
  text-transform: uppercase;
  font-weight: bold;
  background-color: #000000;
  color: #ffffff;
  font-size: 24px;
  padding: 0px;
}

#team .title {
  height: 30px;
  font-size: 16px;
}

#team .blue {
  background-color: dodgerblue;
}

#team .red {
  background-color: tomato;
}

#team .green {
  background-color: olive;
}

#team .orange {
  background-color: orange;
}

#team .grey {
  background-color: #686868;
}

.fade {
   opacity: 1;
   transition: opacity .1s ease-in-out;
   -moz-transition: opacity .1s ease-in-out;
   -webkit-transition: opacity .1s ease-in-out;
   }
代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">
  <title>CodePen - Random Team Generator</title>
  <!-- <link rel="stylesheet" href="./style.css"> -->
  
</head>
<body>
<!-- partial:index.partial.html -->
<body>
  <table align="center" id="team" cellpadding="0" cellspacing="0">
    <tr>
      <td colspan="2" class="title">
        Team 1
      </td>
    </tr>
    <tr>
      <td id="player-0" class="blue"></td>
      <td id="player-1" class="blue"></td>
    </tr>
    <tr>
      <td colspan="2" class="title">
        Team 2
      </td>
    </tr>
    <tr>
      <td id="player-2" class="red"></td>
      <td id="player-3" class="red"></td>
    </tr>
    <tr>
      <td colspan="2" class="title">
        Team 3
      </td>
    </tr>
    <tr>
      <td id="player-4" class="green"></td>
      <td id="player-5" class="green"></td>
    </tr>
    <tr>
      <td colspan="2" class="title">
        Team 4
      </td>
    </tr>
    <tr>
      <td id="player-6" class="orange"></td>
      <td id="player-7" class="orange"></td>
    </tr>
  </table>
  <table width="600" align="center">
    <tr>
      <td colspan="2" align="center">
        <button id="random">RANDOM</button>
      </td>
    </tr>
  </table>
</body>
<!-- partial -->
  <!-- <script  src="./script.js"></script> -->
 
</body>
</html>

票数 1
EN

Stack Overflow用户

发布于 2021-06-22 20:26:11

根据您的评论:

代码语言:javascript
复制
var team = [
  ["Sandra Tom*","Jerry Lin*"],
  ["Josh Renaud*","Katie Bostian"],
  ["Sammy Singh","Nader Shehayed*"],
  ["Joseph Tu*","James Kim"]
];
var text = "";

function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    var randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    var temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}


function shuffler() {
  shuffle(team);
  document.getElementById("player-0").innerHTML = team[0][0];
  document.getElementById("player-1").innerHTML = team[0][1];
  document.getElementById("player-2").innerHTML = team[1][0];
  document.getElementById("player-3").innerHTML = team[1][1];
  document.getElementById("player-4").innerHTML = team[2][0];
  document.getElementById("player-5").innerHTML = team[2][1];
  document.getElementById("player-6").innerHTML = team[3][0];
  document.getElementById("player-7").innerHTML = team[3][1];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68083165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档