Class: NeuroGammon::BestOfTournamentEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/neuro_gammon/best_of_tournament_engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(players, total_games) ⇒ BestOfTournamentEngine

Returns a new instance of BestOfTournamentEngine.

Raises:

  • (Exception)


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

#gamesObject (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

#playersObject (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_gamesObject (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

#winnersObject (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

#resetObject



21
22
23
24
# File 'lib/neuro_gammon/best_of_tournament_engine.rb', line 21

def reset
  @games=[]
  @winners=[]
end

#startObject



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

#summaryObject



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

#winnerObject



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