Class: Brigitte::Game
- Inherits:
-
Object
- Object
- Brigitte::Game
- Defined in:
- lib/brigitte/game.rb
Overview
A Game has maximum 4 players (active_players
) and can be started with play
method when all players are ready.
Instance Attribute Summary collapse
-
#active_players ⇒ Object
readonly
Returns the value of attribute active_players.
-
#cards ⇒ Object
readonly
Returns the value of attribute cards.
-
#current_player ⇒ Object
Returns the value of attribute current_player.
-
#game_over ⇒ Object
Returns the value of attribute game_over.
-
#pile ⇒ Object
readonly
Returns the value of attribute pile.
-
#removed_cards ⇒ Object
readonly
Returns the value of attribute removed_cards.
-
#winners ⇒ Object
readonly
Returns the value of attribute winners.
Class Method Summary collapse
-
.from_h(hash) ⇒ Object
rubocop:disable Metrics/AbcSize.
Instance Method Summary collapse
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #play ⇒ Object
-
#start_new_game(players, player_name_key: nil, player_id_key: nil) ⇒ Object
Starts the game with the provided
players
. - #take_blind_card(player, blind_card_index) ⇒ Object
- #take_cards_from_pile(player) ⇒ Object
- #throw_cards(player, *thrown_cards) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize ⇒ Game
Returns a new instance of Game.
16 17 18 19 20 21 22 23 |
# File 'lib/brigitte/game.rb', line 16 def initialize @active_players = [] @cards = [] @pile = [] @removed_cards = [] @winners = [] @game_over = false end |
Instance Attribute Details
#active_players ⇒ Object (readonly)
Returns the value of attribute active_players.
14 15 16 |
# File 'lib/brigitte/game.rb', line 14 def active_players @active_players end |
#cards ⇒ Object (readonly)
Returns the value of attribute cards.
14 15 16 |
# File 'lib/brigitte/game.rb', line 14 def cards @cards end |
#current_player ⇒ Object
Returns the value of attribute current_player.
13 14 15 |
# File 'lib/brigitte/game.rb', line 13 def current_player @current_player end |
#game_over ⇒ Object
Returns the value of attribute game_over.
13 14 15 |
# File 'lib/brigitte/game.rb', line 13 def game_over @game_over end |
#pile ⇒ Object (readonly)
Returns the value of attribute pile.
14 15 16 |
# File 'lib/brigitte/game.rb', line 14 def pile @pile end |
#removed_cards ⇒ Object (readonly)
Returns the value of attribute removed_cards.
14 15 16 |
# File 'lib/brigitte/game.rb', line 14 def removed_cards @removed_cards end |
#winners ⇒ Object (readonly)
Returns the value of attribute winners.
14 15 16 |
# File 'lib/brigitte/game.rb', line 14 def winners @winners end |
Class Method Details
.from_h(hash) ⇒ Object
rubocop:disable Metrics/AbcSize
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/brigitte/game.rb', line 117 def self.from_h(hash) # rubocop:disable Metrics/AbcSize game = new hash[:active_players].each { |h| game.active_players << Player.from_h(h) } hash[:cards].each { |h| game.cards << Card.from_h(h) } hash[:pile].each { |h| game.pile << Card.from_h(h) } hash[:removed_cards].each { |h| game.removed_cards << Card.from_h(h) } game.current_player = Player.from_h(hash[:current_player]) hash[:winners].each { |h| game.winners << Player.from_h(h) } game.game_over = hash[:game_over] game end |
Instance Method Details
#play ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/brigitte/game.rb', line 64 def play return false unless @active_players.all?(&:ready) return @current_player if @current_player @current_player = @active_players.min do |p1, p2| p1.hand.map(&:order_level).min <=> p2.hand.map(&:order_level).min end end |
#start_new_game(players, player_name_key: nil, player_id_key: nil) ⇒ Object
Starts the game with the provided players
.
Returns this Game instance.
Arguments
players
- (Array) containing player names. Default strings of names.
Optional arguments
When players
is an array of hashes.
player_name_key
: - The key of name in player hash.
player_id_key
: - The key of id in player hash.
Examples
start_new_game(['Bell', 'Biv', 'Devoe'])
or
start_new_game(
[{ name: 'Bell', id: 1 },
{ name: 'Biv', id: 2 },
{ name: 'Devoe', id: 3 }],
player_name_key: :name,
player_id_key: :id
)
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/brigitte/game.rb', line 50 def start_new_game(players, player_name_key: nil, player_id_key: nil) if player_name_key && player_id_key players.each do |p| @active_players << Player.new(p[player_name_key], p[player_id_key]) end else players.each { |pn| @active_players << Player.new(pn) } end @cards = Deck.new.cards deal_cards self end |
#take_blind_card(player, blind_card_index) ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'lib/brigitte/game.rb', line 96 def take_blind_card(player, blind_card_index) return false if player != @current_player return false if @cards.any? return false if player.visible_cards.any? return false if player.hand.any? player.pull_blind_card(blind_card_index) end |
#take_cards_from_pile(player) ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/brigitte/game.rb', line 87 def take_cards_from_pile(player) return false unless player == @current_player player.hand.push(*@pile.pop(@pile.count)) player.sort_hand! select_next_player(force: true) unless @game_over end |
#throw_cards(player, *thrown_cards) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/brigitte/game.rb', line 73 def throw_cards(player, *thrown_cards) return false unless player == @current_player return false unless Commands::Pile::AddCards.process( player, thrown_cards, @pile, @removed_cards ) take_cards(player) take_visible_cards(player) player_won(player) select_next_player true end |
#to_h ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/brigitte/game.rb', line 105 def to_h { active_players: active_players.map(&:to_h), cards: cards.map(&:to_h), pile: pile.map(&:to_h), removed_cards: removed_cards.map(&:to_h), current_player: current_player.to_h, winners: winners.map(&:to_h), game_over: game_over } end |