Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this video we'll see how we can print out the suits just like any other character!
Unicode Suits
diamonds -> "\u2666"
clubs -> "\u2663"
hearts -> "\u2665"
spades -> "\u2660"
Related Links
Project Files
-
0:00
All right on to the suparameter.
-
0:03
Did you know that each of the suits is included in Unicode?
-
0:07
Meaning that each suit is a character that we can type out.
-
0:11
Let's start by replacing our suit and
-
0:13
our print string with a dollar sign and then brackets.
-
0:18
Inside the brackets, let's call a function that doesn't exist yet
-
0:22
named getSuitChar and pass in the suit.
-
0:28
Then let's use Alt+Enter to create the function.
-
0:36
And after it's created, let's delete the brackets and
-
0:40
then set this function equal to when suit and then add brackets.
-
0:47
And inside our when statement,
-
0:49
let's start by checking if the suit is equal to diamonds.
-
0:53
And if it is, Then let's return the Unicode
-
1:00
character for diamonds, which is \u2666.
-
1:06
Next hit command or Ctrl+D three times to duplicate this line.
-
1:14
And then let's change these extra
-
1:19
diamonds to clubs, hearts and spades.
-
1:24
And let's change the Unicode characters to u2663 for
-
1:30
clubs, u2665 for hearts and u2660 for
-
1:35
spades, these are all in the teacher's notes as well.
-
1:42
Finally, since we're counting on this when statement to always return something
-
1:47
let's add an else.
-
1:49
And if we get anything else,
-
1:52
let's return a string that says incorrect suit then let's run the app.
-
2:00
And sweet, I got the ten of spades.
-
2:02
My favorite.
-
2:03
Last but not least, let's deal with the face up property.
-
2:07
If a card is face up, we should be able to see it.
-
2:10
But if it's face down, we shouldn't be able to tell what card it is.
-
2:14
So really, I shouldn't be able to tell that this card is a ten of spades.
-
2:20
To fix this, let's first take the face up property out of our print string.
-
2:29
Then let's add a line above the return and type if faceUp.
-
2:37
And then put our return statement inside the brackets.
-
2:41
Next let's add an else and if it's not face up, let's return three Xs.
-
2:51
Great, but before we move on, let's make this two string function a lot smaller.
-
2:56
To do that, I first need to tell you about the ternary operator and column.
-
3:01
Remember the Ternary Operator?
-
3:03
It's these guys.
-
3:04
They let us do a conditional statement on only one line.
-
3:07
See, the thing is, Kotlin doesn't have a Ternary Operator.
-
3:11
Instead, with Kotlin, an if statement returns a value.
-
3:16
So instead of using some weird syntax, we can just use if and
-
3:20
else like we're used to.
-
3:22
So to get this down to one line,
-
3:24
let's first delete the outside brackets and then add an equal sign.
-
3:30
Then, since if statements already return values, let's delete the return keywords.
-
3:41
And finally, let's get rid of the brackets and put everything on one line.
-
3:59
Awesome, our two string function is ready for action.
-
4:03
Now let's give it some exercise by printing out the entire game board instead
-
4:07
of just the top card in our deck.
You need to sign up for Treehouse in order to download course files.
Sign up