lib/bridge/score.rb in bridge-0.0.17 vs lib/bridge/score.rb in bridge-0.0.18

- old
+ new

@@ -6,11 +6,12 @@ # # ==== Example # Bridge::Score.new(:contract => "7SXX", :vulnerable => true, :tricks => "=") def initialize(options = {}) @contract, @modifier = split_contract(options[:contract]) - @tricks = calculate_tricks(options[:tricks].to_s) if (0..13).to_a.include?(calculate_tricks(options[:tricks].to_s)) + @tricks = calculate_tricks(options[:tricks]) + raise ArgumentError, "invalid tricks: #{@tricks}" unless (0..13).include?(@tricks) @vulnerable = options[:vulnerable] || false end # Returns nr of overtricks or undertricks. 0 if contract was made without them def result @@ -147,17 +148,19 @@ 0 end end def calculate_tricks(tricks) - if tricks =~ /\A\+\d\Z/ + if tricks.kind_of? Numeric + tricks + elsif tricks =~ /\A\+\d\Z/ tricks_to_make_contract + tricks[1..1].to_i elsif tricks =~ /\A-\d\Z/ tricks_to_make_contract - tricks[1..1].to_i elsif tricks =~ /\A=\Z/ - tricks_to_make_contract.to_i - else + tricks_to_make_contract + elsif tricks =~ /\A\d[0-3]?\Z/ tricks.to_i end end def split_contract(contract) @@ -166,11 +169,11 @@ [Bridge::Bid.new(contract), modifier] end def self.all_contracts result = {} - contracts = %w(1 2 3 4 5 6 7).inject([]) do |b, l| - b += ["H/S", "C/D", "NT"].map { |s| l + s } + contracts = %w(1 2 3 4 5 6 7).inject([]) do |bids, level| + bids += ["H/S", "C/D", "NT"].map { |suit| level + suit } end (contracts + contracts.map { |c| c + "X" } + contracts.map { |c| c + "XX" }).each do |contract| [true, false].each do |vulnerable| (0..13).each do |tricks| result["#{contract}-#{tricks}#{vulnerable == true ? "-vulnerable" : ""}"] = new(:contract => contract.sub("H/S", "S").sub("C/D", "C"), :tricks => tricks, :vulnerable => vulnerable).points \ No newline at end of file