Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Handle boolean responses

  • Loading branch information...
commit 5a22abdf89a1d476cc6366631cf423b971892909 1 parent 5e272ad
Carter Thaxton authored

Showing 4 changed files with 48 additions and 15 deletions. Show diff stats Hide diff stats

  1. +1 0  Gemfile
  2. +2 0  Gemfile.lock
  3. +6 8 lib/cards.rb
  4. +39 7 lib/player.rb
1  Gemfile
... ... @@ -1,3 +1,4 @@
1 1 source 'http://rubygems.org'
2 2
3 3 gem 'pry'
  4 +gem 'rake'
2  Gemfile.lock
@@ -9,6 +9,7 @@ GEM
9 9 method_source (~> 0.6.5)
10 10 ruby_parser (~> 2.0.5)
11 11 slop (~> 2.1.0)
  12 + rake (0.9.2.2)
12 13 ruby_parser (2.0.6)
13 14 sexp_processor (~> 3.0)
14 15 sexp_processor (3.0.7)
@@ -19,3 +20,4 @@ PLATFORMS
19 20
20 21 DEPENDENCIES
21 22 pry
  23 + rake
14 lib/cards.rb
@@ -662,14 +662,12 @@ class Mountebank < Card
662 662
663 663 def play_action
664 664 other_players.each do |player|
665   - player.reveal :type => Curse, :attack => true do |curse, attack_failed|
666   - unless attack_failed
667   - if curse
668   - player.discard curse
669   - else
670   - player.gain Copper
671   - player.gain Curse
672   - end
  665 + player.reveal :type => Curse, :attack => true do |curse|
  666 + if curse
  667 + player.discard curse
  668 + else
  669 + player.gain Curse
  670 + player.gain Copper
673 671 end
674 672 end
675 673 end
46 lib/player.rb
@@ -286,7 +286,33 @@ def respond(response)
286 286 @choice_in_progress = nil
287 287 response
288 288 end
289   -
  289 +
  290 + def reveal(options = {})
  291 + attack_cond options[:attach] do
  292 + type = options[:type]
  293 + card = find_card_in_hand(type)
  294 + if card
  295 + if options[:required]
  296 + yield card
  297 + else
  298 + ask "Reveal #{card}" do |response|
  299 + yield card if response
  300 + end
  301 + end
  302 + end
  303 + end
  304 + end
  305 +
  306 + def attack_cond(condition)
  307 + if condition
  308 + attack do
  309 + yield
  310 + end
  311 + else
  312 + yield
  313 + end
  314 + end
  315 +
290 316 def cards_in_play
291 317 actions_in_play + treasures_in_play + durations_on_first_turn + durations_on_second_turn
292 318 end
@@ -340,13 +366,19 @@ def handle_response(response)
340 366 else
341 367 return find_card_in_hand(response)
342 368 end
  369 + else
  370 + raise "Only cards may be chosen from hand"
343 371 end
344 372 end
345   -
  373 +
  374 + if @choice_in_progress[:type] == :bool && !@choice_in_progress[:multiple]
  375 + raise "Response must be true or false" unless response == true or response == false
  376 + end
  377 +
346 378 if @choice_in_progress[:multiple] && @choice_in_progress[:max]
347 379 raise "At most #{@choice_in_progress[:max]} may be chosen" if response.size > @choice_in_progress[:max]
348 380 end
349   -
  381 +
350 382 if @choice_in_progress[:multiple] && @choice_in_progress[:min]
351 383 raise "At least #{@choice_in_progress[:min]} must be chosen" if response.size < @choice_in_progress[:min]
352 384 end
@@ -355,13 +387,13 @@ def handle_response(response)
355 387 end
356 388
357 389 def find_card_in_hand(card, options = {})
358   - set = options.fetch :set, hand
  390 + hand = options.fetch :hand, self.hand
359 391 if card.is_a? Card
360   - raise "#{card} is not in the player's hand" unless set.include?(card)
  392 + raise "#{card} is not in the player's hand" unless hand.include?(card)
361 393 elsif is_card_class(card)
362 394 # choose an instance from the player's hand of the given class
363 395 card_class = card
364   - card = set.find {|card| card.is_a? card_class}
  396 + card = hand.find {|card| card.is_a? card_class}
365 397 if options[:required]
366 398 raise "No card of type #{card_class} found in hand" unless card
367 399 end
@@ -371,7 +403,7 @@ def find_card_in_hand(card, options = {})
371 403
372 404 def find_cards_in_hand(cards, options = {})
373 405 tmp_hand = hand.dup
374   - options[:set] = tmp_hand
  406 + options[:hand] = tmp_hand
375 407
376 408 cards.collect do |card|
377 409 card = find_card_in_hand(card, options)

0 comments on commit 5a22abd

Please sign in to comment.
Something went wrong with that request. Please try again.