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 create an object to represent one of the foundation piles!
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
Our model now has a deck and a waste pile.
0:00
But it still needs foundation piles and
tableau piles.
0:03
Let's keep on modeling by creating a class
to help represent a foundation pile.
0:06
Just like with the waste pile,
we should start by analyzing
0:11
what requirements we have for
a foundation pile.
0:14
The first requirement is
that each foundation pile
0:17
needs to correspond to a specific suit.
0:20
They also require that their cards
follow a nice orderly progression
0:23
from As to king, zero to 12.
0:27
Along with those requirements,
0:29
we also need to be aware of which
actions can happen to a foundation file.
0:31
Just like with our waste file,
0:36
there's only three things that
can happen to a foundation file.
0:38
We can add a card, we can remove a card,
and we can reset to an empty pile.
0:41
Now that we know what it takes
to make a foundation pile,
0:47
let's make a foundation pile.
0:50
Start by creating a new
class named FoundationPile.
0:52
Then, since each foundation pile needs
to correspond to a specific suit,
1:01
let's add a constructor.
1:05
And inside, let's add a suit parameter,
which will be a String.
1:08
And then, let's upgrade our suit
parameter into an immutable property
1:13
by adding the val keyword.
1:17
Next, let's create a property
to represent the cards
1:20
contained in this foundation pile.
1:23
Val cards, which is going to
be a MutableList of Cards.
1:26
And let's set it equal to an empty list.
1:34
Now, all that's left are the actions.
1:37
We need to be able to add a card,
remove a card and reset our pile to empty.
1:40
Let's start with resetting our pile.
1:45
Let's create a new function named reset,
and then inside this function,
1:47
let's type cards.clear to remove
all the items from our cards list.
1:52
Next, let's handle removing a card.
1:59
Let's create a new function name to
removeCard which takes in a card,
2:02
And then removes that
card from the cards list.
2:10
So cards.remove and
then pass in the card we'd like to remove.
2:13
Finally let's finish up our foundation
class by adding our add card function.
2:20
This function will take in a card and if
it's a match it will add it to the cards
2:25
property and return true to tell
our model that we found a match.
2:30
If it's not a match, it will return
false and our model will be sad.
2:35
Let's declare this function by typing fun,
addCard.
2:39
Adding a card object for
the parameter and returning a Boolean.
2:44
Nice.
2:53
Now we just need to figure out if
the card parameter is a match for
2:54
the next card in our foundation.
2:58
If our foundation is empty,
the next card would be the ace.
3:00
Which corresponds to a value of zero.
3:04
Inside the addCard function, let's declare
a mutable property named, nextValue.
3:06
And let's set it equal to 0,
which is the value of the ace.
3:15
Then on the next line, let's add an if
statement to check if there are already
3:19
any cards in our foundation.
3:24
If cards.size > 0.
3:25
And if there are,
let's update our next value property to be
3:33
equal to the value of the last
card in our foundation plus one.
3:37
So nextValue = cards.last.value + 1.
3:41
Cool.
3:52
Next, we need to check if
the suit of our card parameter
3:53
matches the foundation suit.
3:56
We also need to check that
the value of our card parameter
3:58
matches with our next value property.
4:02
Let's add a line below
the if statement and
4:04
then type if card.suit is equal to
4:07
our super parameter or suit property and
4:12
the cards value Is equal
to our nextValue property.
4:19
And if that's true then we've got a match,
and
4:26
we want to add this card
to our card's property.
4:29
Cards.add and pass in our card.
4:32
And then return true to let our
model know that we found a match.
4:36
Finally, let's add return false at the end
to finish off our addCard function.
4:41
Which also finishes our foundation class.
4:48
Awesome.
4:50
Now let's head back to our GameModel and
create a property for
4:51
our foundation piles.
4:55
Let's add a line below our waste pile,
and type val foundationPiles,
4:57
which will be an Array of FoundationPiles.
5:05
And let's set it equal to arrayOf.
5:12
And for the parameters, let's pass in
one foundation pile for each suit.
5:15
So FoundationPile of Clubs.
5:19
Then a FoundationPile of Diamonds.
5:24
And I'm going to put these on a new line,
so it's easier to see.
5:29
And a FoundationPile of Hearts and
then a FoundationPile of Spades.
5:35
And remember since this is
clearly going to give us an array
5:46
of foundation piles we can
get rid of the data type.
5:49
Nice.
5:54
Now that we're finished with
our foundation pile object.
5:55
We need to create an object
to represent a tableau pile.
5:58
But before we get the tableau piles,
6:01
let's see if we can't find a way to stop
repeating ourselves with these card suits.
6:03
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