Class: TournamentOrganizer::Tournament

Inherits:
Object
  • Object
show all
Defined in:
lib/tournament_organizer/tournament.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTournament

Returns a new instance of Tournament.



5
6
7
8
# File 'lib/tournament_organizer/tournament.rb', line 5

def initialize
  @players = []
  @games = []
end

Instance Attribute Details

#gamesObject (readonly)

Returns the value of attribute games.



3
4
5
# File 'lib/tournament_organizer/tournament.rb', line 3

def games
  @games
end

#playersObject (readonly)

Returns the value of attribute players.



3
4
5
# File 'lib/tournament_organizer/tournament.rb', line 3

def players
  @players
end

Instance Method Details

#add_games(*games) ⇒ Object



15
16
17
18
# File 'lib/tournament_organizer/tournament.rb', line 15

def add_games *games
  @games.push(*games)
  @games.uniq!
end

#add_players(*players) ⇒ Object



10
11
12
13
# File 'lib/tournament_organizer/tournament.rb', line 10

def add_players *players
  @players.push(*players)
  @players.uniq!
end

#get_all_fightsObject



53
54
55
56
57
58
59
60
61
# 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) ⇒ Object



64
65
66
67
68
# 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_organizationObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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