1
00:00:00,120 --> 00:00:04,470
This lesson, we're gonna be constructing a deck of cards, so first of all, let's go over to we've

2
00:00:04,470 --> 00:00:08,970
we've got the variables and we're going to setup the suits for the cards.

3
00:00:09,270 --> 00:00:10,920
So these are the various suits.

4
00:00:11,190 --> 00:00:12,540
And this is all going to be dynamic.

5
00:00:12,570 --> 00:00:19,080
So depending on what information we've got here, we can adjust and the deck of cards will adjust accordingly.

6
00:00:19,350 --> 00:00:25,350
So we're just going to still start out with the typical deck of cards where it's got the four different

7
00:00:25,350 --> 00:00:25,860
suits.

8
00:00:26,160 --> 00:00:29,850
So that's spades, hearts, clubs and diamonds.

9
00:00:30,330 --> 00:00:35,910
And then also we're going to create another three and this one can contain all the ranks.

10
00:00:36,210 --> 00:00:39,030
So these are all the possible ranks that are card might have.

11
00:00:39,330 --> 00:00:41,370
So we know that we've got aces.

12
00:00:41,700 --> 00:00:42,860
There's two stories.

13
00:00:42,920 --> 00:00:46,250
Four, five, six, seven, eight, nine, 10.

14
00:00:47,070 --> 00:00:48,810
And this can also be customized.

15
00:00:48,810 --> 00:00:55,920
So depending on what type of card ranks you want to throw into the mix, you can customize this and

16
00:00:56,250 --> 00:00:59,430
it will customize the card output that you're going to be expecting.

17
00:01:00,360 --> 00:01:05,460
So let's save that and go in to down here at the bottom or iron 80.

18
00:01:05,470 --> 00:01:12,990
So after we've built all of the content and we can also put this into our function so that it's not

19
00:01:13,080 --> 00:01:13,770
as messy.

20
00:01:13,790 --> 00:01:19,410
So how about we do that where we're going to create another function that's going to be just called

21
00:01:19,470 --> 00:01:19,980
build?

22
00:01:20,430 --> 00:01:25,800
And all of this construction that we did in the last few lessons can be contained within there.

23
00:01:26,520 --> 00:01:32,430
So just copy paste it in and then we're going to initiate the building of the game board.

24
00:01:32,910 --> 00:01:35,850
So that's the first thing that we do when we initiate the function.

25
00:01:36,090 --> 00:01:38,430
Just make sure that everything is still working properly.

26
00:01:38,850 --> 00:01:45,300
And we also did remove out the HDD himself from the index file so that we only have that one I.D. with

27
00:01:45,300 --> 00:01:47,600
game and it's constructing the gameboard.

28
00:01:48,270 --> 00:01:53,070
So let's say that we need to do is we need to build the deck of cards, because this is going to serve

29
00:01:53,130 --> 00:01:56,010
as the base for the content of the game.

30
00:01:56,370 --> 00:01:59,910
So set up another function that's going to build the deck of cards.

31
00:02:00,270 --> 00:02:04,920
And then we can create the function just below and then we can move these around as needed.

32
00:02:05,280 --> 00:02:07,380
So not passing any parameters in there.

33
00:02:07,680 --> 00:02:13,530
And this is just going to be a function that's going to take the content of those arrays that we created

34
00:02:13,890 --> 00:02:15,780
and build a deck of cards out of it.

35
00:02:16,050 --> 00:02:23,400
So let's set up an object that we're gonna call game deck using the game object that we've got that

36
00:02:23,400 --> 00:02:24,540
we established earlier.

37
00:02:24,540 --> 00:02:26,640
So this are all of our game content can go.

38
00:02:27,360 --> 00:02:31,290
And I'm going to switch this over to the console because we're going to output the deck of cards into

39
00:02:31,290 --> 00:02:34,470
the console and creating a few loops.

40
00:02:34,560 --> 00:02:37,950
So we're letting I equals and you can also do a for loop.

41
00:02:37,980 --> 00:02:43,260
But I did need to use the variables for the ranks.

42
00:02:43,590 --> 00:02:44,980
So I'm going to keep it at this.

43
00:02:45,030 --> 00:02:49,650
But you could also just do a for loop looping through all the items within the array for each.

44
00:02:50,190 --> 00:02:58,230
So going as I is less than suit's length, and that's how many times we're going to initiate the loop.

45
00:02:58,890 --> 00:03:02,280
Don't forget to increment I because we need to count.

46
00:03:02,970 --> 00:03:04,140
And a way out of the loop.

