Class: BlackjackCli
- Inherits:
-
Object
- Object
- BlackjackCli
- Defined in:
- lib/blackjack/blackjack_cli.rb
Overview
Class that offers an interactive interface for the user to play blackjack
Instance Method Summary collapse
-
#format_cards(cards) ⇒ string
Formats cards to be displayed side-by-side.
-
#game_loop ⇒ nil
the main game loop.
-
#get_play_command ⇒ string
Gets the user’s input on what to do next.
-
#initialize ⇒ nil
constructor
Constructor for the BlackJackCli class.
-
#pause_when_game_ends ⇒ nil
This pauses the game until the user presses enter/return before starting a new game.
-
#print_result(result) ⇒ nil
Prints the current game state based on the results of a hit or stand action.
Constructor Details
#initialize ⇒ nil
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
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_loop ⇒ nil
the main game loop. It continually asks the user for a new choice of either hit or stand
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_command ⇒ string
Gets the user’s input on what to do next
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_ends ⇒ nil
This pauses the game until the user presses enter/return before starting a new game
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 |
#print_result(result) ⇒ nil
Prints the current game state based on the results of a hit or stand action
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 |