diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4ebc8ae --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +coverage diff --git a/lib/blackjack_score.rb b/lib/blackjack_score.rb index fca19e0..a7c4bb7 100644 --- a/lib/blackjack_score.rb +++ b/lib/blackjack_score.rb @@ -3,5 +3,35 @@ VALID_CARDS = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King', 'Ace'] def blackjack_score(hand) + score = 0 + ace_count = 0 + hand.each do |card| + unless VALID_CARDS.include?(card) + raise ArgumentError.new("#{card} is invalid") + end + case card + when 'Jack', 'Queen', 'King' + score += 10 + when 'Ace' + ace_count += 1 + score += 11 + else + score += card + end + end + + while score > 21 && ace_count > 0 + score -= 10 + ace_count -= 1 + end + + if score > 21 + raise ArgumentError.new("Bust! Score is over 21") + end + + return score end + +# Maybe I should create a separate variable for ace? + diff --git a/test/blackjack_score_test.rb b/test/blackjack_score_test.rb index 3175a82..89e7a4b 100644 --- a/test/blackjack_score_test.rb +++ b/test/blackjack_score_test.rb @@ -3,6 +3,8 @@ require 'minitest/autorun' require 'minitest/reporters' require 'minitest/pride' +# require 'simplecov' +# SimpleCov.start require_relative '../lib/blackjack_score' @@ -18,26 +20,45 @@ score = blackjack_score(hand) # Assert <- You do this part! + expect(score).must_equal 7 end it 'facecards have values calculated correctly' do + hand = ['Jack', 3, 5] + score = blackjack_score(hand) + expect(score).must_equal 18 + end it 'calculates aces as 11 where it does not go over 21' do - + hand = ['Ace', 7] + score = blackjack_score(hand) + expect(score).must_equal 18 end it 'calculates aces as 1, if an 11 would cause the score to go over 21' do + hand = ['Ace', 5, 9] + score = blackjack_score(hand) + expect(score).must_equal 15 end it 'raises an ArgumentError for invalid cards' do + hand = ['poker', 2] + expect { + blackjack_score(hand) + }.must_raise ArgumentError end it 'raises an ArgumentError for scores over 21' do + hand = [10, 10, 3] + + expect { + blackjack_score(hand) + }.must_raise ArgumentError end end