Class: Game
- Inherits:
-
Object
- Object
- Game
- Defined in:
- lib/blackjack/game.rb
Overview
Class that simulates a Blackjack game
Class Method Summary collapse
-
.calculate_optimal_card_score(cards) ⇒ int
Calculates the optimal amount of points for blackjack for an array of cards.
Instance Method Summary collapse
-
#cleanup ⇒ Card Array
Resets the game to enable starting a new game.
-
#draw_card ⇒ Card
Draws a new card It automatically stocks up the deck with previously used cards if the deck runs out of cards.
-
#evaluate(player_standing = false) ⇒ Card Array, string
Evaluates the current state of the game according to the standard blackjack rules.
-
#generate_deck ⇒ Card Array
Generates a new deck of cards The deck is shuffled directly after creation.
-
#get_dealer_score ⇒ int
The dealer’s current score.
-
#get_player_score ⇒ int
The player’s current score.
-
#hit(dealer = false) ⇒ Card Array, string
Draws a new card for a player and immediately evaluates the current state of the game.
-
#initialize ⇒ nil
constructor
Initializes the instance variables used to keep track of the game state.
-
#stand ⇒ Card Array, string
Stops the user interaction.
-
#start ⇒ Card Array, string
Starts a game of roulette.
Constructor Details
#initialize ⇒ nil
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
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
#cleanup ⇒ Card Array
Resets the game to enable starting a new game.
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_card ⇒ Card
Draws a new card It automatically stocks up the deck with previously used cards if the deck runs out of cards
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
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_deck ⇒ Card Array
Generates a new deck of cards The deck is shuffled directly after creation
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_score ⇒ int
Returns the dealer’s current score.
188 189 190 |
# File 'lib/blackjack/game.rb', line 188 def get_dealer_score @dealer_score end |
#get_player_score ⇒ int
Returns 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
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 |
#stand ⇒ Card 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
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 |
#start ⇒ Card 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
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 |