Class: BlackjackCli

Inherits:
Object
  • Object
show all
Defined in:
lib/blackjack/blackjack_cli.rb

Overview

Class that offers an interactive interface for the user to play blackjack

Instance Method Summary collapse

Constructor Details

#initializenil

Constructor for the BlackJackCli class



30
31
32
33
34
# File 'lib/blackjack/blackjack_cli.rb', line 30

def initialize
    @default_text_color = [LIGHT_GRAY_BG, BLACK_FG]
    puts " Blackjack CLI started \n\n".set_attributes(@default_text_color)
    @blackjack = Game.new
end

Instance Method Details

#format_cards(cards) ⇒ string

Formats cards to be displayed side-by-side

Parameters:

  • cards (Card Array)

    the cards to be displayed

Returns:

  • (string)

    a formatted string of all cards side by side



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/blackjack/blackjack_cli.rb', line 98

def format_cards(cards)
    cards_string = ''

    card_parts = []
    cards.each { |card|
        card_parts.push(card.get_ascii_card.split("\n"))
    }

    if card_parts.length == 0
        return ''
    end

    (0...card_parts[0].length).each { |i|
        card_parts.each { |card_part|
            cards_string += card_part[i] + '   '
        }
        cards_string += "\n"
    }
    cards_string
end

#game_loopnil

the main game loop. It continually asks the user for a new choice of either hit or stand

Returns:

  • (nil)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/blackjack/blackjack_cli.rb', line 38

def game_loop
    new_game = true

    while true
        if new_game
        print_result(@blackjack.start)
        new_game = false
    end

    result = nil
    case get_play_command
        when "stand\n"
            result = @blackjack.stand
        when "hit\n"
            result = @blackjack.hit
        else
            puts 'Something went wrong'
    end

    print_result(result)

    if result[2] == 'win'
        puts ' You won :D '.set_attributes([GREEN_BG, BLACK_FG])
    elsif result[2] == 'loss'
        puts ' You lost :( '.set_attributes([RED_BG, BLACK_FG])
    elsif result[2] == 'draw'
        puts " It's a draw ".set_attributes([[YELLOW_BG, BLACK_FG]])
    end

    if result[2] != 'undecided'
        new_game = true
        pause_when_game_ends
    end

    end
end

#get_play_commandstring

Gets the user’s input on what to do next

Returns:

  • (string)

    the (validated) user input



121
122
123
124
125
126
127
128
129
130
# File 'lib/blackjack/blackjack_cli.rb', line 121

def get_play_command
puts ' What would you like to do? (hit|stand) '.set_attributes(@default_text_color)
input = gets
while not input == "hit\n" and not input == "stand\n"
    puts " Please enter 'hit' or 'stand' ".set_attributes(@default_text_color)
    input = gets
end
puts "\n"
input
end

#pause_when_game_endsnil

This pauses the game until the user presses enter/return before starting a new game

Returns:

  • (nil)


77
78
79
80
# File 'lib/blackjack/blackjack_cli.rb', line 77

def pause_when_game_ends
    puts ' Press enter to start a new game '.set_attributes(@default_text_color)
    gets
end

Prints the current game state based on the results of a hit or stand action

Parameters:

  • result (Card Array, Card Array)

    the result to be displayed

Returns:

  • (nil)


85
86
87
88
89
90
91
92
93
# File 'lib/blackjack/blackjack_cli.rb', line 85

def print_result(result)
    puts " Player Cards: (#{Game.calculate_optimal_card_score(result[0])}) \n"
             .set_attributes(@default_text_color)
    puts format_cards(result[0])
    puts "\n Dealer Cards: (#{Game.calculate_optimal_card_score(result[1])}) \n"
             .set_attributes(@default_text_color)
    puts format_cards(result[1])
    puts "\n"
end