Skip to content

Commit

Permalink
* Shuffle agents before sorting.
Browse files Browse the repository at this point in the history
  Without this fix, on Safari (Mac), "grudger" ends up
  always dominating on step 4, not just sometimes.
  • Loading branch information
audreyt committed Aug 6, 2017
1 parent f6da0dc commit 71986e4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
13 changes: 12 additions & 1 deletion js/lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,15 @@ function _makeMovieClip(resourceName, options){
// Gimme
return mc;

}
}

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;
}
7 changes: 2 additions & 5 deletions js/sims/Tournament.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 71986e4

Please sign in to comment.