bridge 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/bridge/constants.rb +1 -1
- data/lib/bridge/points/chicago.rb +1 -10
- data/lib/bridge/points/duplicate.rb +39 -16
- data/lib/bridge/points.rb +9 -0
- data/lib/bridge/version.rb +1 -1
- data/lib/bridge.rb +1 -0
- data/test/test_duplicate.rb +59 -24
- metadata +5 -4
data/lib/bridge/constants.rb
CHANGED
@@ -26,16 +26,7 @@ module Bridge::Points
|
|
26
26
|
|
27
27
|
# Returns imps (negative or positive) based on given points
|
28
28
|
def imps
|
29
|
-
|
30
|
-
return (imps * sign) if range.include?(points_difference.abs)
|
31
|
-
end
|
32
|
-
nil
|
33
|
-
end
|
34
|
-
|
35
|
-
private
|
36
|
-
|
37
|
-
def sign
|
38
|
-
points >= 0 ? 1 : -1
|
29
|
+
Bridge::Points.imps(points_difference)
|
39
30
|
end
|
40
31
|
end
|
41
32
|
end
|
@@ -1,29 +1,52 @@
|
|
1
1
|
module Bridge::Points
|
2
2
|
class Duplicate
|
3
3
|
def initialize(*scores)
|
4
|
-
@scores = Array(scores).flatten
|
4
|
+
@scores = Array(scores).flatten.sort.reverse
|
5
5
|
end
|
6
6
|
|
7
|
-
def
|
8
|
-
@scores.
|
9
|
-
|
10
|
-
|
7
|
+
def theoretical_maximum
|
8
|
+
(@scores.size - 1) * 2
|
9
|
+
end
|
10
|
+
|
11
|
+
def skipped_scores(number = 1)
|
12
|
+
@scores[number..(-1 - number)]
|
13
|
+
end
|
14
|
+
|
15
|
+
def average_score(number_to_skip = 1)
|
16
|
+
scores = skipped_scores(number_to_skip)
|
17
|
+
(scores.inject(0.0) { |result, score| result += score} / scores.size).round(-1)
|
18
|
+
end
|
19
|
+
|
20
|
+
def maximum
|
21
|
+
{}.tap do |result|
|
22
|
+
@scores.each_with_index do |score, i|
|
23
|
+
result[score] ||= @scores[(i + 1)..-1].inject(0) { |points, s| points += (score <=> s) + 1 }
|
11
24
|
end
|
12
25
|
end
|
13
26
|
end
|
14
27
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
28
|
+
def maximum_in_percents(precision = 2)
|
29
|
+
maximum.tap do |result|
|
30
|
+
result.each do |score, points|
|
31
|
+
result[score] = (points * 100.0 / theoretical_maximum).round(precision)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
22
35
|
|
23
|
-
|
36
|
+
def butler(number_to_skip = 1)
|
37
|
+
{}.tap do |result|
|
38
|
+
@scores.each do |score|
|
39
|
+
result[score] = Bridge::Points.imps(score - average_score(number_to_skip))
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
24
43
|
|
25
|
-
|
26
|
-
|
27
|
-
|
44
|
+
def cavendish
|
45
|
+
{}.tap do |result|
|
46
|
+
@scores.each do |score|
|
47
|
+
result[score] ||= (@scores.inject(0.0) { |points, s| points += Bridge::Points.imps(score - s) } / (@scores.size - 1)).round(1)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
28
51
|
end
|
29
52
|
end
|
data/lib/bridge/version.rb
CHANGED
data/lib/bridge.rb
CHANGED
data/test/test_duplicate.rb
CHANGED
@@ -2,37 +2,72 @@ require "helper"
|
|
2
2
|
|
3
3
|
class TestDuplicate < Test::Unit::TestCase
|
4
4
|
test "maximum points with unique values" do
|
5
|
-
|
6
|
-
assert_equal 10,
|
7
|
-
assert_equal 8,
|
8
|
-
assert_equal 6,
|
9
|
-
assert_equal 4,
|
10
|
-
assert_equal 2,
|
11
|
-
assert_equal 0,
|
5
|
+
maximum = Bridge::Points::Duplicate.new(-200, -100, 620, 630, 660, 690).maximum
|
6
|
+
assert_equal 10, maximum[690]
|
7
|
+
assert_equal 8, maximum[660]
|
8
|
+
assert_equal 6, maximum[630]
|
9
|
+
assert_equal 4, maximum[620]
|
10
|
+
assert_equal 2, maximum[-100]
|
11
|
+
assert_equal 0, maximum[-200]
|
12
12
|
end
|
13
13
|
|
14
14
|
test "maximum points with non-unique values" do
|
15
|
-
|
16
|
-
assert_equal 9,
|
17
|
-
assert_equal 5,
|
18
|
-
assert_equal 2,
|
19
|
-
assert_equal 0,
|
15
|
+
maximum = Bridge::Points::Duplicate.new(420, 420, 400, 400, -50, -100).maximum
|
16
|
+
assert_equal 9, maximum[420]
|
17
|
+
assert_equal 5, maximum[400]
|
18
|
+
assert_equal 2, maximum[-50]
|
19
|
+
assert_equal 0, maximum[-100]
|
20
20
|
end
|
21
21
|
|
22
22
|
test "maximum points with non-unique values, without zero value" do
|
23
|
-
|
24
|
-
assert_equal 10,
|
25
|
-
assert_equal 6,
|
26
|
-
assert_equal 1,
|
23
|
+
maximum = Bridge::Points::Duplicate.new(430, 420, 420, 420, 300, 300).maximum
|
24
|
+
assert_equal 10, maximum[430]
|
25
|
+
assert_equal 6, maximum[420]
|
26
|
+
assert_equal 1, maximum[300]
|
27
27
|
end
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
29
|
+
test "maximum percents with non-unique values" do
|
30
|
+
maximum_in_percents = Bridge::Points::Duplicate.new(-630, 100, 100, -600, 200, -600, 100, 100, 100, -600, 100, 100, 100, 100).maximum_in_percents(1)
|
31
|
+
assert_equal 100.0, maximum_in_percents[200]
|
32
|
+
assert_equal 61.5, maximum_in_percents[100]
|
33
|
+
assert_equal 15.4, maximum_in_percents[-600]
|
34
|
+
assert_equal 0.0, maximum_in_percents[-630]
|
35
|
+
end
|
36
|
+
|
37
|
+
test "maximum percents with unique-values" do
|
38
|
+
maximum_in_percents = Bridge::Points::Duplicate.new(200, 170, 500, 430, 550, 420).maximum_in_percents(2)
|
39
|
+
assert_equal 100.0, maximum_in_percents[550]
|
40
|
+
assert_equal 80.0, maximum_in_percents[500]
|
41
|
+
assert_equal 60.0, maximum_in_percents[430]
|
42
|
+
assert_equal 40.0, maximum_in_percents[420]
|
43
|
+
assert_equal 20.0, maximum_in_percents[200]
|
44
|
+
assert_equal 0.0, maximum_in_percents[170]
|
45
|
+
end
|
46
|
+
|
47
|
+
test "butler with skipping the highest and the lowest score" do
|
48
|
+
butler = Bridge::Points::Duplicate.new(690, 660, 630, 620, -100, -200).butler
|
49
|
+
assert_equal 6, butler[690]
|
50
|
+
assert_equal 5, butler[660]
|
51
|
+
assert_equal 5, butler[630]
|
52
|
+
assert_equal 5, butler[620]
|
53
|
+
assert_equal -11, butler[-100]
|
54
|
+
assert_equal -12, butler[-200]
|
55
|
+
end
|
56
|
+
|
57
|
+
test "cavendish with unique values" do
|
58
|
+
cavendish = Bridge::Points::Duplicate.new(690, 660, 630, 620, -100, -200).cavendish
|
59
|
+
assert_equal 6.2, cavendish[690]
|
60
|
+
assert_equal 5.4, cavendish[660]
|
61
|
+
assert_equal 4.4, cavendish[630]
|
62
|
+
assert_equal 4.4, cavendish[620]
|
63
|
+
assert_equal -9.4, cavendish[-100]
|
64
|
+
assert_equal -11.0, cavendish[-200]
|
65
|
+
end
|
33
66
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
67
|
+
test "cavendish with non-unique values" do
|
68
|
+
cavendish = Bridge::Points::Duplicate.new(100, 100, 110, 140).cavendish
|
69
|
+
assert_equal 1.0, cavendish[140]
|
70
|
+
assert_equal -0.3, cavendish[110]
|
71
|
+
assert_equal -0.3, cavendish[100]
|
72
|
+
end
|
38
73
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 3
|
9
|
+
version: 0.1.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- "Jakub Ku\xC5\xBAma"
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-09-
|
18
|
+
date: 2010-09-15 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -54,6 +54,7 @@ files:
|
|
54
54
|
- lib/bridge/card.rb
|
55
55
|
- lib/bridge/constants.rb
|
56
56
|
- lib/bridge/deal.rb
|
57
|
+
- lib/bridge/points.rb
|
57
58
|
- lib/bridge/points/chicago.rb
|
58
59
|
- lib/bridge/points/duplicate.rb
|
59
60
|
- lib/bridge/score.rb
|
@@ -82,7 +83,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
82
83
|
requirements:
|
83
84
|
- - ">="
|
84
85
|
- !ruby/object:Gem::Version
|
85
|
-
hash: -
|
86
|
+
hash: -942411906783919221
|
86
87
|
segments:
|
87
88
|
- 0
|
88
89
|
version: "0"
|