class GameShuffleCards::Game

This class is the main class to shuffle a deck of cards @author Diego Piccinini Lagos

Constants

CARDS_PER_PLAYER

the defaul value of cards per player

CARDS_VALUES

The cards values collection

MAXIMUN_PLAYERS

Maximun players allowed in a Game

MINIMUN_CARDS

the minimun cards per player

MINIMUN_PLAYERS

the minimun players in a game

PLAYERS

the default value of players

SUITS

The suit collection of the deck of cards

TOTAL_CARDS

Count total cards in a deck

Attributes

cards_availables[RW]
cards_collection[R]
cards_per_player[R]
maximun_cards_per_player[R]
players[R]

Public Class Methods

get_maximun_cards_per_player(players) click to toggle source

Get the maximun of cards per player, given a card collection and a number of players @return maximun cards per player [Integer]

# File lib/game_shuffle_cards/game.rb, line 79
def self.get_maximun_cards_per_player(players)
        players = ValidateGame.parse_and_validate_players(players)
        TOTAL_CARDS / players
end
new(players = PLAYERS, cards_per_player=CARDS_PER_PLAYER) click to toggle source

Initialize the game players and cards per player you want @param players [String|Integer] the number of players in the game @param cards_per_player [String|Integer] the number of cards that will recieve each player

# File lib/game_shuffle_cards/game.rb, line 34
def initialize(players = PLAYERS, cards_per_player=CARDS_PER_PLAYER)
        # Validate the input params and parse values
        @players, @cards_per_player = ValidateGame.parse_and_validate(players,cards_per_player)

        # The card collection, all the cards in the deck
        @cards_collection = {}

        CARDS_VALUES.each do |card_value|
                SUITS.each do |suit|
                        # Create one card and add it to cards_collections
                        card = Card.new(card_value,suit)
                        @cards_collection[card.id]=card
                end
                # add the cards keys to the cards availables
                @cards_availables= @cards_collection.keys
        end
        # Initialize the maximun possible of cards per player,
        # given a card collection and a number of players
        @maximun_cards_per_player = Game.get_maximun_cards_per_player(@players)

end

Public Instance Methods

get_cards() click to toggle source

Get cards of the cards still availables in the deck @return group of cards [Array] an array of cards_per_player

# File lib/game_shuffle_cards/game.rb, line 70
def get_cards
        cards=@cards_availables.sample(@cards_per_player)
        @cards_availables-= cards
        cards
end
results() click to toggle source

Shuffle the deck of cards @return a group of players [Hash] each with their cards

# File lib/game_shuffle_cards/game.rb, line 60
def results
        players={}
        @players.times do |player|
                players[player]=get_cards
        end
        return players
end