Conditionals are used to check for certain conditions and/or criteria. The most basic way of performing a conditional operation is using a single if statement.
The parseCard function should take the card string (e.g. ace) and turn it into its value (e.g. 11).
- Use a big
switchstatement on thecardvariable. - King, Queen, Jack and 10 can be handled with a single case.
- The switch can have a
defaultcase. In any case the function should return0for unknown cards.
isBlackJack checks if 2 cards have the combined value of 21.
- Should use the
parseCardfunction to get the value for each card. - Should sum up the values of the 2 cards.
- Should return
trueif the sum is equal to21. - No additional statement is needed here. The result for the comparison can be returned.
As the largeHand function is only called for hands with a value larger than 20, there are only 2 different possible hands: A BlackJack with a total value of 21 and 2 Aces with a total value of 22.
- The function should check if
isBlackJackistrueand return "P" otherwise. - If
isBlackJackistrue, the dealerScore needs to be checked for being lower than 10. If it is lower, return "W" otherwise "S".
The smallHand function is only called if there are no Aces on the hand (handScore is less than 21).
- Implement every condition using logical operators if necessary:
- If your cards sum up to 17 or higher you should always stand.
- If your cards sum up to 11 or lower you should always hit.
- If your cards sum up to a value within the range [12, 16] you should always stand if the dealer has a 6 or lower.
- If your cards sum up to a value within the range [12, 16] you should always hit if the dealer has a 7 or higher.
- (optional) Try to optimize the conditions:
- Pull together the conditions for stand into one.
- Pull together the conditions for hit into one.
- Remove redundant parts of the conditions (e.g.
A || !A && Bcan beA || B).