Last active
December 29, 2015 09:19
-
-
Save jmoon90/7649252 to your computer and use it in GitHub Desktop.
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
| require 'pry' | |
| class BlackJack | |
| def initialize | |
| @deck = Deck.new.build_deck | |
| @players_hand = {} | |
| @symbol_value = PlayingCards.new.symbols | |
| player | |
| @players_score = Hash.new(0) | |
| player_score | |
| end | |
| def player | |
| puts "How many players are there?" | |
| print "> \n " | |
| #players = gets.chomp | |
| #players += 1 | |
| players = 2 | |
| player_hands#(players) | |
| deal_initial_hand(players) | |
| end | |
| def player_hands | |
| i = 1 | |
| #players.times do |player| | |
| 2.times do |player| | |
| @players_hand[i] = [] | |
| i += 1 | |
| end | |
| end | |
| def deal_initial_hand(players) | |
| 2.times do | |
| @players_hand.each do |player, card| | |
| card << @deck.pop | |
| end | |
| end | |
| initial_hand_message | |
| end | |
| def initial_hand_message | |
| @players_hand.each do |player,card| | |
| puts "Player #{player} was dealth #{card[0]} and #{card[1]}" | |
| end | |
| end | |
| def player_score | |
| i = 0 | |
| @players_hand.each do |player, hand| | |
| @score = 0 | |
| symbol_value(hand, player) | |
| puts "Player#{player} score: #{@score}" | |
| @players_score[player] = @score | |
| end | |
| turn(@players_hand) | |
| end | |
| def player_scores(player) | |
| i = 0 | |
| @score = 0 | |
| @players_score[player] = @score | |
| symbol_value(@players_hand[player], player) | |
| end | |
| def symbol_value(card, player) | |
| i = 0 | |
| card.count.times do | |
| if card[i][0] == "A" | |
| @score += 1 if @players_score[player] > 11 | |
| @score += 11 if @players_score[player] < 11 | |
| elsif @symbol_value[card[i][0]] != nil | |
| @score += @symbol_value[card[i][0]] | |
| else | |
| @score += card[i][(/\d+/)].to_i | |
| end | |
| i += 1 | |
| end | |
| end | |
| def turn(player) | |
| player.each do |player, cards| | |
| puts "\nPlayer #{player} turn" | |
| hit_or_stand?(player) | |
| end | |
| end | |
| def hit_or_stand?(player) | |
| print "Hit or stand (H/S): " | |
| decision = gets.chomp.downcase | |
| hit(player) if decision == "h" | |
| stand(player) if decision == "s" | |
| end | |
| def hit(player) | |
| new_card = @deck.pop | |
| @players_hand[player] << new_card | |
| puts "Player #{player} was dealth #{new_card}" | |
| player_scores(player) | |
| puts "Players score: #{@players_score[player]}\n " | |
| bust?(player) | |
| end | |
| def stand(player) | |
| puts "Player #{player} score: #{@players_score[player]}" | |
| win? if player == @players_score.keys.last | |
| end | |
| def bust?(player) | |
| if @players_score[player] > 21 | |
| puts "You Busted" | |
| return | |
| else | |
| hit_or_stand?(player) | |
| end | |
| end | |
| def win? | |
| if @players_score[1] <= 21 && @players_score[1] > @players_score[2] | |
| puts "You win!" | |
| else | |
| puts "You lost." | |
| end | |
| end | |
| end | |
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
| require 'pry' | |
| class Deck | |
| def build_deck | |
| deck = [] | |
| PlayingCards::SUITS.each do |suit| | |
| PlayingCards::VALUES.each do |value| | |
| deck.push(value + suit) | |
| end | |
| end | |
| deck.shuffle | |
| end | |
| end | |
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 PlayingCards | |
| SUITS = ['♠', '♣', '♥', '♦'] | |
| VALUES = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'] | |
| def symbols | |
| @symbol_values = { | |
| "J" => 10, | |
| "Q" => 10, | |
| "K" => 10, | |
| "A" => [1, 11] | |
| } | |
| end | |
| end | |
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
| require 'pry' | |
| require_relative "deck" | |
| require_relative "playingcard" | |
| require_relative "blackjack" | |
| puts "Welcome to Blackjack!" | |
| BlackJack.new | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment