bridge 0.0.1

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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Jakub Kuźma
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = bridge
2
+
3
+ Contract bridge utils.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Jakub Kuźma. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,55 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'rubygems'
4
+ require 'rake'
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.name = "bridge"
10
+ gem.summary = %Q{Contract bridge utilities}
11
+ gem.description = %Q{Useful contract bridge utilities - deal generator, id to deal and deal to id conversion}
12
+ gem.email = "qoobaa+github@gmail.com"
13
+ gem.homepage = "http://github.com/qoobaa/bridge"
14
+ gem.authors = ["Jakub Kuźma"]
15
+ gem.add_development_dependency "test-unit", ">= 2"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ require 'rake/testtask'
24
+ Rake::TestTask.new(:test) do |test|
25
+ test.libs << 'lib' << 'test'
26
+ test.pattern = 'test/**/test_*.rb'
27
+ test.verbose = true
28
+ end
29
+
30
+ begin
31
+ require 'rcov/rcovtask'
32
+ Rcov::RcovTask.new do |test|
33
+ test.libs << 'test'
34
+ test.pattern = 'test/**/test_*.rb'
35
+ test.verbose = true
36
+ end
37
+ rescue LoadError
38
+ task :rcov do
39
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
40
+ end
41
+ end
42
+
43
+ task :test => :check_dependencies
44
+
45
+ task :default => :test
46
+
47
+ require 'rake/rdoctask'
48
+ Rake::RDocTask.new do |rdoc|
49
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "bridge #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/lib/bridge.rb ADDED
@@ -0,0 +1,94 @@
1
+ module Bridge
2
+
3
+ # Total number of possible deals in bridge
4
+ DEALS = 53_644_737_765_488_792_839_237_440_000
5
+
6
+ # Array with cards in the bridge deck (from two to ace, four suits)
7
+ DECK = %w(S H D C).inject([]) do |d, s|
8
+ d += %w(A K Q J T 9 8 7 6 5 4 3 2).map { |c| c + s }
9
+ end
10
+
11
+ # Converts given number to deal (hash)
12
+ def self.number_to_deal(number)
13
+ deal = { :n => [], :e => [], :s => [], :w => [] }
14
+ k = DEALS
15
+ DECK.each_with_index do |card, i|
16
+ x = k * (13 - deal[:n].size) / (52 - i)
17
+ if number < x
18
+ deal[:n] << card
19
+ else
20
+ number -= x
21
+ x = k * (13 - deal[:e].size) / (52 - i)
22
+ if number < x
23
+ deal[:e] << card
24
+ else
25
+ number -= x
26
+ x = k * (13 - deal[:s].size) / (52 - i)
27
+ if number < x
28
+ deal[:s] << card
29
+ else
30
+ number -= x
31
+ x = k * (13 - deal[:w].size) / (52 - i)
32
+ deal[:w] << card
33
+ end
34
+ end
35
+ end
36
+ k = x
37
+ end
38
+ deal
39
+ end
40
+
41
+ # Converts given deal (hash) to number
42
+ def self.deal_to_number(d)
43
+ k = DEALS; number = 0
44
+ deal = { :n => d[:n].dup, :e => d[:e].dup, :s => d[:s].dup, :w => d[:w].dup }
45
+ DECK.each_with_index do |card, i|
46
+ x = k * deal[:n].size / (52 - i)
47
+ unless deal[:n].delete(card)
48
+ number += x
49
+ x = k * deal[:e].size / (52 - i)
50
+ unless deal[:e].delete(card)
51
+ number += x
52
+ x = k * deal[:s].size / (52 - i)
53
+ unless deal[:s].delete(card)
54
+ number += x
55
+ x = k * deal[:w].size / (52 - i)
56
+ deal[:w].delete(card)
57
+ end
58
+ end
59
+ end
60
+ k = x
61
+ end
62
+ number
63
+ end
64
+
65
+ # Returns a random deal number
66
+ def self.random_deal_number
67
+ rand(DEALS)
68
+ end
69
+
70
+ # Returns a random deal
71
+ def self.random_deal
72
+ number_to_deal(random_deal_number)
73
+ end
74
+
75
+ # Checks if the given number is valid deal number
76
+ def self.deal_number?(n)
77
+ (0..DEALS - 1).include?(n)
78
+ end
79
+
80
+ # Checks if the given hash is valid deal
81
+ def self.deal?(h)
82
+ if [:n, :e, :s, :w].all? { |s| h[s] && h[s].size == 13 }
83
+ cards = (h[:n] + h[:e] + h[:s] + h[:w]).uniq
84
+ if cards.size == 52
85
+ cards.all? { |card| DECK.include?(card) }
86
+ else
87
+ false
88
+ end
89
+ else
90
+ false
91
+ end
92
+ end
93
+
94
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ require 'bridge'
7
+
8
+ class Test::Unit::TestCase
9
+
10
+ end
@@ -0,0 +1,82 @@
1
+ require 'helper'
2
+
3
+ class TestBridge < Test::Unit::TestCase
4
+ test "first deal conversion" do
5
+ number = 0
6
+ deal = Bridge.number_to_deal(number)
7
+ assert Bridge.deal?(deal)
8
+ assert_equal %w(AS KS QS JS TS 9S 8S 7S 6S 5S 4S 3S 2S), deal[:n]
9
+ assert_equal %w(AH KH QH JH TH 9H 8H 7H 6H 5H 4H 3H 2H), deal[:e]
10
+ assert_equal %w(AD KD QD JD TD 9D 8D 7D 6D 5D 4D 3D 2D), deal[:s]
11
+ assert_equal %w(AC KC QC JC TC 9C 8C 7C 6C 5C 4C 3C 2C), deal[:w]
12
+ assert_equal number, Bridge.deal_to_number(deal)
13
+ end
14
+
15
+ test "last deal conversion" do
16
+ number = Bridge::DEALS - 1
17
+ deal = Bridge.number_to_deal(number)
18
+ assert Bridge.deal?(deal)
19
+ assert_equal %w(AC KC QC JC TC 9C 8C 7C 6C 5C 4C 3C 2C), deal[:n]
20
+ assert_equal %w(AD KD QD JD TD 9D 8D 7D 6D 5D 4D 3D 2D), deal[:e]
21
+ assert_equal %w(AH KH QH JH TH 9H 8H 7H 6H 5H 4H 3H 2H), deal[:s]
22
+ assert_equal %w(AS KS QS JS TS 9S 8S 7S 6S 5S 4S 3S 2S), deal[:w]
23
+ assert_equal number, Bridge.deal_to_number(deal)
24
+ end
25
+
26
+ test "deal no 1 000 000 000" do
27
+ number = 1_000_000_000
28
+ deal = Bridge.number_to_deal(number)
29
+ assert Bridge.deal?(deal)
30
+ assert_equal number, Bridge.deal_to_number(deal)
31
+ end
32
+
33
+ test "sample deal to number conversion" do
34
+ deal = {
35
+ :n => %w(AS KS QS JS AH KH QH AD KD QD AC KC QC),
36
+ :e => %w(TS 9S 8S 7S 6S 5S 4S 3S 2S JH TH 9H 8H),
37
+ :s => %w(7H 6H 5H 4H 3H 2H JD TD 9D 8D 7D 6D 5D),
38
+ :w => %w(4D 3D 2D JC TC 9C 8C 7C 6C 5C 4C 3C 2C)
39
+ }
40
+ assert Bridge.deal?(deal)
41
+ number = Bridge.deal_to_number(deal)
42
+ assert_equal deal, Bridge.number_to_deal(number)
43
+ end
44
+
45
+ test "deal with doubled cards is not valid deal" do
46
+ deal = {
47
+ :n => %w(AS AS QS JS AH KH QH AD KD QD AC KC QC),
48
+ :e => %w(TS 9S 8S 7S 6S 5S 4S 3S 2S JH TH 9H 8H),
49
+ :s => %w(7H 6H 5H 4H 3H 2H JD TD 9D 8D 7D 6D 5D),
50
+ :w => %w(4D 3D 2D JC TC 9C 8C 7C 6C 5C 4C 3C 2C)
51
+ }
52
+ assert_false Bridge.deal?(deal)
53
+ end
54
+
55
+ test "deal with different length hands is not valid deal" do
56
+ deal = {
57
+ :n => %w(AS KS QS JS AH KH QH AD KD QD AC KC QC TS),
58
+ :e => %w(9S 8S 7S 6S 5S 4S 3S 2S JH TH 9H 8H),
59
+ :s => %w(7H 6H 5H 4H 3H 2H JD TD 9D 8D 7D 6D 5D),
60
+ :w => %w(4D 3D 2D JC TC 9C 8C 7C 6C 5C 4C 3C 2C)
61
+ }
62
+ assert_false Bridge.deal?(deal)
63
+ end
64
+
65
+ test "negative number is not valid deal_number" do
66
+ assert_false Bridge.deal_number?(-1)
67
+ end
68
+
69
+ test "total number of possible deals is not valid deal_number" do
70
+ assert_false Bridge.deal_number?(Bridge::DEALS)
71
+ end
72
+
73
+ test "random deal number is valid deal number" do
74
+ number = Bridge.random_deal_number
75
+ assert Bridge.deal_number?(number)
76
+ end
77
+
78
+ test "random deal is valid deal" do
79
+ deal = Bridge.random_deal
80
+ assert Bridge.deal?(deal)
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bridge
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - "Jakub Ku\xC5\xBAma"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-18 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: test-unit
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "2"
24
+ version:
25
+ description: Useful contract bridge utilities - deal generator, id to deal and deal to id conversion
26
+ email: qoobaa+github@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/bridge.rb
42
+ - test/helper.rb
43
+ - test/test_bridge.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/qoobaa/bridge
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --charset=UTF-8
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project:
68
+ rubygems_version: 1.3.5
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Contract bridge utilities
72
+ test_files:
73
+ - test/test_bridge.rb
74
+ - test/helper.rb