Last active
December 22, 2015 14:58
-
-
Save ashleytbasinger/7791c03723d4c5e9bacc to your computer and use it in GitHub Desktop.
blackjack OOP
This file contains 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
require 'pry' | |
class Game | |
def initialize | |
puts "\nWELCOME to BLACKJACK!\n" | |
Deck.new | |
end | |
def deal_cards | |
2.times{@player << @deck.pop} | |
2.times{@dealer << @deck.pop} | |
score(@player) | |
end | |
def display_cards(someone) | |
if someone == @player | |
puts "Player's hand: " | |
puts @player | |
puts "Player's score is #{score(@player)}" | |
still_playing | |
else | |
score(@dealer) | |
puts "Dealer's hand: " | |
puts @dealer | |
end | |
end | |
def dealers_turn | |
display_cards(@dealer) | |
#score(@dealer) | |
puts "Dealer's score: #{score(@dealer)}" | |
if score(@dealer) < 17 | |
hit(@dealer) | |
dealers_turn | |
elsif score(@dealer) >= 17 && score(@dealer) <= 21 | |
final_results | |
elsif score(@dealer) > 21 | |
puts "Dealer busts- you win!" | |
end | |
end | |
def still_playing | |
if score(@player) > 21 | |
puts 'Busted! Game over.' | |
return nil | |
end | |
puts '' | |
puts "Hit or Stay (h/s): " | |
input = gets.chomp | |
if input == 'h' | |
hit(@player) | |
elsif input == 's' | |
dealers_turn | |
else | |
puts "invalid input" | |
still_playing | |
end | |
end | |
def hit(someone) | |
someone << @deck.pop | |
score(someone) | |
display_cards(someone) | |
end | |
def score(someone) | |
hand_score = 0 | |
has_ace = false | |
someone.each do |card| | |
card = card.chop | |
if card == 'J' || card == 'Q' || card == 'K' | |
hand_score += 10 | |
elsif card == 'A' | |
has_ace = true | |
hand_score += 1 | |
else | |
hand_score += card.to_i | |
end | |
end | |
if hand_score <= 11 && has_ace | |
hand_score += 10 | |
end | |
return hand_score | |
display_cards | |
end | |
def final_results | |
puts '' | |
puts "Player's score: #{score(@player)}" | |
puts "Dealer's score: #{score(@dealer)}" | |
if score(@dealer) > score(@player) | |
puts "Dealer wins!" | |
elsif score(@player) > score(@dealer) | |
puts "You win!" | |
else | |
puts "Tie!" | |
end | |
return nil | |
end | |
class Card < Game | |
def initialize(value, suit) | |
@value = value | |
@suit = suit | |
end | |
end | |
class Deck < Game | |
SUITS = ['♠', '♣', '♥', '♦'] | |
VALUES = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'] | |
def initialize | |
@deck = [] | |
@dealer = [] | |
@player = [] | |
build_deck | |
deal_cards | |
display_cards(@player) | |
end | |
def build_deck | |
SUITS.each do |suit| | |
VALUES.each do |value| | |
@deck.push(value + suit) | |
end | |
end | |
@deck.shuffle! | |
end | |
def pop | |
@deck.pop | |
end | |
end | |
end | |
blackjack = Game.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment