You need to enable JavaScript to run this app. 如何在JS实现的1v1辩论赛随机分组逻辑中添加固定对阵规则?
优惠活动
大模型
产品
解决方案
定价
更多

如何在JS实现的1v1辩论赛随机分组逻辑中添加固定对阵规则?

1v1辩论赛随机分组工具调整方案

实现逻辑

  • 先提取强制对阵的两名选手(Jerry Lin、Sammy Singh),其余选手单独存入待分组名单
  • 随机决定强制配对的正反方,生成第一组对阵
  • 将剩余14名选手随机打乱,按顺序两两配对,生成其余7组对阵,保证所有对阵不重复
  • 打乱预设的8个辩题,随机分配给8组
  • 将8组对阵再次打乱,前4组设为上午场,后4组设为下午场
  • 最终结果直接渲染到页面

调整后完整代码

HTML代码

<table align="center" id="team" cellpadding="0" cellspacing="0">
  <!-- 上午场4组 -->
  <tr>
    <td colspan="2" class="session-title">上午场</td>
  </tr>
  <tr>
    <td colspan="2" class="title" id="topic-0"></td>
  </tr>
  <tr>
    <td id="player-0-aff" class="blue">正方(AFF)</td>
    <td id="player-0-neg" class="red">反方(NEG)</td>
  </tr>
  <tr>
    <td colspan="2" class="title" id="topic-1"></td>
  </tr>
  <tr>
    <td id="player-1-aff" class="blue">正方(AFF)</td>
    <td id="player-1-neg" class="red">反方(NEG)</td>
  </tr>
  <tr>
    <td colspan="2" class="title" id="topic-2"></td>
  </tr>
  <tr>
    <td id="player-2-aff" class="blue">正方(AFF)</td>
    <td id="player-2-neg" class="red">反方(NEG)</td>
  </tr>
  <tr>
    <td colspan="2" class="title" id="topic-3"></td>
  </tr>
  <tr>
    <td id="player-3-aff" class="blue">正方(AFF)</td>
    <td id="player-3-neg" class="red">反方(NEG)</td>
  </tr>
  <!-- 下午场4组 -->
  <tr>
    <td colspan="2" class="session-title">下午场</td>
  </tr>
  <tr>
    <td colspan="2" class="title" id="topic-4"></td>
  </tr>
  <tr>
    <td id="player-4-aff" class="blue">正方(AFF)</td>
    <td id="player-4-neg" class="red">反方(NEG)</td>
  </tr>
  <tr>
    <td colspan="2" class="title" id="topic-5"></td>
  </tr>
  <tr>
    <td id="player-5-aff" class="blue">正方(AFF)</td>
    <td id="player-5-neg" class="red">反方(NEG)</td>
  </tr>
  <tr>
    <td colspan="2" class="title" id="topic-6"></td>
  </tr>
  <tr>
    <td id="player-6-aff" class="blue">正方(AFF)</td>
    <td id="player-6-neg" class="red">反方(NEG)</td>
  </tr>
  <tr>
    <td colspan="2" class="title" id="topic-7"></td>
  </tr>
  <tr>
    <td id="player-7-aff" class="blue">正方(AFF)</td>
    <td id="player-7-neg" class="red">反方(NEG)</td>
  </tr>
</table>
<table width="600" align="center">
  <tr>
    <td colspan="2" align="center">
      <button id="random">RANDOM</button>
    </td>
  </tr>
</table>

CSS代码

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 .session-title {
  height: 40px;
  font-size: 20px;
  background-color: #686868;
}

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

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

.fade {
   opacity: 1;
   transition: opacity .1s ease-in-out;
   -moz-transition: opacity .1s ease-in-out;
   -webkit-transition: opacity .1s ease-in-out;
}

JS代码

// 16名参赛选手名单,可自行修改补充
var allPlayers = [
  "Sandra Tom*",
  "Jerry Lin*",
  "Josh Renaud*",
  "Katie Bostian",
  "Sammy Singh",
  "Nader Shehayed*",
  "Joseph Tu*",
  "James Kim",
  "Lisa Wong",
  "Mike Chen",
  "Sarah Lee",
  "David Park",
  "Emma Davis",
  "Ryan Wilson",
  "Olivia Taylor",
  "Ben Brown"
];
// 8个预设辩题,可自行修改
var debateTopics = [
  "辩题1:人工智能是否会取代人类大部分工作",
  "辩题2:高校是否应该全面实行宽进严出",
  "辩题3:外卖平台是否应该为骑手社保承担主要责任",
  "辩题4:元宇宙的发展机遇大于风险",
  "辩题5:流量明星对影视行业发展利大于弊",
  "辩题6:新能源汽车终将全面取代燃油车",
  "辩题7:远程办公对员工发展利大于弊",
  "辩题8:未成年人上网应该实行全面实名制"
];
// 强制配对的两名选手,可修改
const forcedPair = ["Jerry Lin*", "Sammy Singh"];

function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;
  while (0 !== currentIndex) {
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }
  return array;
}

function shuffler() {
  // 提取强制配对选手,生成剩余选手列表
  const restPlayers = allPlayers.filter(p => !forcedPair.includes(p));
  shuffle(restPlayers);
  
  // 随机生成强制配对的正反方
  let fixedMatch = [...forcedPair];
  if(Math.random() > 0.5) fixedMatch.reverse();
  let allMatches = [[fixedMatch[0], fixedMatch[1]]];
  
  // 剩余选手两两配对
  for(let i=0; i<restPlayers.length; i+=2) {
    allMatches.push([restPlayers[i], restPlayers[i+1]]);
  }
  
  // 随机分配辩题
  shuffle(debateTopics);
  // 打乱所有对阵,分上下午场
  shuffle(allMatches);
  
  // 渲染结果到页面
  for(let i=0; i<8; i++) {
    document.getElementById(`topic-${i}`).innerHTML = debateTopics[i];
    document.getElementById(`player-${i}-aff`).innerHTML = `AFF: ${allMatches[i][0]}`;
    document.getElementById(`player-${i}-neg`).innerHTML = `NEG: ${allMatches[i][1]}`;
  }
}

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

window.addEventListener("keypress", checkKeyPressed, false);
function checkKeyPressed(e) {
  if (e.charCode == "32") {
    shuffler();
  }
}

内容的提问来源于stack exchange,提问作者Godinall

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.15 19:15:08