From 71986e40b95c1e48bc175134419c6f7df48dfaec Mon Sep 17 00:00:00 2001 From: Audrey Tang Date: Sun, 6 Aug 2017 20:43:48 +0800 Subject: [PATCH] * Shuffle agents before sorting. Without this fix, on Safari (Mac), "grudger" ends up always dominating on step 4, not just sometimes. --- js/lib/helpers.js | 13 ++++++++++++- js/sims/Tournament.js | 7 ++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/js/lib/helpers.js b/js/lib/helpers.js index ebee2925..07a50160 100644 --- a/js/lib/helpers.js +++ b/js/lib/helpers.js @@ -173,4 +173,15 @@ function _makeMovieClip(resourceName, options){ // Gimme return mc; -} \ No newline at end of file +} + +function _shuffleArray(array) { + var tmp, current, top = array.length; + if(top) while(--top) { + current = Math.floor(Math.random() * (top + 1)); + tmp = array[current]; + array[current] = array[top]; + array[top] = tmp; + } + return array; +} diff --git a/js/sims/Tournament.js b/js/sims/Tournament.js index 12447345..d3bd0217 100644 --- a/js/sims/Tournament.js +++ b/js/sims/Tournament.js @@ -229,11 +229,8 @@ function Tournament(config){ self.agentsSorted = null; self.playOneTournament = function(){ PD.playOneTournament(self.agents, Tournament.NUM_TURNS); - self.agentsSorted = self.agents.slice(); - self.agentsSorted.sort(function(a,b){ - if(a.coins==b.coins) return (Math.random()<0.5); // if equal, random - return a.coins-b.coins; // otherwise, sort as per usual - }); + self.agentsSorted = _shuffleArray(self.agents.slice()); + self.agentsSorted.sort(function(a,b){ return a.coins-b.coins; }); }; // Get rid of X worst