47
00:03:04,530 --> 00:03:10,080
And this is gonna be another loop inside where we're going to as we're looping through all of the suits.

48
00:03:10,530 --> 00:03:16,260
We also want to loop through all of the ranks are all the possible ranks.

49
00:03:16,980 --> 00:03:20,490
And this is what's making our deck dynamic.

50
00:03:21,120 --> 00:03:25,830
So looping through that array and increment G by one.

51
00:03:26,250 --> 00:03:32,520
And so it's going to give us each individual card A can see that where we've got the suits.

52
00:03:33,360 --> 00:03:37,170
And this is under the eye and ranks.

53
00:03:37,590 --> 00:03:38,550
And this is under J.

54
00:03:39,660 --> 00:03:42,450
So this should produce 52 different results.

55
00:03:42,870 --> 00:03:44,400
So you can see the script runs.

56
00:03:44,670 --> 00:03:50,100
We've got the ace of spades, all of the spades, the hearts, the clubs, the diamonds.

57
00:03:50,560 --> 00:03:55,600
Now let's create an object that we're gonna add to the deck as we're constructing.

58
00:03:55,620 --> 00:03:56,760
And as we're looping through.

59
00:03:57,320 --> 00:04:00,300
So create an object and we'll just call it card.

60
00:04:00,690 --> 00:04:04,170
So we could out a number bunch of different values to the card itself.

61
00:04:04,680 --> 00:04:11,670
And then within the card object, we've got the card suit and the suit is just going to be equal to

62
00:04:11,700 --> 00:04:13,800
whatever the value of suits is.

63
00:04:14,490 --> 00:04:16,260
We've got the card rank.

64
00:04:16,890 --> 00:04:18,650
And this is all contained within the object.

65
00:04:18,660 --> 00:04:20,550
And then we can use this during the gameplay.

66
00:04:21,120 --> 00:04:25,070
So within the ranks and that's controlled by the variable G.

67
00:04:25,650 --> 00:04:27,290
And then we've got card value.

68
00:04:27,330 --> 00:04:33,680
So this one is a little bit tricky because we do have jacks, queens, kings.

69
00:04:33,840 --> 00:04:36,030
But then we also have much new numbers in there.

70
00:04:36,630 --> 00:04:43,320
So let's set up a variable and we're going to just call this temp value and let's work out what the

71
00:04:43,320 --> 00:04:44,910
temp value is going to be.

72
00:04:45,390 --> 00:04:48,630
So setting up that temp value variable with LET.

73
00:04:49,320 --> 00:04:53,670
And first, let's check to see if it's not a number.

74
00:04:54,090 --> 00:04:59,430
And by checking to see if it's not a number is a default function in JavaScript to check.

75
00:04:59,520 --> 00:05:01,800
To see if it's a number or not.

76
00:05:02,310 --> 00:05:09,060
And we've got an example here where it's checking to see if X and if it's not a number that's going

77
00:05:09,060 --> 00:05:09,580
to return back.

78
00:05:09,600 --> 00:05:10,290
Not a number.

79
00:05:10,500 --> 00:05:17,820
And if it is a number, then we're going to multiply whatever the value of X is by 1000 and return it.

80
00:05:18,270 --> 00:05:20,220
So that's an example of how that works.

81
00:05:20,580 --> 00:05:24,340
And we can easily apply that within trinary operator.

82
00:05:24,810 --> 00:05:30,750
So those of you that aren't familiar territory operators, they allow us to do a shortened type condition

83
00:05:31,200 --> 00:05:35,280
where we've got our default JavaScript operator and three operands.

84
00:05:35,760 --> 00:05:38,430
So that condition is followed by a question mark.

85
00:05:38,490 --> 00:05:41,550
So it's kind of like doing if condition.

86
00:05:41,640 --> 00:05:46,260
But we do it all within one statement and we've got that expression to execute.

87
00:05:46,290 --> 00:05:48,330
So if it's true, it's going to return back.

88
00:05:48,360 --> 00:05:49,800
This if it's false.

89
00:05:49,860 --> 00:05:51,070
It's going to return back that.

90
00:05:51,420 --> 00:05:58,500
And the example they give here is that we see if it's if get fee is true.

91
00:05:58,860 --> 00:06:01,440
And that means that it's a member is true.

92
00:06:01,620 --> 00:06:05,550
They're going to get paid two dollars, whereas false is ten dollars.

93
00:06:05,910 --> 00:06:08,100
And if it's one, it's going to be two dollars.

94
00:06:08,130 --> 00:06:09,480
Because one is true.

