4
4
5
5
class Game {
6
6
constructor ( ) {
7
- this . suits = [ 'spades' , 'clubs' , 'hearts' , 'diams' ] // Array of Card Suits
8
- this . values = [ 'A' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '10' , 'J' , 'Q' , 'K' ] // Array of possible card-ranks ie. values.
9
-
10
- this . deck = [ ]
11
- this . players = [ ]
7
+ // Array of Card Suits - should not accessed from outside the class
8
+ this . _suits = [ 'spades' , 'clubs' , 'hearts' , 'diams' ]
9
+ // Array of possible card-ranks ie. values - should not accessed from outside the class
10
+ this . _values = [ 'A' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '10' , 'J' , 'Q' , 'K' ]
11
+
12
+ // the cards in play for this game
13
+ this . _deck = [ ]
14
+ // ids of the cards for this game - should not accessed from outside the class
15
+ this . _idNumber = 1
16
+
17
+ // the players for this game - should not mutated from outside class
18
+ this . _players = [ ]
19
+ // index of the player whose turn it currently is
12
20
this . _turn = 0
13
21
14
- this . centralStack = [ ] // Central stack to store cards that are clicked upon and moved by players
15
- this . record = [ ] // Adding a record to store whether the last player bluffed or not
16
- this . currentRank = '' // Data of which rank is currently being played
22
+ // Central stack to store cards that are moved by players
23
+ this . _centralStack = [ ]
24
+ // Adding a record to store whether the last player bluffed or not
25
+ this . _record = ''
26
+ // Data of which rank is currently being played
27
+ this . _currentRank = ''
28
+ }
17
29
18
- this . idNumber = 1
30
+ get players ( ) {
31
+ return this . _players
32
+ }
33
+
34
+ get deck ( ) {
35
+ return this . _deck
36
+ }
37
+
38
+ set deck ( deck ) {
39
+ this . _deck = deck
19
40
}
20
41
21
42
get turn ( ) {
@@ -26,6 +47,30 @@ class Game {
26
47
this . _turn = turn
27
48
}
28
49
50
+ get centralStack ( ) {
51
+ return this . _centralStack
52
+ }
53
+
54
+ set centralStack ( stack ) {
55
+ this . _centralStack = stack
56
+ }
57
+
58
+ get record ( ) {
59
+ return this . _record
60
+ }
61
+
62
+ set record ( record ) {
63
+ this . _record = record
64
+ }
65
+
66
+ get currentRank ( ) {
67
+ return this . _currentRank
68
+ }
69
+
70
+ set currentRank ( rank ) {
71
+ this . _currentRank = rank
72
+ }
73
+
29
74
start ( ) {
30
75
// input the number of players
31
76
let playerCount = window . prompt ( 'Enter the number of players(Between 2 and 12): ' )
@@ -45,7 +90,7 @@ class Game {
45
90
this . addDeck ( )
46
91
}
47
92
48
- // shuffle
93
+ // shuffle the cards
49
94
this . shuffle ( )
50
95
51
96
// Creating n players based on user input
@@ -71,15 +116,15 @@ class Game {
71
116
// add a 54 card deck to the cards beinf used in this game
72
117
addDeck ( ) {
73
118
// iterate over the suits and the values generating a new card.
74
- this . suits . forEach ( ( suit ) => {
75
- this . values . forEach ( ( value ) => {
76
- this . deck . push ( new Card ( suit , value , this . idNumber ++ ) )
119
+ this . _suits . forEach ( ( suit ) => {
120
+ this . _values . forEach ( ( value ) => {
121
+ this . deck . push ( new Card ( suit , value , this . _idNumber ++ ) )
77
122
} )
78
123
} )
79
124
80
125
// Two Joker Cards pushed to Deck.
81
- this . deck . push ( new Card ( 'Joker' , 'Joker' , this . idNumber ++ ) )
82
- this . deck . push ( new Card ( 'Joker' , 'Joker' , this . idNumber ++ ) )
126
+ this . deck . push ( new Card ( 'Joker' , 'Joker' , this . _idNumber ++ ) )
127
+ this . deck . push ( new Card ( 'Joker' , 'Joker' , this . _idNumber ++ ) )
83
128
}
84
129
85
130
shuffle ( ) {
@@ -99,7 +144,7 @@ class Game {
99
144
createPlayers ( playerCount ) {
100
145
// create the players one at time and push them to the game object's players attribute
101
146
for ( let i = 0 ; i < playerCount ; i ++ ) {
102
- this . players . push ( new Player ( 'Player ' + ( i + 1 ) ) )
147
+ this . _players . push ( new Player ( 'Player ' + ( i + 1 ) ) )
103
148
}
104
149
105
150
// Array to store the number of cards each player should get.
@@ -119,7 +164,7 @@ class Game {
119
164
120
165
// Attaching the number of cards each player gets to the player, to keep track of the number of cards the player has
121
166
for ( let i = 0 ; i < playerCount ; i ++ ) {
122
- this . players [ i ] . numberOfCards = parts [ i ]
167
+ this . _players [ i ] . numberOfCards = parts [ i ]
123
168
}
124
169
}
125
170
0 commit comments