Sha256: f2e1fc2e868d784ee2034e878ae29912fef665cdfdcdacb85ecda1ff5a43f4c4

Contents?: true

Size: 693 Bytes

Versions: 3

Compression:

Stored size: 693 Bytes

Contents

module FootballManager
  module TeamPicker
    def self.calculate_fair_teams(players)
      # players_in_descending_order = players.sort { |a,b| b.skill <=> a.skill }
      players_in_descending_order = []

      5.downto(1) do |i|
        players_in_descending_order <<
          players.select { |player| player.skill == i }.sort_by {rand}
      end

      team_a, team_b  = FootballManager::Team.new, FootballManager::Team.new

      players_in_descending_order.flatten.partition do |player|
        if team_a.empty? || team_a.points <= team_b.points
          team_a << player
        else
          team_b << player
        end
      end

      return [team_a, team_b]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
football-manager-0.1.2 lib/football-manager/team_picker.rb
football-manager-0.1.1 lib/football-manager/team_picker.rb
football-manager-0.1.0 lib/football-manager/team_picker.rb