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 take a look at the big picture and see how we can start modelling a game of solitaire!
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
[MUSIC]
0:00
Great job putting
together that deck class.
0:04
I know it's a lot to throw at you,
but you're doing fantastic.
0:07
We've created a fully functional deck
that we can shuffle and even draw from.
0:10
But before we can use our deck,
0:15
we'll need to create a few other objects
to help us model a game of solitaire.
0:17
So let's take a minute to talk
about what objects we need and
0:22
how all the pieces will fit together.
0:25
We've already modeled a card class and
the debt.
0:28
So, looking at the layout of solitaire,
it looks like we still need
0:31
to make objects for the waste piles, the
foundation piles, and the tableau piles.
0:34
We're also going to need an object
to help us manage the game itself.
0:40
This object will be responsible for
setting up the game and
0:43
handling any actions we take,
kind of like a dealer at a casino.
0:46
Put another way,
this object is responsible for
0:50
modeling our game of solitaire.
0:54
It will make up the model
layer of our app.
0:56
Along with the model layer, we'll also
have a view layer, and a presenter layer.
0:59
The view layer will just be a simple
interface with one function
1:04
to update the view.
1:08
On the other hand, the presenter
layer will act as a middleman
1:09
between our model and view layers.
1:13
Whenever we take an action on our view,
1:16
like clicking on a card in the tableau
we'll call a method on our presenter.
1:18
This method will then update our model and
1:23
once that's done, it'll update our view
to reflect any changes made to the model.
1:26
All right, now that we've got that
under control, let's zoom back out and
1:30
take a look at the big picture.
1:35
We started with our card class
which we used to create our deck.
1:37
Our deck will then be used by our
model along with our waste pile,
1:41
foundation piles and tableau piles to
correctly model a game of solitaire.
1:45
The model then goes through our
middle man, the presenter and
1:50
connects to the view.
1:53
And that's pretty much
entirely how the app works.
1:54
It's gonna take a bit to
get through all this so
1:57
don't be afraid if it doesn't
100% make sense just yet but
2:00
for now we're gonna keep on trucking
by creating a class for a model.
2:04
So let's create a new colon
class named game model.
2:08
Then and sat our model we need to create
properties for each of our game objects.
2:18
Let's start with the deck by
typing val deck equals deck.
2:23
Up next is the waste pile.
2:30
But before we start creating
a waste pile object.
2:33
Let's take a second to think
about how we use the waste pile.
2:35
In solitaire, there's three things
that can happen to our waste pile.
2:39
First off, when we tap the deck,
we add a card to the waste pile.
2:43
Secondly, when we tap the waste pile,
assuming there's a valid play available
2:47
we remove a card from the waste pile and
play it.
2:52
Lastly, when we're all out of options,
2:55
we can tap the waste pile to
shuffle it back into the deck.
2:58
So our three waste pile actions
are add a card, remove a card, and
3:01
remove all the cards.
3:06
Lucky for us, this functionality already
exists in the form of the list object.
3:08
So instead of creating
a whole new class just for
3:12
our waste pile let's just
make it a mutable list.
3:15
val wastePile which is gonna
3:18
be a MutableList of cards.
3:22
And since the wastePile starts empty let's
set it equal to a MutableList of nothing.
3:26
MutableList of and leave it empty.
3:33
Nice, now that we've conquered
the wastePile next step
3:36
is creating a property to
hold our foundation piles.
3:40
But before we can do that we'll need
to create our foundation pile object
3:43
which we'll do in the next video.
3:47
But before we get to the game, I need to
tell you about an awesome resource for
3:50
learning Kotlin on and
that is the Kotlin cones.
3:53
Basically there's 42 code challenges
provided by the good people at
3:57
to help you learn Kotlin.
4:01
So while there aren't any code challenges
in this course, if you're looking for
4:03
extra practice,
look no further than the Kotlin cones.
4:06
I provided a link in the teacher's
notes of every video.
4:10
So even if you don't need
any extra practice yet,
4:13
if you want some extra practice
later you'll know where to find it.
4:16
With that said,
let's move on to programming the game.
4:20
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