This course will be retired on July 14, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Kotlin for Java Developers!
You have completed Kotlin for Java Developers!
Preview
In this video we'll see how we can print out some data about our game!
Cards Map
mapOf(0 to "A", 1 to "2", 2 to "3" , 3 to "4", 4 to "5", 5 to "6", 6 to "7", 7 to "8", 8 to "9", 9 to "10", 10 to "J", 11 to "Q", 12 to "K")
Kotlin Links
Project Files
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
[MUSIC]
0:00
[SOUND] We're all setup and
ready to move on to implementing the UI.
0:04
The only downside is that we don't
know even know if this works or not.
0:08
We should probably test it, but
before we get to writing a test for
0:12
the whole game, let's start with just
being able to print out the game state.
0:16
Over in our GameModel class,
0:21
let's add a function at the very
bottom named debugPrint().
0:23
This function will print out important
information about our GameModel so
0:29
we can see what's going
on behind the scenes.
0:34
Inside this function let's start by simply
printing out the top card in the deck.
0:37
s-o-u-t, tab, for println.
0:42
And then deck.cardsInDeck.last().
0:44
Then let's head over to our App.kt
file and set up our GameModel.
0:51
Remember, since our GameModel is
a singleton, we can just access it.
0:56
So let's start by resetting the game.
1:01
GameModel.resetGame(), and
then let's see what the top card is.
1:04
GameModel.debugPrint().
1:09
Finally, let's run the app and
see what happens.
1:12
And it looks like the top card
in my deck is the four of clubs.
1:18
Cool, and notice that since our
card class is a data class,
1:23
we've got a better two string
function than we normally would.
1:27
But you know what?
1:31
We can do a lot better than this for
our toString function.
1:32
Let's head over to our Card class and
see how.
1:36
First, let's use control+o to
bring up the override window.
1:42
And then let's override
the toString function.
1:46
And let's make it return
an empty string for now.
1:50
Next, we need to add our
card's value to our string.
1:55
A great way to do this in
is with string templates.
1:58
String templates are basically just a way
to add code and inside our strings.
2:02
Inside this empty string, let's add
our card's value by typing $value.
2:08
Now, when this code runs,
it will print out the value of our card.
2:16
So let's add a space and
then add our card suit.
2:20
$suit, and then do the same thing for
the face up property, $faceUp.
2:23
Then let's run our app again.
2:30
And now my top card is the seven of clubs.
2:33
All right,
now let's take this one step further.
2:37
I'm tired of looking at card values when I
really just want to know what card it is.
2:40
To do this, let's make a map
mapping all the card values
2:46
to the cards those values represent.
2:51
And let's make it as
a package scope variable
2:53
just in case we want to
use it from another class.
2:56
Right below our blackSuits variable,
2:59
let's create a new immutable
variable named cardsMap.
3:02
And let's set it equal to
what's on the teacher's notes.
3:06
It's kind of a lot of typing.
3:11
Now, instead of passing in value,
let's pass in cardsMap.get(value).
3:14
Next, to make it work,
we just need to add brackets around it.
3:24
We can't leave them off this time.
3:28
Then, telling us that we can replace
the get call with the indexing operator.
3:34
So let's hit ALT+Enter, and looks good.
3:40
Now, let's run the app and we should
start to see some of the royal cards.
3:46
But it looks like I got a ten of diamonds.
3:53
So let's try again, and
there we go, the ace of spades.
3:55
In the next video, we'll see what
we can do about our suit parameter.
3:59
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up