This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Card = Struct.new(:rank, :suit) do | |
def to_s; "#{rank} of #{suit}"; end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'values' | |
require 'rank' | |
class Card < Value.new(:rank, :suit) | |
def suit | |
(@suit || 'naked').to_sym | |
end | |
def to_s | |
"#{rank.to_s} of #{suit.to_s.capitalize}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A _Deck_ of _Card_s is used to play many different types of games. | |
_Card_s have a _Rank_ and a _Suit_ and depending on the rules of the game, | |
these are used to score. Also, many games require a _shoe size_, which | |
specifies how many standard _Deck_s of _Card_s to include in the generated | |
_Deck_. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Deck | |
attr_reader :cards | |
def initialize(show_size=1) | |
@cards = build_deck*shoe_size | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rank = Struct.new(:rank, :value) do | |
def to_s; rank; end | |
end | |
Card = Struct.new(:rank, :suit) do | |
def to_s; "#{rank} of #{suit}"; end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Deck.new.cards[24].to_s #=> "seven of hearts" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Deck | |
SUITS = %w(hearts clubs spades diamonds) | |
RANKS = %w(ace two three four five six seven | |
eight nine ten jack queen king) | |
... | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Deck | |
... | |
protected | |
def all_ranks | |
RANKS.map.with_index {|rank, value| Rank.new(rank, value) } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Deck | |
... | |
protected | |
def build_deck | |
all_ranks.flat_map {|rank| SUITS.flat_map {|suit| Card.new(rank,suit) } } | |
end | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rank = Struct.new(:rank, :value) do | |
def to_s; rank; end | |
end | |
Card = Struct.new(:rank, :suit) do | |
def to_s; "#{rank} of #{suit}"; end | |
end | |
class Deck | |
SUITS = %w(hearts clubs spades diamonds) |