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
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
In this video we'll finish resetting the game by shuffling the deck and dealing out each of the tableau piles!
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Sourabh Pal
210 Points2 Answers
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Getting back to our Reset game function,
we still need to shuffle the deck and
0:00
deal out each of the tableau files.
0:04
Shuffling the deck is easy.
0:07
Remember, we already created
a function for it in our deck class.
0:08
So let's add a line below where
we reset our foundation piles and
0:13
then type deck.reset to reset our deck
to have all 52 cards and shuffle it.
0:17
The last thing we need to do is set
up each of those tableau piles.
0:23
To do this, we need to loop
through each of the piles and
0:27
make sure that each pile has
the right number of cards.
0:30
Specifically, the first pile should have
one card, the second pile should have two,
0:33
and so on, until we get to the seventh
pile which should have seven.
0:38
An easy way for us to do this is to make
use of another extension function on
0:42
the array class called forEachIndexed.
0:47
Let's add a couple of lines below
where we reset our deck and
0:50
then type TableauPiles.forEachIndexed.
0:54
ForEachIndexed still loops through
the entire array just like forEach does.
0:58
But with forEachIndexed we also get
access to which index we're evaluating.
1:03
So since the action for
forEachIndexed takes in both the index and
1:09
the item, we can't get away
with using the it keyword.
1:14
With the forEach function,
the action only requires one parameter.
1:19
So we're allowed to omit it and
column will declare it for us as it.
1:24
But with forEachIndexed, the action
requires both the index and the item.
1:29
So we're not allowed to omit
our parameters this time.
1:34
All right,
let's add a line after the arrow.
1:37
And then let's create a variable to
represent the cards that need to go
1:41
in this tableau pile.
1:45
Let's call it cardsInPile.
1:46
So val cardsInPile, and
1:49
since we initialize a tableau
with a mutable list of cards,
1:52
let's give this variable a type
of MutableList of cards.
1:57
Then let's set this equal to a new array,
and
2:03
before we get to the array let's go
ahead and convert it to MutableList.
2:06
Perfect, getting back to the array,
for the size let's pass in i + 1,
2:11
which will end up being
the numbers 1 through 7.
2:17
Just what we need.
2:21
Next, for the second parameter to
our array, the initializer function,
2:22
let's add brackets to make
a lambda expression and then,
2:27
inside our lambda expression, we just
need to return the next card in the deck.
2:31
So let's type deck.drawCard,
and there we go.
2:36
Now let's add a line, And
2:42
then let's set this tableauPile equal to
a new tableauPile with those cards in it.
2:46
Let's type tableauPiles, add index i, and
2:52
set it equal to a new TableauPile
with those cards in it.
2:57
Nice, now, for each TableauPile,
we'll draw a card i + 1 times,
3:03
and then use those cards to replace
the old TableauPile pile with the new one.
3:09
And once we're done,
we'll be ready to start playing solitaire.
3:15
In the next video, we'll start adding
some of the functionality to let us
3:18
actually play a game of solitaire.
3:22
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