if and else inside of a function
Since if expressions result in values, they can be the result of functions, which also result in values.
In this exercise, you'll create the body a function for your Twenty-One program called pointsToBust, which takes the current hand's point value as an input and calculates the the number of points remaining until 21. As a player, knowing how many additional points it takes to cause your current hand to bust will help you decide whether to hit or stay. The card variables you need and the bust function are already defined for you.
Deze oefening maakt deel uit van de cursus
Introduction to Scala
Oefeninstructies
- Write an
ifcondition: if the hand busts, make theInt0 the result of the function. - Write an
elsecondition: if the hand does not bust, subtracthandfrom 21 and make that difference the result of the function. - Call
pointsToBustwith a hand with the cardstenSpadesandfiveClubsas the argument.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
// Find the number of points that will cause a bust
def pointsToBust(hand: Int): Int = {
// If the hand is a bust, 0 points remain
___ ___
___
// Otherwise, calculate the difference between 21 and the current hand
___
___
}
// Test pointsToBust with 10♠ and 5♣
val myHandPointsToBust = ___
println(myHandPointsToBust)