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 check the suits of our cards, and then we'll finish up the 'addCards' function!

0:00
Getting back to our suitCheck function.

0:02
Let's use our new properties to help us determine if the two cards

0:06
are different colors, which can only happen in two ways.

0:09
Either card one is red, and card two is black, or card one is black, and

0:14
card two is red.

0:15
So let's type if and then add our brackets.

0:20
Then inside the if statement, let's add parentheses for

0:24
our first case then add an or and add parentheses for our second case.

0:30
Then let's jump back to our first case.

0:33
And check if c1 is red by seeing if its suit is

0:36
contained by our redSuits property.

0:39
So redSuits.contains c1.suit.

0:44
Then let's add an and and check if c2 is black.

0:51
blackSuits.contains c2.suit.

0:56
Now we just need to handle the second case where card one is black and

1:00
card two is red, and I'll put this on a new line so it's easier to see.

1:04
So inside our second case parentheses,

1:09
let's type blackSuits.contains c1.suit and

1:15
redSuits.contains c2.suit.

1:19
And there we go.

1:20
Our suitCheck function is basically finished.

1:23
We just need to add return true inside our if statement, and

1:27
then add return false at the bottom of the function.

1:31
Jumping back to our addCards function, if this if statement is true,

1:36
then we want to add the new cards to our current cards and return true.

1:41
So, inside this if statement, let's update our tableau

1:46
pile by typing cards.addAll and passing in our new cards list.

1:52
Then let's return true to let our model know that it was a success.

1:57
Finally, let's return false at the end of this function.

2:02
And we should be done, right?

2:03
Not quite.

2:05
We still need to handle the case where our tableauPile is empty.

2:09
Remember, an empty tableauPile will only accept a king as the first card.

2:14
Also, as we have it now,

2:15
an empty tableauPile will give us an error when we try to use cards.last.

2:20
There's no last card in an empty pile.

2:23
To fix this, let's only evaluate our if statement if our Cards property

2:28
actually has cards in it.

2:29
At the top of the function, let's type if cards is

2:34
greater than 0, add the left bracket and

2:38
then let's add the right bracket, down here.

2:45
Next let's add an else if to handle if a king is being played.

2:49
Else if (newCards.first().value

2:55
== 12, which is the value of a king.

3:02
And finally, inside the else if, let's just copy and

3:05
paste these two lines from up here.

3:15
And there we go.

3:16
Now when we run this function, if there are cards in the tableau pile and

3:22
the first new card meets these criteria,

3:25
then we'll add the new cards and return true.

3:29
On the other hand, if this tableau pile is empty but

3:33
the first a new card is a king, then we'll also add the new cards and return true.

3:41
Otherwise, whatever cards we're trying to add aren't a match.

3:45
So we'll just return false.

3:47
We're finally done with the addCards function and

3:50
all that remains is figuring out how to remove cards from our tableau pile.

3:54
In the next video, we'll see how we can do that, and

3:57
along the way, we'll learn all about for loops in Kotlin.
You need to sign up for Treehouse in order to download course files.
Sign up