Class: NeuroGammon::BestOfTournamentEngine
- Inherits:
-
Object
- Object
- NeuroGammon::BestOfTournamentEngine
- Defined in:
- lib/neuro_gammon/best_of_tournament_engine.rb
Instance Attribute Summary collapse
-
#games ⇒ Object
readonly
Returns the value of attribute games.
-
#players ⇒ Object
readonly
Returns the value of attribute players.
-
#total_games ⇒ Object
readonly
Returns the value of attribute total_games.
-
#winners ⇒ Object
readonly
Returns the value of attribute winners.
Instance Method Summary collapse
-
#initialize(players, total_games) ⇒ BestOfTournamentEngine
constructor
A new instance of BestOfTournamentEngine.
- #reset ⇒ Object
- #start ⇒ Object
- #summary ⇒ Object
- #winner ⇒ Object
Constructor Details
#initialize(players, total_games) ⇒ BestOfTournamentEngine
Returns a new instance of BestOfTournamentEngine.
13 14 15 16 17 18 19 |
# File 'lib/neuro_gammon/best_of_tournament_engine.rb', line 13 def initialize players,total_games raise Exception.new("Has to be 2 players only") if players.size!=2 @total_games=total_games @players=players @games=[] @winners=[] end |
Instance Attribute Details
#games ⇒ Object (readonly)
Returns the value of attribute games.
12 13 14 |
# File 'lib/neuro_gammon/best_of_tournament_engine.rb', line 12 def games @games end |
#players ⇒ Object (readonly)
Returns the value of attribute players.
10 11 12 |
# File 'lib/neuro_gammon/best_of_tournament_engine.rb', line 10 def players @players end |
#total_games ⇒ Object (readonly)
Returns the value of attribute total_games.
9 10 11 |
# File 'lib/neuro_gammon/best_of_tournament_engine.rb', line 9 def total_games @total_games end |
#winners ⇒ Object (readonly)
Returns the value of attribute winners.
11 12 13 |
# File 'lib/neuro_gammon/best_of_tournament_engine.rb', line 11 def winners @winners end |
Instance Method Details
#reset ⇒ Object
21 22 23 24 |
# File 'lib/neuro_gammon/best_of_tournament_engine.rb', line 21 def reset @games=[] @winners=[] end |
#start ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/neuro_gammon/best_of_tournament_engine.rb', line 26 def start p=players.reverse total_games.downto(1) do |n| print("("<<n.to_s<<")") eng=GameEngine.new(players[0],players[1]) @games << eng.play_game @winners << eng.winner p=players.reverse end end |
#summary ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/neuro_gammon/best_of_tournament_engine.rb', line 50 def summary result="" players.each do |p| c=winners.select {|w| w==p}.size result << p.to_s << " won " << c.to_s << " games.\n" end return result end |
#winner ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/neuro_gammon/best_of_tournament_engine.rb', line 37 def winner max=-1 best=0 players.each_index do |i| c=winners.select {|w| w==players[i]}.size if (c>max) max=c best=i end end return players[best] end |