-2

I have 5 arrays. I want to combine these 5 arrays and then shuffle them respectively.

I'm using the Fisher-Yates shuffle method.

The code:

for (let i = 0; i < numPlayersIsNumLoops; i++) {    
movePlayerToTeam = maxPlayers.shift();
cmpPlayer = QueryPlayerIDInterface(movePlayerToTeam);
playerTeam = cmpPlayer.GetTeam(movePlayerToTeam);
playerTeam = playerTeam+1;
    if (playerTeam == 0) {
     team0.push(movePlayerToTeam);
    }if (playerTeam == 1) {
     team1.push(movePlayerToTeam);
    } if (playerTeam == 2) {
     team2.push(movePlayerToTeam);
    } if (playerTeam == 3) {
     team3.push(movePlayerToTeam);
    } if (playerTeam == 4) {
     team4.push(movePlayerToTeam);
    }
}
/* Positions inside teams also gets shuffled to further reduce the chance of 
 getting the same spot on subsequent playthroughs (also this way you never know who is next to you) */
if (numPlayers === 8) {
team0 = shuffleArray(team0);
team1 = shuffleArray(team1);
team2 = shuffleArray(team2);
team3 = shuffleArray(team3);
team4 = shuffleArray(team4);
playersShuffledAsTeams = playersShuffledAsTeams.concat(team1, team2, team3, team4, team0); // this is what i want shuffled aswell

The last line of code is what i want to shuffle.

The results i'm aiming for is something like this after a shuffle:

(team2, team0, team1, team3, team4) or (team0, team3, team1, team2, team4)

The solution with the answer below is:

playersShuffledAsTeams = playersShuffledAsTeams.concat(team1, team2, team3, team4, team0);
playersShuffledAsTeams = shuffleArray([team0, team1, team2, team3, team4]);
  • 1
    Does this answer your question? [How to randomize (shuffle) a JavaScript array?](https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array) – Reyno Jan 20 '21 at 12:16
  • Wouldn't `shuffleArray(playersShuffledAsTeams)` do that shuffle already for you? – Anderson Jan 20 '21 at 12:19
  • It doesnt, because i want to shuffle a variable holding 5 arrays, without breaking them @reyno –  Jan 20 '21 at 12:20
  • can you provide your implementation of `shuffleArray` then? From what I understand what you'd want is a `shuffleArray` implementation that wouldn't do a "deep shuffle" in internal arrays. – Anderson Jan 20 '21 at 12:22
  • Check the code. Also what you suggested breaks team arrays. @Anderson –  Jan 20 '21 at 12:24
  • What do you mean "breaking"? – Dexygen Jan 20 '21 at 12:28
  • I don't believe that the last line can be assumed as correct. playersShuffledAsTeams might have never been initialized, thus rendering the `playersShuffledAsTeams.concat(...)` useless - at least that's what I assume – Pascal Stockert Jan 20 '21 at 12:30
  • Anderson his way broke for example team1 array meaning the number inside it gets split up and are not a pair anymore. Anyway, the other person had the answer to my issue. @Dexygen –  Jan 20 '21 at 12:31
  • @PascalStockert They are correct because i double check that with logs. –  Jan 20 '21 at 12:32

1 Answers1

0
playersShuffledAsTeams = shuffleArray([team0, team1, team2, team3, team4]);

That do the trick for you?

Pascal Stockert
  • 356
  • 1
  • 12