Last active
December 25, 2015 18:08
-
-
Save gjackson12/7017777 to your computer and use it in GitHub Desktop.
I probably should create a game class for the flow but not sure how to go about it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Deck | |
| SUITS = ['♠', '♣', '♥', '♦'] | |
| VALUES = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'] | |
| def initialize | |
| @cards = [] | |
| SUITS.each do |suit| | |
| VALUES.each do |value| | |
| @cards.push(Card.new(value,suit)) | |
| end | |
| end | |
| @cards.shuffle! | |
| end | |
| def deal_card | |
| @cards.pop | |
| end | |
| end | |
| class Card | |
| attr_reader :value, :suit | |
| def initialize(value, suit) | |
| @value = value | |
| @suit = suit | |
| end | |
| end | |
| class Hand | |
| def initialize(player_title) | |
| @player_title = player_title | |
| @cards_in_hand = [] | |
| end | |
| def hit(card) | |
| @cards_in_hand << card | |
| end | |
| def stay | |
| puts "#{@player_title} score is #{self.score}" | |
| end | |
| def score | |
| score = 0 | |
| has_ace = false | |
| @cards_in_hand.each do |card| | |
| if card.value == 'A' | |
| score += 1 | |
| has_ace = true | |
| elsif card.value == "K" || card.value == "Q" || card.value == "J" | |
| score += 10 | |
| else | |
| score += card.value.to_i | |
| end | |
| end | |
| if has_ace && score <= 11 | |
| score += 10 | |
| end | |
| return score | |
| end | |
| def bust? | |
| score > 21 | |
| end | |
| def display_hand | |
| if @cards_in_hand.length <= 2 | |
| @cards_in_hand.each do |card| | |
| puts "#{@player_title} was dealt #{card.value} #{card.suit}" | |
| end | |
| else | |
| puts "#{@player_title} was dealt #{@cards_in_hand.last.value} #{@cards_in_hand.last.suit}" | |
| end | |
| end | |
| end | |
| deck = Deck.new | |
| player = Hand.new("Player") | |
| dealer = Hand.new("Dealer") | |
| # Initial Deal | |
| 2.times {player.hit(deck.deal_card)} | |
| 2.times {dealer.hit(deck.deal_card)} | |
| puts "---------Welcome to BlackJack--------" | |
| puts "" | |
| while player.score < 22 | |
| player.display_hand | |
| puts "Player score #{player.score}" | |
| puts "" | |
| print "Hit or stand (H/S):" | |
| move = gets.chomp | |
| puts "" | |
| if move == 'h' | |
| player.hit(deck.deal_card) | |
| elsif move == 's' | |
| player.stay | |
| break | |
| end | |
| if player.bust? | |
| player.display_hand | |
| puts "Player score #{player.score}" | |
| puts "" | |
| puts "Bust! You lose..." | |
| exit | |
| end | |
| end | |
| while true | |
| dealer.display_hand | |
| puts "Dealer score #{dealer.score}" | |
| puts "" | |
| if dealer.score < 17 | |
| dealer.hit(deck.deal_card) | |
| elsif dealer.bust? | |
| puts "Dealer busts! You win!" | |
| exit | |
| else | |
| puts "Dealer stands" | |
| puts "" | |
| dealer.stay | |
| break | |
| end | |
| end | |
| if player.score <= 21 && dealer.score <= 21 | |
| if player.score > dealer.score | |
| puts "You win!" | |
| else | |
| puts "You lose!" | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment