-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontest.rb
72 lines (55 loc) · 1.42 KB
/
contest.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
require 'bundler'
Bundler.require
require './judge.rb'
require './bot.rb'
class Contest
include Judge
def initialize(*names)
load_bots(*names)
@results = []
@win_counts = Hash.new(0)
end
def start
@bots.each_with_index do |bot, index|
@bots.slice((index + 1)..(-1)).each do |opponent|
fight! bot, opponent
end
end
print_overall_results
end
private
def load_bots(*names)
@bots = Dir['./bots/*.rb'].map do |path|
require path
File.basename(path).sub('.rb', '').classify.constantize
end
if names.any?
@bots.select! { |bot| names.include?(bot.name) }
end
end
def fight!(botclass1, botclass2)
bot1 = botclass1.new
bot2 = botclass2.new
results = 1000.times.map do
a, b = bot1.choose, bot2.choose
result = judge a, b
bot1.learn(a, b)
bot2.learn(b, a)
result
end
@results.push(:a => bot1, :b => bot2, :results => results)
sums = [results.count(1), results.count(2), results.count(0)]
@win_counts[bot1.name] += 1 if sums[0] > sums[1]
@win_counts[bot2.name] += 1 if sums[0] < sums[1]
print_avg_result(bot1, bot2, sums)
end
def print_overall_results
@win_counts.each_pair do |bot, count|
puts "#{bot} won #{count} time#{count != 1 ? 's' : ''}."
end
end
def print_avg_result(a, b, sums)
puts "#{a.name} vs. #{b.name}:"
puts ' ' + sums.join(' : ')
end
end