Welcome back in this lesson, we're going to be creating a deck of cards, so first that we need to
do is set up a few more variables and these are some default variables that we can utilize.
We need a deck.
This is going to be a global variable, which will be the array that we're going to use.
We're also going to set up a few other arrays.
So we've got the ranks of the cards.
And this is where you can list out all the cards that you want.
So want all the way up to nine, 10 and then jox queens.
And these are strings and kings and we want aces to be the largest card.
And then there's the different suits.
So set up another array to hold the suits and we're going to specify the suits as we've got hearts,
diamonds, club and spades.
So using these values, we can loop through the suits and then we can also loop through the ranks and
build a deck of cards.
So that's what we want to do when we start the game.
We've got the start game function.
We've got a global variable to handle and to take all of the deck contents.
So next thing that we want to do when we start the game is we want to build the deck of cards.
So build deck can be the function that will create the deck of cards.
Let's set that function up so we start the game.
We're building our deck and this is going to be a series of loops.
So first that we want to do clear out the existing deck of cards as a global object.
So so we don't have to declare it again and let's use for loops.
So starting with AI is zero and while I is less than and looping through that suits array.
So looping through while I is less than one, we're going to increment it by one.
And so we need another loop inside because we've got another parameter that we're using in order to
build the deck of cards.
So set this one as G and looping through while G is less than ranks length as we've got both of those
arrays that we've got to iterate through in order to build a full deck of cards.
So once we're looping through there, let's create a blank card object.
So this is going to be an object and this can hold all of the information that we need for our card.
So let's set up some values for that object.
So taking in the suit.
So this is going to be coming in as suits and suits is controlled within eye card and we want to get
the rank of the card as well.
So this is held within the ranks array.
So as we iterate through there, when we get to G, we'll output that value.
We've also got a card value, so that can be G plus one.
So setting this up to be whatever the value of J is.
So as we increase in ranks, we can increase the value of the card.
And then lastly, let's take that card and we'll push it into the deck.
So as we loop through both of these loops, by the time we're completed, we're going to have a finished
deck of cards and we can see that when we cancel out whatever the value of deck is.
So let's see what that looks like and refresh.
We hit the start of the game and that builds up our 52 card deck.
And as you can see, we start out with all of the hearts.
So we've got a rank of two and a value of one.
So going up and aces are the largest values.
They have 13.
So they're going to be the highest card.
You might also want to have an ace as a one.
So it all depends on how you want to set up your deck for this case.
What I'm doing is I'm having the ace set up as the highest card, so it's going to be three.
So we get all of the values, all the ranks for the hearts, all the ranks for the values, diamonds,
clubs and spades.
And then we've got all the cards have a value in the order that they appear in the array.
So if you want to switch these around, you're welcome to do that.
But best practices to keep it within that sequential order so that that way you can automatically set
the values as you loop through them.
And it's going to make it a lot easier to compare which card is higher.
So now that we've built a deck, next thing that we do is we need to set up the players and then deal
the cards.
So that's still all to come in the upcoming lessons.
So go ahead and build a deck of cards.
And this is another thing that we're going to do is going to shuffle this deck of cards before we deal
it out as well.