class Tournament
def initialize
@players = []
end
def add_player(player)
@players << player
end
def sort_players
@players.sort!{|p1, p2| p1.score <=> p2.score}.reverse!
end
def to_s
str = "Average rating is: #{average_rating}\n"
@players.each do |p|
str += p.to_s
str += " AVG OPPONENT RATING: #{p.get_rating}"
str += " TOTAL: #{p.score} points"
str += " NEW RATING: #{new_rating(p).to_i}\n"
end
str
end
private
def total_rating
total_rating = 0
@players.each{|p| total_rating += p.get_rating}
total_rating
end
def average_rating
total_rating / @players.size
end
def average_opponent_ratings(curr_player_rating)
(total_rating - curr_player_rating) / (@players.size - 1)
end
def expected_score_per_round(player)
1.0 / (1.0 + 10.0**((average_opponent_ratings( player.get_rating ) - player.get_rating ) / 400.0) )
end
def new_rating(player)
player.get_rating + 40 * ( player.score * 1.0 - ( ( @players.size - 1) * expected_score_per_round(player)))
end
end
class Player
def initialize(name, rating, results)
@name = name
@rating = rating
@results = results
end
def get_rating
@rating
end
def score
@results.inject(0) { |sum,val| sum += val }
end
def to_s
str = @name + " #{@rating} "
@results.each {|r| str+=" #{r}"}
str
end
end
t = Tournament.new
file = File.new('tournament_results.txt', "r")
while (line = file.gets)
data = line.split # so data is an array of strings
name = data[0]
rating = data[1].to_i
results = []
data[2..data.length].each_with_index{|res,i| results[i]=res.to_i}
p = Player.new(name, rating, results)
t.add_player(p)
end
t.sort_players
puts t.to_s
Comments
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