diff --git a/lib/blackjack_score.rb b/lib/blackjack_score.rb index 18110b9..c0770a5 100644 --- a/lib/blackjack_score.rb +++ b/lib/blackjack_score.rb @@ -1,7 +1,42 @@ # blackjack_score.rb - -VALID_CARDS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 'King', 'Queen', 'Jack'] +require "pry" +VALID_CARDS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'King', 'Queen', 'Jack'] def blackjack_score(hand) - + # Raise ArgumentError for invalid cards + if !hand.is_a?(Array) + raise ArgumentError, "Hand must be an array" + elsif hand.size > 3 || hand.size < 1 + raise ArgumentError, "Hand must be 1-3 cards" + else + hand.each do |card| + if !VALID_CARDS.include?(card) + raise ArgumentError, "Invalid card(s)" + end + end + end + # Generates parallel array for score, e.g. [10, 1, 3] + scores_low_ace = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] + card_to_score_low_ace = Hash[VALID_CARDS.zip(scores_low_ace)] # {1=>1, 2=>2, 3=>3, 4=>4, 5=>5, 6=>6, 7=>7, 8=>8, 9=>9, "King"=>10, "Queen"=>10, "Jack"=>10} + scores = [] + hand.size.times do |i| + sum_before_this_card = scores.reduce(0, :+) + # ace default score is 1 + # if card is an ace, score can be 11 in this case ONLY: + if hand[i] == 1 && sum_before_this_card < 10 + scores << 11 + else + # if card is not an ace + card_score = card_to_score_low_ace[hand[i]] + scores << card_score + end + end + # Sum + score = scores.reduce(:+) + # raise ArgumentError for scores over 21 + if score > 21 + raise ArgumentError, "You lose! Score > 21" + end + # return score + return score end diff --git a/specs/blackjack_score_spec.rb b/specs/blackjack_score_spec.rb index 6ef084a..9f9ec77 100644 --- a/specs/blackjack_score_spec.rb +++ b/specs/blackjack_score_spec.rb @@ -1,43 +1,84 @@ +#blackjack_score_spec.rb + require 'minitest' require 'minitest/spec' require 'minitest/autorun' require 'minitest/reporters' require 'minitest/pride' +require "minitest/skip_dsl" require_relative '../lib/blackjack_score' Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -describe 'Blackjac Score' do +describe 'Blackjack Score' do it 'can calculate the score for a pair of number cards' do - # Arrange - hand = [3, 4] - + hand = [6, 5] # Act score = blackjack_score(hand) - # Assert <- You do this part! - + expect(score).must_equal 11 end it 'facecards have values calculated correctly' do - + face_cards = ["Jack", "Queen", "King"] + face_cards.each do |card| + # Arrange + hand = Array.new + 2.times {hand.push(card)} + # Act + score = blackjack_score(hand) + # Assert <- You do this part! + expect(score).must_equal 20 + end end it 'calculates aces as 11 where it does not go over 21' do - + # Arrange + hand = [9,1] + # Act + score = blackjack_score(hand) + # Assert <- You do this part! + expect(score).must_equal 20 end it 'calculates aces as 1, if an 11 would cause the score to go over 21' do + # Arrange + hand = [10, 10, 1] + # Act + score = blackjack_score(hand) + # Assert <- You do this part! + expect(score).must_equal 21 + # Arrange + hand = [10,1] + # Act + score = blackjack_score(hand) + # Assert <- You do this part! + expect(score).must_equal 11 end it 'raises an ArgumentError for invalid cards' do - + # ARRANGE + possible_hands = [[10,1],[6,5]] + # Act-Assert + possible_hands.each do |hand| + score = blackjack_score(hand) + expect(score).must_equal 11 + end + # Re-arrange + possible_hands = [[], nil, [0], ["Joker"], [1,2,3,4]] + # Act-Assert + possible_hands.each do |hand| + expect{blackjack_score(hand)}.must_raise ArgumentError + end end it 'raises an ArgumentError for scores over 21' do - + # ARRANGE + hand = [10,10,2] + # ACT-ASSERT + expect{blackjack_score(hand)}.must_raise ArgumentError end end