Class: Game

Inherits:
Object
  • Object
show all
Defined in:
lib/blackjack/game.rb

Overview

Class that simulates a Blackjack game

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializenil

Initializes the instance variables used to keep track of the game state



29
30
31
32
33
34
35
36
# File 'lib/blackjack/game.rb', line 29

def initialize
    @deck = generate_deck
    @used_cards = []
    @player_cards = []
    @dealer_cards = []
    @player_score = 0
    @dealer_score = 0
end

Class Method Details

.calculate_optimal_card_score(cards) ⇒ int

Calculates the optimal amount of points for blackjack for an array of cards

Parameters:

  • cards (Card Array)

    the cards to be checked

Returns:

  • (int)

    the optimal blackjack value of the cards



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/blackjack/game.rb', line 162

def self.calculate_optimal_card_score(cards)
    score = 0
    aces = 0
    cards.each { |card|
        unless card.is_flipped  # == if card is not flipped
            score += card.get_value
            if card.is_ace
                aces += 1
            end
        end
    }
    while score > 21 and aces > 0
        aces -= 1
        score -= 10
    end
    score
end

Instance Method Details

#cleanupCard Array

Resets the game to enable starting a new game.

Returns:

  • (Card Array, Card Array)

    the user’s cards and the dealer’s cards



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/blackjack/game.rb', line 145

def cleanup
    @used_cards += @player_cards + @dealer_cards
    @player_score = 0
    @dealer_score = 0
    @player_ace_count = 0
    @dealer_ace_count = 0
    old_player_cards = @player_cards.clone
    old_dealer_cards = @dealer_cards.clone
    @player_cards = []
    @dealer_cards = []
    return old_player_cards, old_dealer_cards
end

#draw_cardCard

Draws a new card It automatically stocks up the deck with previously used cards if the deck runs out of cards

Returns:

  • (Card)

    the newly drawn card



57
58
59
60
61
62
63
# File 'lib/blackjack/game.rb', line 57

def draw_card
    if @deck.length == 0
        @deck = @used_cards.shuffle
        @used_cards = []
    end
    @deck.pop
end

#evaluate(player_standing = false) ⇒ Card Array, string

Evaluates the current state of the game according to the standard blackjack rules

Parameters:

  • player_standing (boolean) (defaults to: false)

    flag that is set to evaluate a game once the player has decided to stand

Returns:

  • (Card Array, Card Array, string)

    the player’s cards, the dealer’s cards, a string stating the current status of the match. Possible Options: ‘loss’, if the user lost

    'win', if the user won
    'draw', if the game ended in a draw
    'undecided', if the game is not finished
    


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/blackjack/game.rb', line 119

def evaluate(player_standing = false)
    player_cards = @player_cards
    dealer_cards = @dealer_cards
    state = 'undecided'
    if @player_score > 21
        state = 'loss'
    elsif @dealer_score > 21
        state = 'win'
    elsif player_standing and @player_score > @dealer_score
        state = 'win'
    elsif player_standing and @player_score < @dealer_score
        state = 'loss'
    elsif player_standing and @player_score == @dealer_score
        state = 'draw'
    end

    if state != 'undecided'
        player_cards, dealer_cards = cleanup
    end

    return player_cards, dealer_cards, state
end

#generate_deckCard Array

Generates a new deck of cards The deck is shuffled directly after creation

Returns:

  • (Card Array)

    the generated deck



42
43
44
45
46
47
48
49
50
51
# File 'lib/blackjack/game.rb', line 42

def generate_deck
    deck = []
    suits = %w(spades hearts diamonds clubs)
    (2...15).each { |i|
        suits.each { |suit|
            deck.push(Card.new(suit, i))
        }
    }
    deck.shuffle
end

#get_dealer_scoreint

Returns the dealer’s current score.

Returns:

  • (int)

    the dealer’s current score



188
189
190
# File 'lib/blackjack/game.rb', line 188

def get_dealer_score
    @dealer_score
end

#get_player_scoreint

Returns the player’s current score.

Returns:

  • (int)

    the player’s current score



182
183
184
# File 'lib/blackjack/game.rb', line 182

def get_player_score
    @player_score
end

#hit(dealer = false) ⇒ Card Array, string

Draws a new card for a player and immediately evaluates the current state of the game

Parameters:

  • dealer (Object) (defaults to: false)

    flag can be set to make the dealer instead of the player draw a card

Returns:

  • (Card Array, Card Array, string)

    the result of the drawing. Refer to evaluate() for details



86
87
88
89
90
91
92
93
94
95
# File 'lib/blackjack/game.rb', line 86

def hit(dealer = false)
    if dealer
        @dealer_cards.push(draw_card)
        @dealer_score = self.class.calculate_optimal_card_score(@dealer_cards)
    else
        @player_cards.push(draw_card)
        @player_score = self.class.calculate_optimal_card_score(@player_cards)
    end
    evaluate
end

#standCard Array, string

Stops the user interaction. The cards of the user will no longer change, and now the dealer draws cards until he is above 16 points. Afterwards, the game state is evaluated

Returns:

  • (Card Array, Card Array, string)

    the result of the drawing. Refer to evaluate() for details



101
102
103
104
105
106
107
# File 'lib/blackjack/game.rb', line 101

def stand
    @dealer_cards[1].flip_over
    while @dealer_score < 17
        hit(true)
    end
    evaluate(true)
end

#startCard Array, string

Starts a game of roulette. Each player will draw two cards, the dealer’s second card will stay top down until the player stands

Returns:

  • (Card Array, Card Array, string)

    the result of the first few drawings. Refer to evaluate() for details



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/blackjack/game.rb', line 69

def start
    # Let the player draw his cards
    hit
    hit

    # Let the dealer draw his cards
    hit(true)
    result = hit(true)
    @dealer_cards[1].flip_over  # Flip over the card

    result
end