Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage
30 changes: 30 additions & 0 deletions lib/blackjack_score.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

23 changes: 22 additions & 1 deletion test/blackjack_score_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/pride'
# require 'simplecov'
# SimpleCov.start

require_relative '../lib/blackjack_score'

Expand All @@ -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