bridge 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/bridge.gemspec +54 -0
- data/lib/bridge.rb +22 -22
- data/test/test_bridge.rb +19 -19
- metadata +2 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
data/bridge.gemspec
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{bridge}
|
8
|
+
s.version = "0.0.2"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Jakub Kuźma"]
|
12
|
+
s.date = %q{2010-02-18}
|
13
|
+
s.description = %q{Useful contract bridge utilities - deal generator, id to deal and deal to id conversion}
|
14
|
+
s.email = %q{qoobaa+github@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"bridge.gemspec",
|
27
|
+
"lib/bridge.rb",
|
28
|
+
"test/helper.rb",
|
29
|
+
"test/test_bridge.rb"
|
30
|
+
]
|
31
|
+
s.homepage = %q{http://github.com/qoobaa/bridge}
|
32
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubygems_version = %q{1.3.5}
|
35
|
+
s.summary = %q{Contract bridge utilities}
|
36
|
+
s.test_files = [
|
37
|
+
"test/test_bridge.rb",
|
38
|
+
"test/helper.rb"
|
39
|
+
]
|
40
|
+
|
41
|
+
if s.respond_to? :specification_version then
|
42
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
43
|
+
s.specification_version = 3
|
44
|
+
|
45
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
46
|
+
s.add_development_dependency(%q<test-unit>, [">= 2"])
|
47
|
+
else
|
48
|
+
s.add_dependency(%q<test-unit>, [">= 2"])
|
49
|
+
end
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<test-unit>, [">= 2"])
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
data/lib/bridge.rb
CHANGED
@@ -1,33 +1,33 @@
|
|
1
1
|
module Bridge
|
2
2
|
|
3
|
-
#
|
3
|
+
# Number of possible deals in bridge
|
4
4
|
DEALS = 53_644_737_765_488_792_839_237_440_000
|
5
5
|
|
6
|
-
# Array with cards in the bridge deck (
|
6
|
+
# Array with cards in the bridge deck (AKQJT98765432, four suits)
|
7
7
|
DECK = %w(S H D C).inject([]) do |d, s|
|
8
8
|
d += %w(A K Q J T 9 8 7 6 5 4 3 2).map { |c| c + s }
|
9
9
|
end
|
10
10
|
|
11
|
-
# Converts given
|
12
|
-
def self.
|
11
|
+
# Converts given id to deal (hash)
|
12
|
+
def self.id_to_deal(id)
|
13
13
|
deal = { :n => [], :e => [], :s => [], :w => [] }
|
14
14
|
k = DEALS
|
15
15
|
DECK.each_with_index do |card, i|
|
16
16
|
x = k * (13 - deal[:n].size) / (52 - i)
|
17
|
-
if
|
17
|
+
if id < x
|
18
18
|
deal[:n] << card
|
19
19
|
else
|
20
|
-
|
20
|
+
id -= x
|
21
21
|
x = k * (13 - deal[:e].size) / (52 - i)
|
22
|
-
if
|
22
|
+
if id < x
|
23
23
|
deal[:e] << card
|
24
24
|
else
|
25
|
-
|
25
|
+
id -= x
|
26
26
|
x = k * (13 - deal[:s].size) / (52 - i)
|
27
|
-
if
|
27
|
+
if id < x
|
28
28
|
deal[:s] << card
|
29
29
|
else
|
30
|
-
|
30
|
+
id -= x
|
31
31
|
x = k * (13 - deal[:w].size) / (52 - i)
|
32
32
|
deal[:w] << card
|
33
33
|
end
|
@@ -38,20 +38,20 @@ module Bridge
|
|
38
38
|
deal
|
39
39
|
end
|
40
40
|
|
41
|
-
# Converts given deal (hash) to
|
42
|
-
def self.
|
43
|
-
k = DEALS;
|
41
|
+
# Converts given deal (hash) to id
|
42
|
+
def self.deal_to_id(d)
|
43
|
+
k = DEALS; id = 0
|
44
44
|
deal = { :n => d[:n].dup, :e => d[:e].dup, :s => d[:s].dup, :w => d[:w].dup }
|
45
45
|
DECK.each_with_index do |card, i|
|
46
46
|
x = k * deal[:n].size / (52 - i)
|
47
47
|
unless deal[:n].delete(card)
|
48
|
-
|
48
|
+
id += x
|
49
49
|
x = k * deal[:e].size / (52 - i)
|
50
50
|
unless deal[:e].delete(card)
|
51
|
-
|
51
|
+
id += x
|
52
52
|
x = k * deal[:s].size / (52 - i)
|
53
53
|
unless deal[:s].delete(card)
|
54
|
-
|
54
|
+
id += x
|
55
55
|
x = k * deal[:w].size / (52 - i)
|
56
56
|
deal[:w].delete(card)
|
57
57
|
end
|
@@ -59,21 +59,21 @@ module Bridge
|
|
59
59
|
end
|
60
60
|
k = x
|
61
61
|
end
|
62
|
-
|
62
|
+
id
|
63
63
|
end
|
64
64
|
|
65
|
-
# Returns a random deal
|
66
|
-
def self.
|
65
|
+
# Returns a random deal id
|
66
|
+
def self.random_deal_id
|
67
67
|
rand(DEALS)
|
68
68
|
end
|
69
69
|
|
70
70
|
# Returns a random deal
|
71
71
|
def self.random_deal
|
72
|
-
|
72
|
+
id_to_deal(random_deal_id)
|
73
73
|
end
|
74
74
|
|
75
|
-
# Checks if the given number is valid deal
|
76
|
-
def self.
|
75
|
+
# Checks if the given number is valid deal id
|
76
|
+
def self.deal_id?(n)
|
77
77
|
(0..DEALS - 1).include?(n)
|
78
78
|
end
|
79
79
|
|
data/test/test_bridge.rb
CHANGED
@@ -2,35 +2,35 @@ require 'helper'
|
|
2
2
|
|
3
3
|
class TestBridge < Test::Unit::TestCase
|
4
4
|
test "first deal conversion" do
|
5
|
-
|
6
|
-
deal = Bridge.
|
5
|
+
id = 0
|
6
|
+
deal = Bridge.id_to_deal(id)
|
7
7
|
assert Bridge.deal?(deal)
|
8
8
|
assert_equal %w(AS KS QS JS TS 9S 8S 7S 6S 5S 4S 3S 2S), deal[:n]
|
9
9
|
assert_equal %w(AH KH QH JH TH 9H 8H 7H 6H 5H 4H 3H 2H), deal[:e]
|
10
10
|
assert_equal %w(AD KD QD JD TD 9D 8D 7D 6D 5D 4D 3D 2D), deal[:s]
|
11
11
|
assert_equal %w(AC KC QC JC TC 9C 8C 7C 6C 5C 4C 3C 2C), deal[:w]
|
12
|
-
assert_equal
|
12
|
+
assert_equal id, Bridge.deal_to_id(deal)
|
13
13
|
end
|
14
14
|
|
15
15
|
test "last deal conversion" do
|
16
|
-
|
17
|
-
deal = Bridge.
|
16
|
+
id = Bridge::DEALS - 1
|
17
|
+
deal = Bridge.id_to_deal(id)
|
18
18
|
assert Bridge.deal?(deal)
|
19
19
|
assert_equal %w(AC KC QC JC TC 9C 8C 7C 6C 5C 4C 3C 2C), deal[:n]
|
20
20
|
assert_equal %w(AD KD QD JD TD 9D 8D 7D 6D 5D 4D 3D 2D), deal[:e]
|
21
21
|
assert_equal %w(AH KH QH JH TH 9H 8H 7H 6H 5H 4H 3H 2H), deal[:s]
|
22
22
|
assert_equal %w(AS KS QS JS TS 9S 8S 7S 6S 5S 4S 3S 2S), deal[:w]
|
23
|
-
assert_equal
|
23
|
+
assert_equal id, Bridge.deal_to_id(deal)
|
24
24
|
end
|
25
25
|
|
26
26
|
test "deal no 1 000 000 000" do
|
27
|
-
|
28
|
-
deal = Bridge.
|
27
|
+
id = 1_000_000_000
|
28
|
+
deal = Bridge.id_to_deal(id)
|
29
29
|
assert Bridge.deal?(deal)
|
30
|
-
assert_equal
|
30
|
+
assert_equal id, Bridge.deal_to_id(deal)
|
31
31
|
end
|
32
32
|
|
33
|
-
test "sample deal to
|
33
|
+
test "sample deal to id conversion" do
|
34
34
|
deal = {
|
35
35
|
:n => %w(AS KS QS JS AH KH QH AD KD QD AC KC QC),
|
36
36
|
:e => %w(TS 9S 8S 7S 6S 5S 4S 3S 2S JH TH 9H 8H),
|
@@ -38,8 +38,8 @@ class TestBridge < Test::Unit::TestCase
|
|
38
38
|
:w => %w(4D 3D 2D JC TC 9C 8C 7C 6C 5C 4C 3C 2C)
|
39
39
|
}
|
40
40
|
assert Bridge.deal?(deal)
|
41
|
-
|
42
|
-
assert_equal deal, Bridge.
|
41
|
+
id = Bridge.deal_to_id(deal)
|
42
|
+
assert_equal deal, Bridge.id_to_deal(id)
|
43
43
|
end
|
44
44
|
|
45
45
|
test "deal with doubled cards is not valid deal" do
|
@@ -62,17 +62,17 @@ class TestBridge < Test::Unit::TestCase
|
|
62
62
|
assert_false Bridge.deal?(deal)
|
63
63
|
end
|
64
64
|
|
65
|
-
test "negative number is not valid
|
66
|
-
assert_false Bridge.
|
65
|
+
test "negative number is not valid deal id" do
|
66
|
+
assert_false Bridge.deal_id?(-1)
|
67
67
|
end
|
68
68
|
|
69
|
-
test "
|
70
|
-
assert_false Bridge.
|
69
|
+
test "number of possible bridge deals is not valid deal id" do
|
70
|
+
assert_false Bridge.deal_id?(Bridge::DEALS)
|
71
71
|
end
|
72
72
|
|
73
|
-
test "random deal
|
74
|
-
|
75
|
-
assert Bridge.
|
73
|
+
test "random deal id is valid deal id" do
|
74
|
+
id = Bridge.random_deal_id
|
75
|
+
assert Bridge.deal_id?(id)
|
76
76
|
end
|
77
77
|
|
78
78
|
test "random deal is valid deal" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bridge
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Jakub Ku\xC5\xBAma"
|
@@ -38,6 +38,7 @@ files:
|
|
38
38
|
- README.rdoc
|
39
39
|
- Rakefile
|
40
40
|
- VERSION
|
41
|
+
- bridge.gemspec
|
41
42
|
- lib/bridge.rb
|
42
43
|
- test/helper.rb
|
43
44
|
- test/test_bridge.rb
|