-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamemodel.js
206 lines (179 loc) · 5.8 KB
/
gamemodel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
class Game {
constructor(room, players) {
this.room = room;
this.deck = getSuffledDeck();
this.players = players;
this.currentPlayerIdx = 0;
this.cards = handOutCards(this.deck, this.players);
this.outOfGameCards = [];
this.stack = [];
this.nrOfReadyPlayers = 0;
this.isOver = false;
this.winnersIndex = null;
};
getCardFromDeck() {
return this.deck.pop();
};
getIndexOfPrevPlayer() {
return (this.currentPlayerIdx - 1) % this.players.length;
};
makemove(playedCards, i) {
this.transferCards(playedCards, i);
this.getNewCards(i);
this.sortHandCards(i);
if (this.isFinished(i)) {
this.makeAWinner(i);
} else if (playedCards[0] === 10 || this.fourInARow(playedCards)) {
this.burnStack();
} else {
if (i === this.currentPlayerIdx) {
this.setNextPlayer();
}
}
};
transferCards(playedCards, i) {
const handCards = this.cards[i].handCards;
const lastCards = this.cards[i].lastCards;
const flippedCards = this.cards[i].flippedCards;
playedCards.forEach(card => {
// Remove card from player
if (handCards.indexOf(card) !== -1) {
handCards.splice(handCards.indexOf(card), 1);
} else if (lastCards.indexOf(card) !== -1) {
lastCards.splice(lastCards.indexOf(card), 1);
} else if (flippedCards.indexOf(card) !== -1) {
flippedCards.splice(flippedCards.indexOf(card), 1);
}
// Add card to stack in the middle
this.stack.push(card);
});
};
getNewCards(desiredIndex) {
while (this.deck.length > 0 && this.cards[desiredIndex].handCards.length < 3) {
this.cards[desiredIndex].handCards.push(this.getCardFromDeck());
}
};
isFinished(i) {
return this.cards[i].handCards.length === 0
&& this.cards[i].lastCards.length === 0
&& this.cards[i].flippedCards.length === 0;
};
fourInARow(playedCards) {
let alltogether = playedCards.length === 4;
let successively = false;
let end = this.stack.length;
if (playedCards[0] === this.stack[end - 1] &&
playedCards[0] === this.stack[end - 2] &&
playedCards[0] === this.stack[end - 3] &&
playedCards[0] === this.stack[end - 4]) {
successively = true;
}
return (alltogether || successively) ? true : false;
};
makeAWinner(i) {
this.isOver = true;
this.winnersIndex = i;
};
sortHandCards(index) {
this.cards[index].handCards.sort(numericSort);
};
sortLastCards(index) {
this.cards[index].lastCards.sort(numericSort);
};
pickUp() {
while (this.stack.length > 0) {
this.cards[this.currentPlayerIdx].handCards.push(this.stack.pop());
}
this.sortHandCards(this.currentPlayerIdx);
this.sortLastCards(this.currentPlayerIdx);
this.setNextPlayer();
};
swapCards(playerIndex, newhand, newlast) {
this.cards[playerIndex].handCards = newhand;
this.cards[playerIndex].lastCards = newlast;
this.sortHandCards(playerIndex);
this.sortLastCards(playerIndex);
return this;
};
burnStack() {
while (this.stack.length > 0) {
this.outOfGameCards.push(this.stack.pop());
}
};
faceUp(playerIndex, flippedCardIndex) {
let flippedCard = this.cards[playerIndex].flippedCards.splice(flippedCardIndex, 1)[0];
this.cards[playerIndex].handCards.push(flippedCard);
};
setNextPlayer() {
// Not an elegant way: TODO: use recursion
if (this.stack[this.stack.length - 1] === 8) {
this.skip();
return;
}
if (this.stack[this.stack.length - 1] === 3) {
if (this.stack[this.stack.length - 2] === 8) {
this.skip();
return;
}
if (this.stack[this.stack.length - 2] === 3) {
if (this.stack[this.stack.length - 3] === 8) {
this.skip();
return;
}
if (this.stack[this.stack.length - 3] === 3) {
if (this.stack[this.stack.length - 4] === 8) {
this.skip();
return;
}
}
}
}
this.noSkip();
};
noSkip() {
this.currentPlayerIdx = (this.currentPlayerIdx + 1) % this.players.length;
};
skip() {
this.currentPlayerIdx = (this.currentPlayerIdx + 2) % this.players.length;
};
};
function getDeck() {
let values = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];
let deck = []
values.forEach((elem) => {
for (let i = 0; i < 4; i++) {
deck.push(elem);
}
});
return deck;
};
// Nice in-place O(n) shuffle thanks to this post: https://bost.ocks.org/mike/shuffle/
function shuffle(array) {
var m = array.length, t, i;
while (m) {
i = Math.floor(Math.random() * m--);
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array;
};
function getSuffledDeck() {
return shuffle(getDeck());
};
function handOutCards(deck, players) {
let cardsArr = [];
players.forEach(() => {
let playersCards = {
flippedCards: [deck.pop(), deck.pop(), deck.pop()],
lastCards: [deck.pop(), deck.pop(), deck.pop()],
handCards: [deck.pop(), deck.pop(), deck.pop()],
};
cardsArr.push(playersCards);
});
return cardsArr;
};
function numericSort(a, b) {
return a - b;
};
module.exports = Game;