mirror of
https://github.com/AlecM33/Werewolf.git
synced 2025-12-26 15:57:50 +01:00
change shuffling function
This commit is contained in:
@@ -379,7 +379,7 @@ function initializePeopleForGame (uniqueCards, moderator) {
|
||||
}
|
||||
}
|
||||
|
||||
cards = shuffleArray(cards); // The deck should probably be shuffled, ey?.
|
||||
cards = shuffle(cards); // The deck should probably be shuffled, ey?.
|
||||
|
||||
let j = 0;
|
||||
if (moderator.userType === globals.USER_TYPES.TEMPORARY_MODERATOR) { // temporary moderators should be dealt in.
|
||||
@@ -410,13 +410,20 @@ function initializePeopleForGame (uniqueCards, moderator) {
|
||||
return people;
|
||||
}
|
||||
|
||||
function shuffleArray (array) {
|
||||
for (let i = 0; i < array.length; i ++) {
|
||||
const randIndex = Math.floor(Math.random() * i);
|
||||
const temp = array[i];
|
||||
array[i] = array[randIndex];
|
||||
array[randIndex] = temp;
|
||||
/*
|
||||
-- To shuffle an array a of n elements (indices 0..n-1):
|
||||
for i from n−1 downto 1 do
|
||||
j ← random integer such that 0 ≤ j ≤ i
|
||||
exchange a[j] and a[i]
|
||||
*/
|
||||
function shuffle(array) {
|
||||
for (let i = array.length - 1; i > 0; i --) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
const temp = array[j];
|
||||
array[j] = array[i];
|
||||
array[i] = temp;
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user