What's next when you're building out a game, you've got to ask yourself, what do you want to happen
next within the gameplay?
So we come in, we have select number of players.
We can select a number of players at this point and start the game.
So we hit the start button and we are represented with all the different players.
We've built a deck of cards and we need to also deal the deck of cards now that we have a bunch of players
set up.
So let's create a function in order to deal cards.
And I also want to pass in a value in here.
So this is going to be the first player that's going to get the card.
And the first player that we want to retrieve the card is going to be player one.
But within that array, they're going to be the first character there.
So that's going to have an index value of zero.
So that's why that one is passing in zero.
Let's take deal cards and take our starting player card.
So that's the card that they're on.
We need to pass in that zero value.
And what we're going to do is we're going to keep going and dealing into the deal.
Cards function until we run out of cards.
And then once we run out of cards, then that means that the deck of cards has been dealt.
So let's take our player card value and we're going to pass in a territory operator here checking to
see if the value of player card is greater than or equal to the player's length.
And remember, players was that array that we created in the last one.
So we've got a length for the players of three.
So we're checking to see if it's larger than three.
And if it is, then that means that whatever the value of player card is, the one that we're going
to reset it back to zero.
That's our starting card once again, that's our starting player so that we go back to, again, that
first player with the index.
And if it is larger, so if it is larger than we're going back to zero, otherwise it's going to be
the value of player card and we can also console log that out.
So it's a little bit easier to follow and we're going to log that out as we deal the cards.
So we're going to continuously loop through here and we're removing cards from the deck.
So as long as the deck has a length greater than zero, then we know that we've got more cards to deal.
And if it doesn't have a value of greater than zero, so that means that all of the cards have been
dealt.
We can take our message element and update the text content to say cards dealt.
And that means that all of the cards are dealt.
And we can also add in a return here to return that function so that we're no longer iterating through.
So next, what we need to do is we need to loop through here so this isn't going to do a whole lot quite
yet.
So we see that we've got all the player stuff that we did last time.
And as we're looping through, we're outputting that player card value.
So we've got player card and there's not a whole lot of information there that we have available to
us because we're only dealing the cards once and that's just passing in.
That same zero sets are starting zero.
So now is where things get interesting, where what we want to do is we want to get a random index value
from the deck of cards so that we're not taking always the top card.
We're actually going to be the way we're dealing.
This is we're actually selecting one of the elements randomly using math, random and then math flaw
from our deck of cards.
And then we're going to remove that.
And that's where we just multiply it by the deck length.
And that's going to give us a random index value from the deck of cards.
So let's create the card and the card is going to have a value.
So using the deck of cards, we're going to splice a value so changes the content of array, add new
elements while removing old ones.
Well, we don't want to add anything new, but we do want to remove an existing one.
So we're existing one is available at random index and we only want to remove one card.
And because this is going to be an array that's going to be returned back, we only want the first item
that's available in the.
So now if we do a console log of card, you're going to see that just after that zero, we have a card
that was randomly picked.
So the first card that we're dealing is the queen of diamonds.
It's value is eleven.
So this came when we built the card.
So that's all the card information.
So next thing that we want to do is take our DLS array and depending on what player card we're on.
And remember, this is that array that we constructed in the last one.
We're setting up the players and we added in within DLs, just empty empty arrays.
So that built out the A structure.
So now we can use the player.
Hard to get the index of which array we want to add this in, so we're pushing the card into the array,
basically.
And if we output deals, so now we don't have to output card.
And if we output deals, you can see that within deals we've got our first player has a first card in
their array of cards that they have.
So next we need to implement the player card value and do this all over again until we run out of cards.
So we're going to return a function here.
And the function we're returning is deal cards and we're returning the value of player card because
we incremented it.
So this will continuously loop through while the deck length is greater than zero.
And if it's not greater than zero, then the cards are all dealt out.
So let's see how this plays out.
So let's do a quick refresh and a start.
We get cards dealt now and as you can see, deals array, we have all of the cards have been dealt out
randomly.
So we see that we've got a total of all of the cards dealt out.
Into the iRace, so we've got 18 plus 17 plus 17, so that's a total of 52 cards, all Datto randomly.
So every time we deal the cards, it's going to be a random sampling of the cards for each player.
And this the nice thing about it here, this is fully dynamic.
So if we have four players and then we look at the deals array, you can see that it's dealt it out
to all four players.
So each one in this case got 13 cards and these are all the cards at random.
So this is how you can select a number of players, deal out card cards to them and build a deck and
deal out those cards to them and have this all dynamically available.
So it doesn't matter how many players you have, it will always deal at the cards nice and neatly.
So next, what we want is more visuals.
We want to be able to see the cards that are dealt out and the card that's on top so that we can play
through the rounds and then also determine who's going to win.
So that's going to come up and we're going to set that up.
Now that we've got the play started, the next one is going to be attaching an event listener to the
attack button.
And the tag button is the one that we're going to deal the next round to.
And this is going to specify how many rounds of play we're going to have and how many interactions we're
going to have.
So this input is going to change from being how many players to how many rounds we're using to attack.
So that's all still to come.
So go ahead and set the ability to deal the cards up in your project.