Completed Panda, Tiger and Eagle levels #6

Open
wants to merge 1 commit into
from

2 participants

@tsemana

Ran into problems writing a test for the values of the holecard in-game before and after the flipped! method is called. I couldn't figure out how an easy way to inject the specific card value for the dealer's first card without having to make changes to the game's initialize method (could have but seemed like overkill)..

I tried

game = Game.new
game.dealer_hand.cards[0] = Holecard.new(:club, 8)
game.stand
game.dealer_hand.cards[0].value.should eq 8

but it kept giving me the error that value was still "XX", which doesn't make sense... to me.
Please let me know if there was an easier way, or why this wouldn't work!

@tsemana

Thanks @HerbCSO! To be fair looking at yours and others' implementation helped me get there quicker than I think I would normally have. So thanks for that!

@ghost Unknown commented on the diff Aug 10, 2012
blackjack.rb
end
def hit
@player_hand.hit!(@deck)
+ stand if @player_hand.value > 21
@ghost
ghost added a line comment Aug 10, 2012

stand or self.stand?

@tsemana
tsemana added a line comment Aug 10, 2012

self. is not required when calling a method defined within the class or its' supers. I find that I seldom use it.
check this link on stackoverflow for a quick explanation

@ghost
ghost added a line comment Aug 10, 2012

Ah, gotcha. For anyone who cares, the accepted answer in the link says the following:

It is idiomatic to prefer to omit self. when invoking methods; it is generally never needed.

Thanks, @tsemana.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jwo jwo commented on the diff Aug 13, 2012
blackjack.rb
end
+class Holecard < Card
+ attr_reader :flipped
+ alias_method :flipped?, :flipped
+
+ def initialize(suit, value)
+ @flipped = false
+ super
+ end
+
+ def to_s
+ if @flipped == false
@jwo
Ruby Off Rails member
jwo added a line comment Aug 13, 2012

I probably would try to right this like:

def to_s
  return "XX" unless flipped?
  super
end

Nice use of super here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jwo jwo commented on the diff Aug 13, 2012
blackjack.rb
def initialize
@deck = Deck.new
@player_hand = Hand.new
@dealer_hand = Hand.new
2.times { @player_hand.hit!(@deck) }
2.times { @dealer_hand.hit!(@deck) }
+ set_holecard(@dealer_hand.cards.first.suit, @dealer_hand.cards.first.symbol)
@jwo
Ruby Off Rails member
jwo added a line comment Aug 13, 2012

This certainly gets the job done... Notes:

  • The Game knows a bit too much about the dealer's hand --- going in and grabbing cards.first.suit is a sign that it knows too much
  • Instead, you could change the dealer_hand to be its own object, say DealerHand .. and maybe change it's initialize method to instantly convert the first card to a HoleCard.
@tsemana
tsemana added a line comment Aug 13, 2012

Thanks. It's definitely doing too much with the Dealer's Hand. After I submitted this I started the same exercise from scratch to try and BDD it with cucumber. This was fun. Enjoying the course so far!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jwo
Ruby Off Rails member

Great job on this!!! I added a couple of notes to look at in the future. Awesome stuff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment