Class: Card
- Inherits:
-
Object
- Object
- Card
- Defined in:
- lib/blackjack/objects/card.rb
Overview
Class that models a playing card. It manages the value of the card in regards to blackjack and generates ASCII art of the card
Instance Method Summary collapse
-
#determine_card_data(type, number) ⇒ string, ...
Determines the suit of the card, its blackjack value, its number to be shown visually as well as if the card is an ace.
-
#flip_over ⇒ nil
Flips over the card so that the cardback is shown instead of the generated ASCII card.
-
#format_card(suit, number) ⇒ string
Generates an ASCII playing card from the provided information.
-
#generate_cardback ⇒ string
Generates a cardback ASCII art.
-
#get_ascii_card ⇒ string
The generated ASCII card, or the cardback if the card is currently flipped.
-
#get_value ⇒ int
The blackjack value of the card.
-
#initialize(type, number) ⇒ nil
constructor
Creates a new card The constructor generates ASCII art of the card and calculates its blackjack value The card is also assigned a cardback.
-
#is_ace ⇒ boolean
If the card is an ace or not.
-
#is_flipped ⇒ boolean
If the card is flipped or not.
Constructor Details
#initialize(type, number) ⇒ nil
Creates a new card The constructor generates ASCII art of the card and calculates its blackjack value The card is also assigned a cardback.
36 37 38 39 40 41 42 |
# File 'lib/blackjack/objects/card.rb', line 36 def initialize(type, number) suit, @value, display_number, @ace = determine_card_data(type, number) @backside = generate_cardback @flipped = false @ascii_card = format_card(suit, display_number) end |
Instance Method Details
#determine_card_data(type, number) ⇒ string, ...
Determines the suit of the card, its blackjack value, its number to be shown visually as well as if the card is an ace.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/blackjack/objects/card.rb', line 70 def determine_card_data(type, number) suits = {'spades' => '♠', 'diamonds' => '♦', 'hearts' => '♥', 'clubs' => '♣'} special_cards = {10 => '10', 12 => 'B', 13 => 'Q', 14 => 'K'} ace = false suit = suits[type] if type == 'spades' or type == 'clubs' suit = suit.set_attributes([BLACK_FG], [WHITE_BG, BLACK_FG]) else suit = suit.set_attributes([RED_FG], [WHITE_BG, BLACK_FG]) end if number < 10 and number != 1 value = number display_number = number.to_s elsif number >= 10 and number != 11 value = 10 display_number = special_cards[number] else ace = true value = 11 # Set value of ace to 11, since we can then just downgrade the value to 1 if the need arises display_number = 'A' end return suit, value, display_number, ace end |
#flip_over ⇒ nil
Flips over the card so that the cardback is shown instead of the generated ASCII card
109 110 111 112 113 114 |
# File 'lib/blackjack/objects/card.rb', line 109 def flip_over cached = @ascii_card @ascii_card = @backside @backside = cached @flipped = !@flipped end |
#format_card(suit, number) ⇒ string
Generates an ASCII playing card from the provided information
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/blackjack/objects/card.rb', line 49 def format_card(suit, number) top = '┌─────────┐' empty_row = "| |\n" bottom = '└─────────┘' offset = 8 - number.length ascii_card = "#{top}\n| #{number}" + ' ' * offset + "|\n" ascii_card += empty_row * 2 + "| #{suit} |\n" + empty_row * 2 ascii_card += '|' + ' ' * offset + "#{number} |\n#{bottom}" ascii_card.set_attributes([WHITE_BG, BLACK_FG]) end |
#generate_cardback ⇒ string
Generates a cardback ASCII art
101 102 103 104 105 |
# File 'lib/blackjack/objects/card.rb', line 101 def generate_cardback ("┌─────────┐\n" + ("│░░░░░░░░░│\n" * 7) + '└─────────┘').set_attributes([WHITE_BG, BLACK_FG]) end |
#get_ascii_card ⇒ string
Returns the generated ASCII card, or the cardback if the card is currently flipped.
117 118 119 |
# File 'lib/blackjack/objects/card.rb', line 117 def get_ascii_card @ascii_card end |
#get_value ⇒ int
Returns the blackjack value of the card.
122 123 124 |
# File 'lib/blackjack/objects/card.rb', line 122 def get_value @value end |
#is_ace ⇒ boolean
Returns if the card is an ace or not.
127 128 129 |
# File 'lib/blackjack/objects/card.rb', line 127 def is_ace @ace end |
#is_flipped ⇒ boolean
Returns if the card is flipped or not.
132 133 134 |
# File 'lib/blackjack/objects/card.rb', line 132 def is_flipped @flipped end |