95
00:06:09,720 --> 00:06:10,470
Zero is false.

96
00:06:10,500 --> 00:06:12,920
So these can also be boolean values that it can take in.

97
00:06:13,470 --> 00:06:15,510
So going back into our example.

98
00:06:15,570 --> 00:06:19,110
So we're checking to see if is not a number.

99
00:06:19,740 --> 00:06:23,770
And we're going to check whatever is being returned back within Rink's G.

100
00:06:24,420 --> 00:06:25,800
So whatever that value is.

101
00:06:25,980 --> 00:06:30,300
So if it hits being a jack, a queen or a king or an ace.

102
00:06:30,660 --> 00:06:31,830
It's not going to be a number.

103
00:06:32,460 --> 00:06:35,760
And in that case, we want to return back a value of 10.

104
00:06:36,060 --> 00:06:41,370
But if it is a number, then we're just going to simply return back the value of the number.

105
00:06:41,880 --> 00:06:46,590
And I'm going to move the console message down below the temp value.

106
00:06:47,010 --> 00:06:48,450
I'll put that in the console.

107
00:06:48,780 --> 00:06:50,940
So we'll see that they all get 10 assigned.

108
00:06:51,240 --> 00:06:55,830
And as we know with in blackjack, Jack Queen King can be 10 as well.

109
00:06:56,610 --> 00:07:03,630
And there is one other adjustment that we need to make is that ISAs can be either eleven or a one.

110
00:07:04,080 --> 00:07:07,290
So we're gonna set a default value of eleven to that.

111
00:07:07,740 --> 00:07:11,670
And this is where we're gonna use the temp value and equal that.

112
00:07:11,730 --> 00:07:13,680
And do another trinary operator.

113
00:07:14,100 --> 00:07:20,820
And this time the condition is gonna check to see if it's the value of ranks.

114
00:07:20,850 --> 00:07:22,620
G is equal to a.

115
00:07:23,220 --> 00:07:26,910
And if it is that we know that we need to update the temp value.

116
00:07:27,240 --> 00:07:28,530
And if it isn't, then we're fine.

117
00:07:28,530 --> 00:07:30,420
We could just keep it at the temp value.

118
00:07:30,810 --> 00:07:37,190
So taking the value of ranks J and checking to see if it's equal in a.

119
00:07:37,590 --> 00:07:39,600
And that means that we're just gonna make that adjustment.

120
00:07:39,900 --> 00:07:45,270
So let's go down to where we have the A and we see that it's being returned back the value of an eleven.

121
00:07:45,840 --> 00:07:47,340
And the reason we're setting at eleven.

122
00:07:47,490 --> 00:07:48,480
And we know what Blakroc.

123
00:07:48,540 --> 00:07:52,080
You can either an ace computer or a one or an eleven.

124
00:07:52,530 --> 00:07:58,600
And we're gonna do that programmatically where we're going to adjust the value once we get over 21.

125
00:07:59,100 --> 00:08:01,770
So until it's twenty over twenty one.

126
00:08:02,130 --> 00:08:04,200
The value of a will be eleven.

127
00:08:04,560 --> 00:08:08,410
And if we have multiple A's and we are going to be doing a function for that.

128
00:08:08,430 --> 00:08:10,230
So that's coming up in upcoming lessons.

129
00:08:10,770 --> 00:08:17,280
So once we've set up and we've established the values within the card object, we want to just simply

130
00:08:17,310 --> 00:08:19,410
add it to the game deck.

131
00:08:20,040 --> 00:08:23,500
And using JavaScript re push.

132
00:08:24,240 --> 00:08:26,340
We're going to push it into the game deck array.

133
00:08:26,940 --> 00:08:28,200
And once we're done.

134
00:08:28,320 --> 00:08:31,080
We'll output the value of game deck.

135
00:08:31,530 --> 00:08:33,120
Remove that console message.

136
00:08:33,210 --> 00:08:35,940
Remove some of that spacing and save.

137
00:08:36,240 --> 00:08:38,050
So that gives us a deck of cards.

138
00:08:38,070 --> 00:08:40,170
And as we can see, we've got 52 cards.

139
00:08:40,560 --> 00:08:43,710
And each one of these cards, they've got to suit rank and value.

140
00:08:44,250 --> 00:08:49,230
So coming up next, we need to shuffle the cards because we don't want them coming out just as they

141
00:08:49,230 --> 00:08:49,530
are.

142
00:08:49,770 --> 00:08:54,220
So it's coming up in the next lesson where we can shuffle the cards after the deck is built.
