Module: Bridge
- Defined in:
- lib/bridge.rb,
lib/bridge/bid.rb,
lib/bridge/card.rb,
lib/bridge/deal.rb,
lib/bridge/play.rb,
lib/bridge/score.rb,
lib/bridge/trick.rb,
lib/bridge/points.rb,
lib/bridge/auction.rb,
lib/bridge/version.rb,
lib/bridge/constants.rb
Defined Under Namespace
Modules: Points Classes: Auction, Bid, Card, Deal, Play, Score, Trick
Constant Summary collapse
- VERSION =
"0.2.0"
- DEALS =
Number of possible deals in bridge
53_644_737_765_488_792_839_237_440_000
- CARD_VALUES =
Card values - from A to 2
%w(A K Q J T 9 8 7 6 5 4 3 2)
- MAJORS =
%w(S H)
- MINORS =
%w(D C)
- TRUMPS =
Trumps
MAJORS + MINORS
- NO_TRUMP =
No trump string
"NT"
- DECK =
Array with card strings in the bridge deck (AKQJT98765432, four suits). Contains “SA”, “HT”, etc.
TRUMPS.map do |suit| CARD_VALUES.map { |card| suit + card } end.flatten
- DIRECTIONS =
Direction strings “N”, “E”, “S” and “W”
%w(N E S W)
- CONTRACTS =
Possible contracts in ascending order. Contains “1C”, “6NT”, etc.
%w(1 2 3 4 5 6 7).map do |level| (TRUMPS.reverse + [NO_TRUMP]).map { |suit| level + suit } end.flatten
- PASS =
Pass string
"PASS"
- DOUBLE =
Double string
"X"
- REDOUBLE =
Redouble string
"XX"
- MODIFIERS =
Modifier bids (double and redouble)
[DOUBLE, REDOUBLE]
- BIDS =
All possible bids (including contracts, modifiers and pass)
CONTRACTS + MODIFIERS + [PASS]
- SIDES =
2 sides
%w{NS EW}
- VULNERABILITIES =
All possible vullnerabilites
["NONE", SIDES, "BOTH"].flatten
- BID_REGEXP =
Matches 2S, 7NT, 1C
Regexp.new %q{(?<bid>([1-7])([CDHS]|NT))}
- MODIFIER_REGEXP =
Matches X, XX
Regexp.new %q{(?<modifier>(X{1,2}))}
- DIRECTION_REGEXP =
Matches X, XX
Regexp.new %q{(?<direction>([NESW]))}
- RESULT_REGEXP =
Matches =, -12, +4
Regexp.new %q{(?<result>(=|\+[1-6]|-\d{1,2}))}
- CONTRACT_REGEXP =
Matches 7NTXE, 1SXXS
Regexp.new %Q{(?<contract>#{BID_REGEXP}#{MODIFIER_REGEXP}?#{DIRECTION_REGEXP})}
- SCORE_REGEXP =
Matches 1SE=, 7NTXXS-2
Regexp.new %Q{(?<score>#{CONTRACT_REGEXP}#{RESULT_REGEXP})}
Class Method Summary collapse
- .bid?(string) ⇒ Boolean
- .card?(string) ⇒ Boolean
- .compare_cards(first, second) ⇒ Object
- .compare_contracts(first, second) ⇒ Object
- .contract?(string) ⇒ Boolean
- .deal_id?(integer) ⇒ Boolean
- .direction?(string) ⇒ Boolean
- .double?(string) ⇒ Boolean
- .major?(string) ⇒ Boolean
- .minor?(string) ⇒ Boolean
- .modifier?(string) ⇒ Boolean
- .next_direction(direction = nil) ⇒ Object
- .next_in_collection(collection, current) ⇒ Object
- .no_trump?(string) ⇒ Boolean
- .partner_of(direction) ⇒ Object
- .pass?(string) ⇒ Boolean
- .redouble?(string) ⇒ Boolean
- .side_of(direction) ⇒ Object
- .trump?(string) ⇒ Boolean
- .vulnerable_in_deal(deal = nil) ⇒ Object
Class Method Details
.bid?(string) ⇒ Boolean
56 57 58 |
# File 'lib/bridge.rb', line 56 def self.bid?(string) BIDS.include?(string) end |
.card?(string) ⇒ Boolean
23 24 25 |
# File 'lib/bridge.rb', line 23 def self.card?(string) DECK.include?(string) end |
.compare_cards(first, second) ⇒ Object
27 28 29 30 |
# File 'lib/bridge.rb', line 27 def self.compare_cards(first, second) # DECK has reversed order DECK.index(second) <=> DECK.index(first) end |
.compare_contracts(first, second) ⇒ Object
32 33 34 |
# File 'lib/bridge.rb', line 32 def self.compare_contracts(first, second) CONTRACTS.index(first) <=> CONTRACTS.index(second) end |
.contract?(string) ⇒ Boolean
52 53 54 |
# File 'lib/bridge.rb', line 52 def self.contract?(string) CONTRACTS.include?(string) end |
.deal_id?(integer) ⇒ Boolean
19 20 21 |
# File 'lib/bridge.rb', line 19 def self.deal_id?(integer) (0...DEALS).include?(integer) end |
.direction?(string) ⇒ Boolean
15 16 17 |
# File 'lib/bridge.rb', line 15 def self.direction?(string) DIRECTIONS.include?(string) end |
.double?(string) ⇒ Boolean
40 41 42 |
# File 'lib/bridge.rb', line 40 def self.double?(string) DOUBLE == string end |
.major?(string) ⇒ Boolean
68 69 70 |
# File 'lib/bridge.rb', line 68 def self.major?(string) MAJORS.include?(string) end |
.minor?(string) ⇒ Boolean
64 65 66 |
# File 'lib/bridge.rb', line 64 def self.minor?(string) MINORS.include?(string) end |
.modifier?(string) ⇒ Boolean
48 49 50 |
# File 'lib/bridge.rb', line 48 def self.modifier?(string) MODIFIERS.include?(string) end |
.next_direction(direction = nil) ⇒ Object
87 88 89 90 91 |
# File 'lib/bridge.rb', line 87 def self.next_direction(direction = nil) return DIRECTIONS.first if direction.nil? return unless direction?(direction) next_in_collection(DIRECTIONS, direction) end |
.next_in_collection(collection, current) ⇒ Object
102 103 104 105 |
# File 'lib/bridge.rb', line 102 def self.next_in_collection(collection, current) i = (collection.index(current) + 1) % collection.size collection[i] end |
.no_trump?(string) ⇒ Boolean
72 73 74 |
# File 'lib/bridge.rb', line 72 def self.no_trump?(string) NO_TRUMP == string end |
.partner_of(direction) ⇒ Object
76 77 78 79 80 |
# File 'lib/bridge.rb', line 76 def self.partner_of(direction) return unless direction?(direction) i = (DIRECTIONS.index(direction) + 2) % 4 DIRECTIONS[i] end |
.pass?(string) ⇒ Boolean
36 37 38 |
# File 'lib/bridge.rb', line 36 def self.pass?(string) PASS == string end |
.redouble?(string) ⇒ Boolean
44 45 46 |
# File 'lib/bridge.rb', line 44 def self.redouble?(string) REDOUBLE == string end |
.side_of(direction) ⇒ Object
82 83 84 85 |
# File 'lib/bridge.rb', line 82 def self.side_of(direction) return unless direction?(direction) [direction, partner_of(direction)].sort.join end |
.trump?(string) ⇒ Boolean
60 61 62 |
# File 'lib/bridge.rb', line 60 def self.trump?(string) TRUMPS.include?(string) end |
.vulnerable_in_deal(deal = nil) ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/bridge.rb', line 93 def self.vulnerable_in_deal(deal = nil) return VULNERABILITIES.first if deal.nil? round = (deal - 1).div(4) % 4 index = (deal - 1) % 4 vulnerabilities = VULNERABILITIES.dup shift = vulnerabilities.shift(round) vulnerabilities.push(shift).flatten[index] end |