Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video we'll see how we can check the suits of our cards, and then we'll finish up the 'addCards' function!
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
Getting back to our suitCheck function.
0:00
Let's use our new properties to
help us determine if the two cards
0:02
are different colors,
which can only happen in two ways.
0:06
Either card one is red, and card two
is black, or card one is black, and
0:09
card two is red.
0:14
So let's type if and
then add our brackets.
0:15
Then inside the if statement,
let's add parentheses for
0:20
our first case then add an or and
add parentheses for our second case.
0:24
Then let's jump back to our first case.
0:30
And check if c1 is red
by seeing if its suit is
0:33
contained by our redSuits property.
0:36
So redSuits.contains c1.suit.
0:39
Then let's add an and and
check if c2 is black.
0:44
blackSuits.contains c2.suit.
0:51
Now we just need to handle the second
case where card one is black and
0:56
card two is red, and I'll put this
on a new line so it's easier to see.
1:00
So inside our second case parentheses,
1:04
let's type blackSuits.contains c1.suit and
1:09
redSuits.contains c2.suit.
1:15
And there we go.
1:19
Our suitCheck function
is basically finished.
1:20
We just need to add return true
inside our if statement, and
1:23
then add return false at
the bottom of the function.
1:27
Jumping back to our addCards function,
if this if statement is true,
1:31
then we want to add the new cards to
our current cards and return true.
1:36
So, inside this if statement,
let's update our tableau
1:41
pile by typing cards.addAll and
passing in our new cards list.
1:46
Then let's return true to let our
model know that it was a success.
1:52
Finally, let's return false
at the end of this function.
1:57
And we should be done, right?
2:02
Not quite.
2:03
We still need to handle the case
where our tableauPile is empty.
2:05
Remember, an empty tableauPile will
only accept a king as the first card.
2:09
Also, as we have it now,
2:14
an empty tableauPile will give us
an error when we try to use cards.last.
2:15
There's no last card in an empty pile.
2:20
To fix this, let's only evaluate our
if statement if our Cards property
2:23
actually has cards in it.
2:28
At the top of the function,
let's type if cards is
2:29
greater than 0, add the left bracket and
2:34
then let's add the right bracket,
down here.
2:38
Next let's add an else if to
handle if a king is being played.
2:45
Else if (newCards.first().value
2:49
== 12, which is the value of a king.
2:55
And finally, inside the else if,
let's just copy and
3:02
paste these two lines from up here.
3:05
And there we go.
3:15
Now when we run this function,
if there are cards in the tableau pile and
3:16
the first new card meets these criteria,
3:22
then we'll add the new cards and
return true.
3:25
On the other hand,
if this tableau pile is empty but
3:29
the first a new card is a king, then we'll
also add the new cards and return true.
3:33
Otherwise, whatever cards we're
trying to add aren't a match.
3:41
So we'll just return false.
3:45
We're finally done with
the addCards function and
3:47
all that remains is figuring out how
to remove cards from our tableau pile.
3:50
In the next video,
we'll see how we can do that, and
3:54
along the way, we'll learn all about for
loops in Kotlin.
3:57
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