2012-06-02 17 views
2

我的JavaScript代碼中有一個數學問題。我需要將給定數量的玩家隨機分成兩個小組,每次 - 如果玩家想再玩一次 - 球隊再次形成,他們應該有所不同,直到所有組合形成爲止。如何隨機多次將x個玩家分成兩支隊伍,每次都會有所不同?

比方說,我有4名球員,因此,所有的組合如下:
[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]

然而,因爲球隊側不算數,只有3個不同的組合:

[1,2] vs [3,4] 
[1,3] vs [2,4] 
[1,4] vs [2,3] 

當所玩遊戲的數量超過組合數量時,應該重新開始......即隨機選擇三個組合中的一個,隨機選擇下一個組合......

但是有一個轉折點......當我的數學技能很奇怪時,我的數學技能非常難以向南,而其中一名球員需要休息一場比賽。所以,有5名球員的所有比賽組合是(最後一個數字是球員休息):

[1,2] vs [3,4] [5] 
[1,2] vs [3,5] [4] 
[1,2] vs [4,5] [3] 

[1,3] vs [2,4] [5] 
[1,3] vs [2,5] [4] 
[1,3] vs [4,5] [2] 

[1,4] vs [2,3] [5] 
[1,4] vs [2,5] [3] 
[1,4] vs [3,5] [2] 

[1,5] vs [2,3] [4] 
[1,5] vs [2,4] [3] 
[1,5] vs [3,4] [2] 

[2,3] vs [4,5] [1] 
[2,4] vs [3,5] [1] 
[2,5] vs [3,4] [1] 

在JavaScript中怎麼可能讓這些球隊形成?即走進心靈

的一件事是給每個玩家一個獨特的價值(10^X),例如:

player1.value = 10; 
player2.value = 100; 
player3.value = 1000; 
player4.value = 10000; 

...然後循環,形成團隊檢查時,如果一個球隊的總價值等於最後的值。

有人可以用數學/ JavaScriptly更有才華請幫我解決這個編碼問題。謝謝!

回答

3

使用值是一個好主意,但是如果你讓他們有點面具,那麼檢查哪些球員已經被匹配更容易。 例如

player1.value = 1 
player2.value = 2 
player3.value = 4 
//(dynamically player n would have value 1 << (n-1)) 

通過檢查與其他玩家的面膜,它可以,如果他們已經被加上進行檢查,因爲他們已經有了自己的屏蔽值,他們永遠不能與自身匹配。 至於隨機因素,我認爲最簡單的方法是首先創建所有組合,就像在你的例子中做的那樣,然後用這些組合的數組作爲選擇隨機匹配的基礎。 如果您覺得這種方法是一個好方法,但是執行起來有困難,我可以嘗試製作一些示例代碼。

編輯這裏的示例代碼,希望評論有助於理清自己的意思 EDIT2改變團隊組合

var playercount = 5; 

    //add players 
    var players = new Array(); 
    for (var i = 0; i < playercount; i++) { 
     players[i] = { Index: i, Mask: 1 << i, Name: "Player" + (i + 1), toString: function() { return this.Name; } }; 
     //about the 1 << i: "<<" is a so called bit wise shift to the left. 
     //1 << i has the same outcome as 2 to the power of i 
    } 

    //the line below would print all players 
    //for (var pi in players) { var p = players[pi]; document.write(p + " (Mask:" + p.Mask + ")<br>"); } document.writeln("<br>"); 

    //create all possible team combinations 
    var teams = new Array(); 

    var playersPerTeam = Math.floor(playercount/2); 
    function Team(){ 
     this.list = new Array(); 
     this.mask = 0; 
     this.count = 0; 
     this.full =false; 

     this.Add = function (i) { 
      this.list.push(players[i]); 
      this.mask |= players[i].Mask; 
      this.full = ++this.count === playersPerTeam; 
     } 

     this.toString = function() { 
      var res = "", cnt = this.list.length; 
      for (var i = 0; i < cnt; i++) { 
       if (i > 0) 
        res += i == cnt - 1 ? " and " : ","; 
       res += this.list[i].Name; 
      } 
      return res; 
     } 
    } 


    function addplayers() { 
     var indices = new Array(playersPerTeam); 
     for (var p = 0; p < playersPerTeam; p++) indices[p] = p; 
     var l = playersPerTeam - 1; 

     function addteam() { 
      var team = new Team(); 
      for (var p = 0; p < playersPerTeam; p++) team.Add(indices[p]); 
      teams.push(team); 
     } 

     function addplayer(start, depth) { 
      var target = players.length - playersPerTeam + depth + 1; 
      var team; 
      for (var i = start; i < target; i++) { 
       indices[depth] = i; 
       if (depth == l) 
        addteam(); 
       else 
        addplayer(i + 1, depth + 1); 
      } 
     } 

     addplayer(0, 0); 

    } 
    addplayers(); 




    //the line below would print the team combinations 
    //for (var te in teams) { var t = teams[te]; document.write(t + " (mask:" + t.mask + ")<br>"); } document.writeln("<br>"); 

    //create matches 
    var matches = new Array(); 
    //the matches can be created in the same way as the teams, only the first team cannot have players of the second team 
    for (var i = 0; i < teams.length; i++) { 
     for (var j = i + 1; j < teams.length; j++) { 
      var t1 = teams[i], t2 = teams[j]; 
      if ((t1.mask & t2.mask) === 0) //this is where the masks come in, 
       matches.push({ Team1: t1, Team2: t2, toString: function() { return this.Team1 + " versus " + this.Team2 } }); 
     } 
    } 

    //randomize matches. Instead of picking a random match per turn, we can randomize at the 
    //start, so we know all the matches in advance. 
    //this can be done by using a sort on the array with a random index 
    //NB, this isn't the most random randomness (or whatever you call it LOL). For better shuffling 
    //there are several alternatives, but perhaps this one is enough 
    matches.sort(function() { return (parseInt(Math.random() * 100) % 2);}); 


    //the line below prints the matches 
    for (var mi in matches) { document.write(matches[mi] + "<br>"); } document.writeln("<br>"); 
+0

聽起來不錯 - 創建所有組合,然後隨機選取 - 但對「位掩碼」和「1 <<(n-1)」沒有任何理解。一些示例代碼會很棒! – micadelli

+0

對不起,遲到的迴應,我一直在喝咖啡和晚餐:)我已經將樣品添加到 –

+0

以上的帖子中了!試圖運行這個和它的工作,雖然我還沒有得到那些「面具:球員[我] .Mask |球員[j] .Mask」和「如果((t1.mask&t2.mask)=== 0) 「... 但我會。然而,我不知道這是否是因爲我的英文不好,但是這將球員分成了兩名球員,我只想將所有球員分成兩個獨立的球隊。所以,9名球員將被分爲5比4。代碼應該如何改變。 – micadelli

1

對於偶數情況,只需挑選您沒有隨機使用的號碼並組隊。

對於奇怪的情況,隨機挑選一個數字坐下。然後剩餘的數字就像一個偶然的情況。

+0

通過「接你有沒有隨意使用的數字,讓球隊」你的意思是數組所有玩家的指數,對吧?如何不再組成同一個團隊?更重要的是,我該如何組建這些團隊?你能提供一個真正的/僞代碼的例子。 ty – micadelli

相關問題