lib/bridge/score.rb in bridge-0.0.14 vs lib/bridge/score.rb in bridge-0.0.15

- old
+ new

@@ -1,21 +1,17 @@ module Bridge class Score - attr_reader :tricks, :contract, :declarer, :vulnerable + attr_reader :tricks, :contract, :vulnerable # Creates new score object # # ==== Example - # Bridge::Score.new(:contract => "7SXX", :declarer => "S", :vulnerable => "BOTH", :tricks => 13) + # Bridge::Score.new(:contract => "7SXX", :vulnerable => true, :tricks => "=") def initialize(options = {}) - options[:vulnerable] ||= "NONE" - options[:contract].gsub!(/(X+)/, "") - @modifier = $1.nil? ? 1 : $1.to_s.size * 2 - @tricks = options[:tricks] - @contract = Bridge::Bid.new(options[:contract]) - @vulnerable = options[:vulnerable] if Bridge::VULNERABILITIES.include?(options[:vulnerable].upcase) - @declarer = options[:declarer] if Bridge::DIRECTIONS.include?(options[:declarer].upcase) + @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)) + @vulnerable = options[:vulnerable] || false end # Returns nr of overtricks or undertricks. 0 if contract was made without them def result tricks - tricks_to_make_contract @@ -32,28 +28,13 @@ end #private def vulnerable? - case vulnerable - when "BOTH" - true - when "NONE" - false - else - vulnerable.split('').include?(declarer) - end + @vulnerable == true end - def small_slam? - contract.level.to_i == 6 - end - - def grand_slam? - contract.level.to_i == 7 - end - def tricks_to_make_contract contract.level.to_i + 6 end def doubled? @@ -77,19 +58,19 @@ 0 end end def grand_slam_bonus - if made? and grand_slam? + if made? and contract.grand_slam? vulnerable? ? 1500 : 1000 else 0 end end def small_slam_bonus - if made? and small_slam? + if made? and contract.small_slam? vulnerable? ? 750 : 500 else 0 end end @@ -157,8 +138,26 @@ end p else 0 end + end + + def calculate_tricks(tricks) + if 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_i + end + end + + def split_contract(contract) + contract = contract.gsub(/(X+)/, "") + modifier = $1.nil? ? 1 : $1.to_s.size * 2 + [Bridge::Bid.new(contract), modifier] end end end \ No newline at end of file