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
Preview
Start a free Courses trial
to watch this video
In this video we'll start creating an object to represent a tableau pile!
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
We're almost done with all the objects
required to model our game.
0:00
Now we just need to create one more
class to represent a TableauPile.
0:03
Let's start by declaring a new
class named TableauPile.
0:08
Then lets add a constructor and
inside the constructor,
0:15
let's add var_cards and
make it a MutableList of type Card.
0:21
Unlike a foundation, a TableauPile
contains cards right from the get go.
0:28
So instead of declaring
our cards property and
0:33
the class body,
we'll declare it in the constructor.
0:36
This way our cards property will have all
the right cards right from the start.
0:40
But we're not done with
the constructor just yet.
0:45
When we created each of our card objects,
we left out the argument for
0:48
the face up parameter, which means
each card is currently face down.
0:52
But to properly initialize a TableauPile,
the last card needs to be face up.
0:57
To do this,
let's add an init block and inside,
1:01
let's get the last card on our list,
cards.last and
1:06
set its faceUp property to true.
1:10
.FaceUp = true.
1:14
Nice, now that we've handled
initializing our TableauPile,
1:17
let's take a look at the actions
involving the Tableau.
1:21
First, we can move one card from
the waste pile to a TableauPile.
1:24
And we can also move one card from
foundation down to a Tableau pile or
1:28
even just between two tableau piles.
1:33
In addition to adding and
removing one card, we can also add and
1:36
remove multiple cards
between tableau piles.
1:40
When we want to reset the game,
rather than try and
1:43
reuse our tableau piles,
we'll just create new ones.
1:46
So unlike our waste file and
our foundation files,
1:50
we won't have a reset method here.
1:53
All right,
to handle each of these actions,
1:55
we'll need to create two functions.
1:57
One function for adding cards and
one for removing.
2:00
Let's start with adding by creating
a function named addCards.
2:03
It will be just like our
foundation's addCard function
2:09
except this time we'll be able
to add multiple cards at once.
2:12
So let's add newCards as our parameter and
make it a MutableList of type card.
2:16
And then let's return a boolean for
whether or
2:24
not we're able to add the cards.
2:26
Inside this function, we need to determine
if the new cards are a match for
2:29
this TableauPile.
2:33
To do that, we need to make a couple of
comparisons between the first new card and
2:35
the last current card.
2:40
Specifically, the first
card in our newCards list,
2:42
needs to have a value of one less than
the last card and our cards property.
2:46
Also, the first new card and the last
current card need to be different colors.
2:50
So inside this function,
let's start with an if statement and
2:57
then let's check that the value
of the first new card.
3:02
NewCards.first().value is equal to
3:06
the value of the last
current card minusone1,
3:11
equals cards.last().value- 1.
3:16
Then let's add an and and make sure
the suits are different colors as well.
3:21
And since checking the colors is a little
more work than we should be doing here,
3:27
let's create a function
to do that work for us.
3:31
Let's type suitCheck and
pass in the two cards we'd like to check.
3:34
New,Cards.first and cards.last,
3:41
then let's finish off our
if statement with brackets.
3:45
And use Alt + Enter to create
the suitCheck function.
3:51
And since this function is just
going to check if the two cards
3:59
are different colors, first and
last aren't good variable names.
4:02
Instead let's just use c1 and c2.
4:07
And I'm gonna put this if
statement on two lines
4:10
just to make it a little easier to see.
4:15
Inside our suitCheck function,
4:19
we need to make sure that one of these
cards is red and the other card is black.
4:21
But before we dive in, wouldn't it be nice
to have a property telling us which suits
4:25
are red and which suits are black?
4:30
Let's head over to our Card class and
make it so.
4:32
Let's create a new package level property
below our suit properties named redSuits.
4:36
And let's set it equal to
arrayOf diamonds and hearts.
4:44
Then let's do the same thing for
the black suits.
4:51
Val blackSuits equals array of clubs and
spades.
4:54
And now it's incredibly easy for
us to check if a card is red or black.
5:02
If we want to check if a card is red,
5:07
we can just check that the card's suit
is contained in the redSuits property.
5:09
In the next video, we'll check back
in with our suitCheck function and
5:14
then finish up our tableau pile.
5:18
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