Skip to content

Commit cd98d87

Browse files
committed
modernize
1 parent 442df10 commit cd98d87

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

Diff for: examples/elo.rb

+33-17
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,41 @@ def matchup(players)
1414

1515
def retire(players)
1616
retired = Set.new
17-
elo = players[0].elo
18-
(players.count / 10).times {
19-
i = rand(players.count - 1) + 1 # don't retire idx 0
20-
players[i] = Elo::Player.new(elo, skill: rand.round(3))
21-
retired.add i
17+
18+
# take 1 from the top 10 or 20
19+
if rand(3) % 2 == 1
20+
retired.add rand(10)
21+
else
22+
retired.add rand(10) + 10
23+
end
24+
# take the bottom 4
25+
(players.count - 4).upto(players.count - 1) { |i| retired.add i }
26+
# 5 others randomly outside the top 10 and bottom 5
27+
5.times { retired.add rand(players.count - 14) + 10 }
28+
29+
retired.each { |i|
30+
players[i] = Elo::Player.new(elo: ELO, skill: rand.round(3))
2231
}
2332
retired.sort
2433
end
2534

35+
ELO = Elo.new(initial: 1500)
36+
2637
num_players = 99
2738
num_matches = 9999
28-
elo = Elo.new(initial: 1500)
29-
players = Elo::Player.init_pool(num_players, elo).each { |p|
39+
retirement_periods = 3
40+
offset = 7
41+
42+
players = Elo::Player.init_pool(num_players, elo: ELO).each { |p|
3043
p.skill = rand.round(3)
3144
}
3245

46+
class Array
47+
def rank
48+
sort { |a, b| b <=> a }
49+
end
50+
end
51+
3352
puts
3453
puts "#{num_matches} matches without retirement"
3554
puts
@@ -39,26 +58,27 @@ def retire(players)
3958
players[a].simulate players[b]
4059
}
4160

42-
players.each { |p| puts p }
61+
puts players = players.rank
4362
puts
4463
puts "Avg Rating: #{avg_rating(players)}"
4564
puts
4665

4766
########
4867

49-
puts "#{num_matches} matches with 10x 10% retirement"
68+
puts "#{num_matches} matches with #{retirement_periods}x 10% retirement"
5069
puts
5170

5271
num_matches.times { |i|
5372
a, b = matchup(players)
5473
players[a].simulate players[b]
55-
if i % (num_matches / 10) == num_matches / 15
74+
if i % (num_matches / retirement_periods) == num_matches / offset
75+
players = players.rank
5676
puts format("Played %i matches, retired %s", i, retire(players).join(', '))
5777
end
5878
}
5979
puts
6080

61-
players.each { |p| puts p }
81+
puts players = players.rank
6282
puts
6383
puts "Avg Rating: #{avg_rating(players)}"
6484
puts
@@ -75,17 +95,13 @@ def retire(players)
7595
else
7696
players[a].simulate(players[b])
7797
end
78-
if i % (num_matches / 10) == num_matches / 15
98+
if i % (num_matches / retirement_periods) == num_matches / offset
7999
puts format("Played %i matches, retired %s", i, retire(players).join(', '))
80100
end
81101
}
82102
puts
83103

84-
players.each { |p| puts p }
104+
puts players = players.rank
85105
puts
86106
puts "Avg Rating: #{avg_rating(players)}"
87107
puts
88-
89-
puts "SORTED:"
90-
players.sort_by { |p| -1 * p.rating }.each { |p| puts p }
91-
puts

0 commit comments

Comments
 (0)