Class: GameShuffleCards::Game

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

Overview

This class is the main class to shuffle a deck of cards

Author:

Constant Summary

MINIMUN_CARDS =

the minimun cards per player

1
MINIMUN_PLAYERS =

the minimun players in a game

1
PLAYERS =

the default value of players

2
CARDS_PER_PLAYER =

the defaul value of cards per player

3
SUITS =

The suit collection of the deck of cards

['spades', 'hearts', 'diamonds', 'clubs']
CARDS_VALUES =

The cards values collection

['A','2','3','4','5','6','7','8','9','10','J','Q','K']
TOTAL_CARDS =

Count total cards in a deck

SUITS.count * CARDS_VALUES.count
MAXIMUN_PLAYERS =

Maximun players allowed in a Game

TOTAL_CARDS

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Game) initialize(players = PLAYERS, cards_per_player = CARDS_PER_PLAYER)

Initialize the game players and cards per player you want

Parameters:

  • players (String|Integer) (defaults to: PLAYERS)

    the number of players in the game

  • cards_per_player (String|Integer) (defaults to: CARDS_PER_PLAYER)

    the number of cards that will recieve each player



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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

Instance Attribute Details

- (Object) cards_availables

Returns the value of attribute cards_availables



56
57
58
# File 'lib/game_shuffle_cards/game.rb', line 56

def cards_availables
  @cards_availables
end

- (Object) cards_collection (readonly)

Returns the value of attribute cards_collection



55
56
57
# File 'lib/game_shuffle_cards/game.rb', line 55

def cards_collection
  @cards_collection
end

- (Object) cards_per_player (readonly)

Returns the value of attribute cards_per_player



55
56
57
# File 'lib/game_shuffle_cards/game.rb', line 55

def cards_per_player
  @cards_per_player
end

- (Object) maximun_cards_per_player (readonly)

Returns the value of attribute maximun_cards_per_player



55
56
57
# File 'lib/game_shuffle_cards/game.rb', line 55

def maximun_cards_per_player
  @maximun_cards_per_player
end

- (Object) players (readonly)

Returns the value of attribute players



55
56
57
# File 'lib/game_shuffle_cards/game.rb', line 55

def players
  @players
end

Class Method Details

+ (Object) get_maximun_cards_per_player(players)

Get the maximun of cards per player, given a card collection and a number of players

Returns:

  • maximun cards per player [Integer]



79
80
81
82
# 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

Instance Method Details

- (Object) get_cards

Get cards of the cards still availables in the deck

Returns:

  • group of cards [Array] an array of cards_per_player



70
71
72
73
74
# File 'lib/game_shuffle_cards/game.rb', line 70

def get_cards
	cards=@cards_availables.sample(@cards_per_player)
	@cards_availables-= cards
	cards
end

- (Object) results

Shuffle the deck of cards

Returns:

  • a group of players [Hash] each with their cards



60
61
62
63
64
65
66
# File 'lib/game_shuffle_cards/game.rb', line 60

def results
	players={}
	@players.times do |player|
		players[player]=get_cards
	end
	return players
end