Jim's
Tutorials

Fall 2018
course
site

Rails week 9

when I realized two cards wouldn't be enough (blackjack)

So still working on this complicated game. I realized the original structure of my hand object wouldn't work, because as I was writing my controller file (lib) and commanding that I be dealt card after card and my score be constantly reevaluated, I realized I had no way of dealing with getting a 4th card. I'd allowed for two cards to be dealt initially, a third to be added- but that would not cut it. So I realized I needed to put my hand into an array and rework things. I also got the scoring working...

require_relative 'deck'
require_relative 'card'

class Hand
    attr_accessor :card_1, :card_2
    def initialize(card_1, card_2)
        @card_1 = card_1
        @card_2 = card_2
        @hand_array = [@card_1, @card_2]

    end

    def add_card(card)
        @hand_array << card
    end

    def display_score
        sum = 0
        @hand_array.each do |card|
            if card.face_card? == true
                sum +=10
            elsif card.rank == 'a' && sum <= 10
                sum += 11
            elsif card.rank == 'a' && sum > 10
                sum += 1
            else
                sum += card.rank
            end
        end

    return sum
    end
end


...this seems pretty common sense but when I first realized the initial structure of my hand woulden't work, I freaked and thought I'd need to start all over. Actually- here is the origional way I'd set it up:

class Hand
    attr_accessor :deal
    def initialize(deal)
        @deal = deal
    attr_accessor :card_1, :card_2
    def initialize(card_1, card_2)
        @card_1 = card_1
        @card_2 = card_2
    end

So yeah did not allow for different cards to be passed through.