Class: Card

Inherits:
Object
  • Object
show all
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

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.

Parameters:

  • type (string)

    the type of the suit the card belongs to, i.e ‘spades’, ‘hearts’ etc.

  • number (int)

    the number of the card. Numbers 2 through 10 are simply the normal numbers, 11 is the ace, 12 jack, 13 queen, 14 king



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.

Parameters:

  • type (string)

    the suit identifier string (‘spades’, ‘hearts’, ‘clubs’ or ‘diamonds’)

  • number (int)

    the number of the card. Should be a number from 1 to 14

Returns:

  • (string, int, string, boolean)

    the suit of the card, the blackjack value of the card, the string representation of the card number and 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_overnil

Flips over the card so that the cardback is shown instead of the generated ASCII card

Returns:

  • (nil)


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

Parameters:

  • suit (string)

    the suit of the card

  • number (string)

    the number of the card (like ‘A’ for ace or ‘10’ for 10)

Returns:

  • (string)

    the ASCII card



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_cardbackstring

Generates a cardback ASCII art

Returns:

  • (string)

    the 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_cardstring

Returns the generated ASCII card, or the cardback if the card is currently flipped.

Returns:

  • (string)

    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_valueint

Returns the blackjack value of the card.

Returns:

  • (int)

    the blackjack value of the card



122
123
124
# File 'lib/blackjack/objects/card.rb', line 122

def get_value
    @value
end

#is_aceboolean

Returns if the card is an ace or not.

Returns:

  • (boolean)

    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_flippedboolean

Returns if the card is flipped or not.

Returns:

  • (boolean)

    if the card is flipped or not



132
133
134
# File 'lib/blackjack/objects/card.rb', line 132

def is_flipped
    @flipped
end