bridge 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -77,7 +77,7 @@ module Bridge
77
77
  2500..2990 => 21,
78
78
  3000..3490 => 22,
79
79
  3500..3990 => 23,
80
- 4000..7600 => 24
80
+ 4000..15200 => 24
81
81
  }
82
82
  class Chicago
83
83
  # values in array: [not-vulnerable, vulnerable]
@@ -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
- IMPS.each do |range, imps|
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 max
8
- @scores.inject({}) do |result, score|
9
- result.tap do |r|
10
- r[score] ||= @scores.inject(-1) { |points, s| points += (score <=> s) + 1 }
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
- # def max_percents
16
- # max.tap do |result|
17
- # result.each do |score, points|
18
- # result[score] = points * 100.0 / theoretical_max
19
- # end
20
- # end
21
- # end
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
- # protected
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
- # def theoretical_max
26
- # (@scores.size - 1) * 2
27
- # end
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
@@ -0,0 +1,9 @@
1
+ module Bridge
2
+ module Points
3
+ def self.imps(points_difference)
4
+ sign = (points_difference >= 0) ? 1 : -1
5
+ IMPS.each { |range, imps| return (imps * sign) if range.include?(points_difference.abs) }
6
+ nil
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module Bridge
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/bridge.rb CHANGED
@@ -2,6 +2,7 @@ require "bridge/bid"
2
2
  require "bridge/card"
3
3
  require "bridge/constants"
4
4
  require "bridge/deal"
5
+ require "bridge/points"
5
6
  require "bridge/points/chicago"
6
7
  require "bridge/points/duplicate"
7
8
  require "bridge/score"
@@ -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
- max = Bridge::Points::Duplicate.new(-200, -100, 620, 630, 660, 690).max
6
- assert_equal 10, max[690]
7
- assert_equal 8, max[660]
8
- assert_equal 6, max[630]
9
- assert_equal 4, max[620]
10
- assert_equal 2, max[-100]
11
- assert_equal 0, max[-200]
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
- max = Bridge::Points::Duplicate.new(420, 420, 400, 400, -50, -100).max
16
- assert_equal 9, max[420]
17
- assert_equal 5, max[400]
18
- assert_equal 2, max[-50]
19
- assert_equal 0, max[-100]
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
- max = Bridge::Points::Duplicate.new(430, 420, 420, 420, 300, 300).max
24
- assert_equal 10, max[430]
25
- assert_equal 6, max[420]
26
- assert_equal 1, max[300]
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
- # test "maximum percents" do
30
- # max_percents = Bridge::Points::Duplicate.new(50, 400, 110, 100, 110, 120, 50, 120, 140, 420, 110, 110, 110, 110, 50, 110, 140, 110).max_percents
31
- # notify max_percents.inspect
32
- # end
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
- # test "maximum percents 2" do
35
- # max_percents = Bridge::Points::Duplicate.new(-500, -650, -170, -170, -620, -620, -620).max_percents
36
- # notify max_percents.inspect
37
- # end
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
- - 2
9
- version: 0.1.2
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-14 00:00:00 +02:00
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: -4128306614799067572
86
+ hash: -942411906783919221
86
87
  segments:
87
88
  - 0
88
89
  version: "0"