Zrp200/blackjack1

View on GitHub
bin/blackjack1

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
BEGIN {
    helpmessage = <<HELP
\u00B7Goals:
    \u00B7Beat the dealer by having your value be greater than his, without going over 21 and busting.
    \u00B7Get blackjack by having your starting hand have a value of 21.
\u00B7Values:
    \u00B7Ace => 1 or 11
    \u00B7Number => itself
    \u00B7King, Queen, Jack => 10 
HELP
    if ARGV[0].class == String
        if ARGV[0].include? "-h"
            puts helpmessage
            exit
        end
    end
}
require "blackjack1"
Hand.deck, inputerror = CardDeck::Deck.new, "Invalid input. Please input '(h)it' or '(s)tay'"
dealer, player, view = Hand.new, Hand.new, proc do |player|
    puts "\tYou have: #{player.view}"
    sleep(0.5)
    puts "\tValue: #{player.value}"
    sleep(1)
end
puts
unless player.blackjack?
    puts "\tThe dealer is showing #{dealer.cards[1].abbr}\n"
    sleep 1
    loop do
        puts
        view.call player
        if player.cards.length == 2 then print "\t(d)ouble, (h)it, or (s)tay? "
        else; print "\t(h)it or (s)tay? "
        end
        action = gets.chomp.downcase
        player.hit if action.include?("h")
        break if action.include?("s") || player.value >= 21
        if action.include?("d") and player.length == 21
            player.hit
            break
        end
    end
    puts ""
    unless player.bust?
        puts "\tYou finished with #{player.value}."
    else
        puts "\tYou busted @ #{player.value}."
        sleep(1)
        puts "\tDealer wins"
        sleep(2)
        exit
    end
    sleep 2
    if dealer.blackjack?
        puts "\tDealer got blackjack."
        puts; sleep 0.5
        puts "\tDealer wins."
        sleep 2
        exit
    end
    puts
    while dealer.value <= Hand::MDHV
        puts "\tThe dealer hits!"
        dealer.hit
        sleep 0.5
    end
    puts
    if dealer.bust? then puts "\tThe dealer busted @ #{dealer.value}!"
    else
        puts "The dealer stays @ #{dealer.value}"
    end
    sleep 2
    puts compare_score player, dealer
else
    puts "\tYou got blackjack!"
    puts; sleep 1
    puts "\tYou win!"
    sleep 0.5
end