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 start with a quick overview of solitaire, and then we'll create the project!
Related Links
Kotlin Koans are essentially Code Challenges provided by IntelliJ to help us learn Kotlin. If you're ever looking for more practice definitely check out the Koans!
Project Files
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
Before we get started with building the app, 0:00
let's make sure we're all on the same page about what Solitaire is and how it works. 0:02
Solitaire, also known as Patience or Klondike, 0:08
is a card game that you can play all by yourself. 0:11
Here's a quick overview of how we'll be playing. 0:14
We start by creating the tableau, which contains seven piles of cards. 0:17
The last card in each pile is placed face up, and 0:21
each pile has one more card than the last. 0:25
Once we've created the tableau, 0:28
the remaining cards are placed up here to become stock. 0:30
And when we want to draw a card, 0:33
we just flip it over into what's known as the waste pile. 0:35
But don't worry about throwing cards into the waste pile. 0:38
If we ever run out of cards in our stockpile, 0:41
we'll just recycle the waste to give us a new stock. 0:44
The goal of Solitaire is to move every card in the deck 0:47
up to these four foundations. 0:50
The catch is that each foundation corresponds to only one suit, and 0:53
you need to build up each foundation from the bottom starting with the ace. 0:57
There's also a catch when dealing with the tableau piles. 1:02
If we want to add a card to the tableau, its value needs to be 1:05
one lower than the card above it, and it also needs to be the opposite color. 1:09
So if we have a nine of hearts as the last card and one of our tableau piles, and 1:13
we just turned over the eight of clubs into the waste, 1:18
we can play that eight of clubs right below our nine of hearts. 1:21
Also, if a tableau pile is empty, only a king is allowed to fill the void. 1:25
All right, if you're still not quite sure what's going on, 1:30
there's a link in the teacher's notes below to an online Solitaire game 1:34
that works just like ours will. 1:37
Now that we know how to play the game, let's get down to business. 1:40
For this course, we're only going to deal with the game logic. 1:44
That is, we won't be adding a user interface. 1:47
This way, we can use the same code for an Android app and a JavaFX app. 1:50
The only thing that'll be different is the UI code. 1:56
Also, since we won't be adding a UI, rather than use Android Studio, 1:59
we'll be doing all of our programming in IntelliJ IDEA. 2:04
So let's open up IntelliJ and get started. 2:07
The first thing we need to do is create the project, so 2:10
let's click Create New Project. 2:13
Then, on the left, pick Kotlin for the JVM, and hit Next. 2:15
Then let's name the project, Solitaire. 2:22
And hit Finish. 2:27
Hooray, you're your first Kotlin app. 2:30
Now, let's drill down to the source folder. 2:33
And, bummer, it doesn't look like it gave us a main class. 2:36
But, no worries, we can just make our own. 2:39
Let's right-click on the Source folder, and choose New>Kotlin File/Class. 2:42
Then let's name it App, and hit OK to give us our first Kotlin file App.kt. 2:48
Next, we need to provide an entry point end to our Kotlin application. 2:55
Over in App.kt, let's type main, and then hit tab to generate the entry point. 2:58
And there we go. 3:05
Now, let's quickly run this to make sure everything's working. 3:06
But before we can run anything, we'll need to create a run debug configuration. 3:09
Luckily, IntelliJ makes this really easy for us. 3:14
Just click the Kotlin icon in the gutter next to our main function, and pick Run. 3:18
Then, IntelliJ will create a configuration for us and run the app. 3:25
Nice. 3:30
Now that everything's up and running, let's talk about that main function. 3:31
The first thing to note is the fun keyword. 3:34
All functions in Kotlin are declared with the fun keyword. 3:37
Then comes the name of the function, main. 3:41
And then inside the parentheses are any arguments. 3:44
Unlike Java, the name of the argument comes first, 3:47
followed by a colon and then the type, array of strings. 3:51
And if we wanted to return anything, we would add a colon 3:56
after the parentheses, and then specify the return type. 4:00
In Kotlin, instead of using void, we use Unit. 4:05
And just like in Java, we usually omit this for 4:09
functions that don't return anything, so let's undo that. 4:12
And now that we've got our entry point handled, 4:17
in the next video we'll start working on the game. 4:19
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