Class: Blackjack::Game
- Inherits:
-
Object
- Object
- Blackjack::Game
- Defined in:
- lib/blackjack/game.rb
Instance Attribute Summary collapse
-
#actions ⇒ Object
Returns the value of attribute actions.
-
#amount_decks ⇒ Object
Returns the value of attribute amount_decks.
-
#balance ⇒ Object
Returns the value of attribute balance.
-
#bets ⇒ Object
Returns the value of attribute bets.
-
#dealer ⇒ Object
Returns the value of attribute dealer.
-
#deck ⇒ Object
Returns the value of attribute deck.
-
#finished ⇒ Object
Returns the value of attribute finished.
-
#options ⇒ Object
Returns the value of attribute options.
-
#players ⇒ Object
Returns the value of attribute players.
-
#seed ⇒ Object
Returns the value of attribute seed.
Instance Method Summary collapse
- #all_players_finished? ⇒ Boolean
- #american_rules? ⇒ Boolean
- #current_player ⇒ Object
- #double! ⇒ Object
- #finish! ⇒ Object
- #finished? ⇒ Boolean
- #give_out_cards ⇒ Object
- #hit! ⇒ Object
-
#initialize(args = {}) ⇒ Game
constructor
A new instance of Game.
- #initialize_players ⇒ Object
- #load_action(action) ⇒ Object
- #load_actions(actions) ⇒ Object
- #possible_actions ⇒ Object
- #prepare ⇒ Object
- #reindex_players! ⇒ Object
- #split! ⇒ Object
- #stand! ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Game
Returns a new instance of Game.
14 15 16 17 18 19 20 21 22 |
# File 'lib/blackjack/game.rb', line 14 def initialize(args={}) self.seed = args.fetch(:seed) self.bets = args.fetch(:bets) self.amount_decks = args.fetch(:amount_decks, 1) self.balance = args.fetch(:balance, 0) self.actions = [] prepare load_actions(args.delete(:actions) || []) end |
Instance Attribute Details
#actions ⇒ Object
Returns the value of attribute actions.
8 9 10 |
# File 'lib/blackjack/game.rb', line 8 def actions @actions end |
#amount_decks ⇒ Object
Returns the value of attribute amount_decks.
4 5 6 |
# File 'lib/blackjack/game.rb', line 4 def amount_decks @amount_decks end |
#balance ⇒ Object
Returns the value of attribute balance.
10 11 12 |
# File 'lib/blackjack/game.rb', line 10 def balance @balance end |
#bets ⇒ Object
Returns the value of attribute bets.
12 13 14 |
# File 'lib/blackjack/game.rb', line 12 def bets @bets end |
#dealer ⇒ Object
Returns the value of attribute dealer.
7 8 9 |
# File 'lib/blackjack/game.rb', line 7 def dealer @dealer end |
#deck ⇒ Object
Returns the value of attribute deck.
9 10 11 |
# File 'lib/blackjack/game.rb', line 9 def deck @deck end |
#finished ⇒ Object
Returns the value of attribute finished.
11 12 13 |
# File 'lib/blackjack/game.rb', line 11 def finished @finished end |
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/blackjack/game.rb', line 5 def @options end |
#players ⇒ Object
Returns the value of attribute players.
6 7 8 |
# File 'lib/blackjack/game.rb', line 6 def players @players end |
#seed ⇒ Object
Returns the value of attribute seed.
3 4 5 |
# File 'lib/blackjack/game.rb', line 3 def seed @seed end |
Instance Method Details
#all_players_finished? ⇒ Boolean
112 113 114 |
# File 'lib/blackjack/game.rb', line 112 def all_players_finished? players.all?(&:finished?) end |
#american_rules? ⇒ Boolean
66 67 68 |
# File 'lib/blackjack/game.rb', line 66 def american_rules? false end |
#current_player ⇒ Object
48 49 50 |
# File 'lib/blackjack/game.rb', line 48 def current_player players.detect(&:playing?) end |
#double! ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/blackjack/game.rb', line 87 def double! if current_player&.double! self.actions << :double! hit! if current_player&.must_hit? dealer.play! if all_players_finished? true end end |
#finish! ⇒ Object
104 105 106 |
# File 'lib/blackjack/game.rb', line 104 def finish! self.finished = true end |
#finished? ⇒ Boolean
108 109 110 |
# File 'lib/blackjack/game.rb', line 108 def finished? finished end |
#give_out_cards ⇒ Object
60 61 62 63 64 |
# File 'lib/blackjack/game.rb', line 60 def give_out_cards players.each(&:take_card!) dealer.take_card! players.each(&:take_card!) end |
#hit! ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/blackjack/game.rb', line 70 def hit! if current_player&.hit! self.actions << :hit! hit! if current_player&.must_hit? true end end |
#initialize_players ⇒ Object
32 33 34 35 36 |
# File 'lib/blackjack/game.rb', line 32 def initialize_players self.players = bets.collect{ |amount| Player.new(self, amount) } self.dealer = Dealer.new(self) reindex_players! end |
#load_action(action) ⇒ Object
56 57 58 |
# File 'lib/blackjack/game.rb', line 56 def load_action(action) send(action) end |
#load_actions(actions) ⇒ Object
42 43 44 45 46 |
# File 'lib/blackjack/game.rb', line 42 def load_actions(actions) actions.compact.each do |action| load_action(action) end end |
#possible_actions ⇒ Object
52 53 54 |
# File 'lib/blackjack/game.rb', line 52 def possible_actions current_player&.possible_actions end |
#prepare ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/blackjack/game.rb', line 24 def prepare self.deck = Deck.new(amount_decks, seed) initialize_players give_out_cards # one special case: all players have a BJ, then it's dealers turn dealer.play! if all_players_finished? end |
#reindex_players! ⇒ Object
38 39 40 |
# File 'lib/blackjack/game.rb', line 38 def reindex_players! players.each_with_index {|player, index| player.position = index} end |
#split! ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/blackjack/game.rb', line 96 def split! if current_player.split! self.actions << :split! hit! if current_player&.must_hit? true end end |
#stand! ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/blackjack/game.rb', line 78 def stand! if current_player&.stand! self.actions << :stand! hit! if current_player&.must_hit? dealer.play! if all_players_finished? true end end |