Skip to content

Commit e33237d

Browse files
author
Pratham Gupta
committed
added getters and setters according to every game attribute, moved renderDeck
1 parent fd822e5 commit e33237d

File tree

2 files changed

+62
-17
lines changed

2 files changed

+62
-17
lines changed

client/js/Game.js

+62-17
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,39 @@
44

55
class Game {
66
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
1220
this._turn = 0
1321

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+
}
1729

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
1940
}
2041

2142
get turn () {
@@ -26,6 +47,30 @@ class Game {
2647
this._turn = turn
2748
}
2849

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+
2974
start () {
3075
// input the number of players
3176
let playerCount = window.prompt('Enter the number of players(Between 2 and 12): ')
@@ -45,7 +90,7 @@ class Game {
4590
this.addDeck()
4691
}
4792

48-
// shuffle
93+
// shuffle the cards
4994
this.shuffle()
5095

5196
// Creating n players based on user input
@@ -71,15 +116,15 @@ class Game {
71116
// add a 54 card deck to the cards beinf used in this game
72117
addDeck () {
73118
// 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++))
77122
})
78123
})
79124

80125
// 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++))
83128
}
84129

85130
shuffle () {
@@ -99,7 +144,7 @@ class Game {
99144
createPlayers (playerCount) {
100145
// create the players one at time and push them to the game object's players attribute
101146
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)))
103148
}
104149

105150
// Array to store the number of cards each player should get.
@@ -119,7 +164,7 @@ class Game {
119164

120165
// Attaching the number of cards each player gets to the player, to keep track of the number of cards the player has
121166
for (let i = 0; i < playerCount; i++) {
122-
this.players[i].numberOfCards = parts[i]
167+
this._players[i].numberOfCards = parts[i]
123168
}
124169
}
125170

File renamed without changes.

0 commit comments

Comments
 (0)