You've already started watching Building the Foundation
In this video we'll create an object to represent one of the foundation piles!

0:00
Our model now has a deck and a waste pile.

0:03
But it still needs foundation piles and tableau piles.

0:06
Let's keep on modeling by creating a class to help represent a foundation pile.

0:11
Just like with the waste pile, we should start by analyzing

0:14
what requirements we have for a foundation pile.

0:17
The first requirement is that each foundation pile

0:20
needs to correspond to a specific suit.

0:23
They also require that their cards follow a nice orderly progression

0:27
from As to king, zero to 12.

0:29
Along with those requirements,

0:31
we also need to be aware of which actions can happen to a foundation file.

0:36
Just like with our waste file,

0:38
there's only three things that can happen to a foundation file.

0:41
We can add a card, we can remove a card, and we can reset to an empty pile.

0:47
Now that we know what it takes to make a foundation pile,

0:50
let's make a foundation pile.

0:52
Start by creating a new class named FoundationPile.

1:01
Then, since each foundation pile needs to correspond to a specific suit,

1:05
let's add a constructor.

1:08
And inside, let's add a suit parameter, which will be a String.

1:13
And then, let's upgrade our suit parameter into an immutable property

1:17
by adding the val keyword.

1:20
Next, let's create a property to represent the cards

1:23
contained in this foundation pile.

1:26
Val cards, which is going to be a MutableList of Cards.

1:34
And let's set it equal to an empty list.

1:37
Now, all that's left are the actions.

1:40
We need to be able to add a card, remove a card and reset our pile to empty.

1:45
Let's start with resetting our pile.

1:47
Let's create a new function named reset, and then inside this function,

1:52
let's type cards.clear to remove all the items from our cards list.

1:59
Next, let's handle removing a card.

2:02
Let's create a new function name to removeCard which takes in a card,

2:10
And then removes that card from the cards list.

2:13
So cards.remove and then pass in the card we'd like to remove.

2:20
Finally let's finish up our foundation class by adding our add card function.

2:25
This function will take in a card and if it's a match it will add it to the cards

2:30
property and return true to tell our model that we found a match.

2:35
If it's not a match, it will return false and our model will be sad.

2:39
Let's declare this function by typing fun, addCard.

2:44
Adding a card object for the parameter and returning a Boolean.

2:53
Nice.

2:54
Now we just need to figure out if the card parameter is a match for

2:58
the next card in our foundation.

3:00
If our foundation is empty, the next card would be the ace.

3:04
Which corresponds to a value of zero.

3:06
Inside the addCard function, let's declare a mutable property named, nextValue.

3:15
And let's set it equal to 0, which is the value of the ace.

3:19
Then on the next line, let's add an if statement to check if there are already

3:24
any cards in our foundation.

3:25
If cards.size > 0.

3:33
And if there are, let's update our next value property to be

3:37
equal to the value of the last card in our foundation plus one.

3:41
So nextValue = cards.last.value + 1.

3:52
Cool.

3:53
Next, we need to check if the suit of our card parameter

3:56
matches the foundation suit.

3:58
We also need to check that the value of our card parameter

4:02
matches with our next value property.

4:04
Let's add a line below the if statement and

4:07
then type if card.suit is equal to

4:12
our super parameter or suit property and

4:19
the cards value Is equal to our nextValue property.

4:26
And if that's true then we've got a match, and

4:29
we want to add this card to our card's property.

4:32
Cards.add and pass in our card.

4:36
And then return true to let our model know that we found a match.

4:41
Finally, let's add return false at the end to finish off our addCard function.

4:48
Which also finishes our foundation class.

4:50
Awesome.

4:51
Now let's head back to our GameModel and create a property for

4:55
our foundation piles.

4:57
Let's add a line below our waste pile, and type val foundationPiles,

5:05
which will be an Array of FoundationPiles.

5:12
And let's set it equal to arrayOf.

5:15
And for the parameters, let's pass in one foundation pile for each suit.

5:19
So FoundationPile of Clubs.

5:24
Then a FoundationPile of Diamonds.

5:29
And I'm going to put these on a new line, so it's easier to see.

5:35
And a FoundationPile of Hearts and then a FoundationPile of Spades.

5:46
And remember since this is clearly going to give us an array

5:49
of foundation piles we can get rid of the data type.

5:54
Nice.

5:55
Now that we're finished with our foundation pile object.

5:58
We need to create an object to represent a tableau pile.

6:01
But before we get the tableau piles,

6:03
let's see if we can't find a way to stop repeating ourselves with these card suits.
You need to sign up for Treehouse in order to download course files.
Sign up