class TournamentOrganizer::Tournament

Attributes

games[R]
players[R]

Public Class Methods

new() click to toggle source
# File lib/tournament_organizer/tournament.rb, line 5
def initialize
  @players = []
  @games = []
end

Public Instance Methods

add_games(*games) click to toggle source
# File lib/tournament_organizer/tournament.rb, line 15
def add_games *games
  @games.push(*games)
  @games.uniq!
end
add_players(*players) click to toggle source
# File lib/tournament_organizer/tournament.rb, line 10
def add_players *players
  @players.push(*players)
  @players.uniq!
end
get_all_fights() click to toggle source
# File lib/tournament_organizer/tournament.rb, line 53
def get_all_fights
  fights = []

  @players.each do |player|
    fights.push(*self.get_fights_for(player))
  end

  return fights.uniq!
end
get_fights_for(player) click to toggle source
# File lib/tournament_organizer/tournament.rb, line 64
def get_fights_for player
  return [] unless @players.include?(player)

  return @players.select{|other_player| other_player != player}.map {|other_player|  [player, other_player].sort }
end
get_organization() click to toggle source
# File lib/tournament_organizer/tournament.rb, line 20
def get_organization
  organization = []
  fights = self.get_all_fights

  while not fights.empty?
    round = []


    player_on_round = []

    # loop on all games
    @games.shuffle.each do |game|
      # quit loop if we use all fights
      break if fights.empty?

      # ensure than current fighters not already fight on this rouns
      current_fights = fights.select{|f| !player_on_round.include?(f.first) and !player_on_round.include?(f.last)}
      break if current_fights.empty?

      current_fight = current_fights.first

      round << {players: current_fight, game: game}
      player_on_round.push(*current_fight)

      fights.delete current_fight
    end

    organization << round
  end

  return organization
end