-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.js
435 lines (388 loc) · 13.3 KB
/
game.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/** PIG & PLAYER CLASSES **/
class Pig {
constructor(name, image, value, state) {
this.name = name;
this.image = image;
this.value = value;
this.state = state;
}
setState(state) {
switch (state) {
case "unselected":
this.state = "unselected";
this.image = this.name + "-single.jpg";
document.getElementById(this.name).className = "pig-btn";
break;
case "single":
this.state = "single";
this.image = this.name + "-single.jpg";
document.getElementById(this.name).className = "pig-btn single";
break;
case "double":
this.state = "double";
this.image = this.name + "-double.jpg";
document.getElementById(this.name).className = "pig-btn double";
break;
case "disabled":
this.state = "disabled";
this.image = this.name + "-single.jpg";
document.getElementById(this.name).className = "pig-btn";
document.getElementById(this.name).disabled = true;
break;
default:
console.log("default");
break;
}
}
resetState() {
this.state = "unselected";
this.image = this.name + "-single.jpg";
document.getElementById(this.name).className = "pig-btn";
document.getElementById(this.name).disabled = false;
}
}
class Player {
constructor(name, totalPts) {
this.name = name;
this.totalPts = totalPts;
}
}
/** GLOBAL VARIABLES **/
let round;
let rollScore;
let roundScore;
let tempRoundScore = 0;
let currentPlayer;
let tempPlayerScore = 0;
let selectedPigs = [];
let unselectedPigs = [];
let confirmBtn = document.getElementById("roll-done");
let instructions;
let playerList = [];
let card1 = new Pig("sider", "sider-single.jpg", 0.25, "unselected");
let card2 = new Pig("razorback", "razorback-single.jpg", 5, "unselected");
let card3 = new Pig("trotter", "trotter-single.jpg", 5, "unselected");
let card4 = new Pig("snouter", "snouter-single.jpg", 10, "unselected");
let card5 = new Pig("leaning", "leaning-single.jpg", 15, "unselected");
let card6 = new Pig("pigout", "pigout-single.jpg", 0, "unselected");
let card7 = new Pig("oinker", "oinker-single.jpg", 0, "unselected");
let cards = [card1, card2, card3, card4, card5, card6, card7];
/** ADD PLAYER CONNECTION **/
function addPlayers() {
for (let i = 0; i < arguments.length; i++) {
// for every argument in addPlayers()
if (arguments[i] != "") {
// check if empty
let player = new Player(arguments[i], 0); // if not, make a new instance of the Player class
playerList.push(player); // add that instance to the playerList array
}
}
currentPlayer = playerList[0];
addPlayerUI();
}
function addPlayerUI() {
let container = document.getElementById("players");
for (let i = 0; i < playerList.length; i++) {
let plrDiv = document.createElement("div");
plrDiv.setAttribute("id", "player_" + (i + 1));
plrDiv.setAttribute("class", "player-card inactive");
let plrName = document.createElement("p");
plrName.setAttribute("id", "player-name_" + (i + 1));
plrName.setAttribute("class", "player-name");
plrName.innerHTML = playerList[i].name;
let plrTtl = document.createElement("p");
plrTtl.setAttribute("id", "player-total_" + (i + 1));
plrTtl.setAttribute("class", "player-total");
plrTtl.innerHTML = playerList[i].totalPts;
plrDiv.appendChild(plrName);
plrDiv.appendChild(plrTtl);
container.appendChild(plrDiv);
}
let current = playerList.indexOf(currentPlayer);
document.getElementById("player_" + (current + 1)).className = "player-card";
}
function newGame() {
round = 1;
rollScore = 0;
roundScore = 0;
enableDisableByID("confirmRoll", true);
let params = new URLSearchParams(window.location.search); // create an instance of the parameters
let players = JSON.parse(params.get("playerList")); // find all playerList params and parse the JSON into an array
addPlayers(...players); // spread the array out as arguments for
resetCards();
}
function moveToNextPlayer() {
if (playerList.indexOf(currentPlayer) < playerList.length - 1) {
//if not at end of array
let current = playerList.indexOf(currentPlayer);
current++;
currentPlayer = playerList[current];
document.getElementById("player_" + current).className =
"player-card inactive";
document.getElementById("player_" + (current + 1)).className =
"player-card";
newRound();
} else {
currentPlayer = playerList[0];
document.getElementById("player_" + playerList.length).className =
"player-card inactive";
document.getElementById("player_" + 1).className = "player-card";
newRound();
}
document.getElementById;
displayStats();
}
function newRound() {
round++;
roundScore = 0;
rollScore = 0;
enableDisableByID("confirmRoll", true);
newRoll();
}
function newRoll() {
displayStats();
rollScore = 0;
sel = [];
enableDisableByID("confirmRoll", true);
resetCards();
}
function resetCards() {
selectedPigs = [];
unselectedPigs = [
card1.name,
card2.name,
card3.name,
card4.name,
card5.name,
card6.name,
card7.name,
];
card1.resetState();
card2.resetState();
card3.resetState();
card4.resetState();
card5.resetState();
card6.resetState();
card7.resetState();
}
function displayStats() {
document.getElementById("round-num").innerHTML = "Round " + round;
document.getElementById("round-score").innerHTML = roundScore;
document.getElementById("player-name_1").innerHTML = playerList[0].name;
for (let i = 0; i < playerList.length; i++) {
document.getElementById("player-total_" + (i + 1)).innerHTML =
playerList[i].totalPts;
}
}
let sel = [];
function addPigs(array) {
let arraySum = array.reduce((sum, a) => sum + a, 0); //adds up each element in array\
rollScore = Math.floor(arraySum); // sets points to the sum of the array rounded down
}
function pigSelected(card) {
switch (selectedPigs.length) {
case 0: // if no other selected
card.setState("single"); //switchState to single
shiftArray(card.name, unselectedPigs, selectedPigs); //shift to selected
enableDisableByID("oinker", true); //disable oinker
enableDisableByID("pigout", true); //disable pigout
sel.push(card.value);
addPigs(sel);
document.getElementById("confirmRoll").innerHTML = "Select Another Pig!";
break;
case 1: // if one is already selected
switch (card.state) {
case "unselected": //different card as already selected
card.setState("single"); //switchState to single
shiftArray(card.name, unselectedPigs, selectedPigs); //shift from unselected to selected array
enableDisableArray(unselectedPigs, true); //disable all unselected
enableDisableByID("confirmRoll", false);
sel.push(card.value);
addPigs(sel);
document.getElementById(
"confirmRoll"
).innerHTML = `Confirm: ${rollScore}pts`;
break;
case "single": //same card as already selected
card.setState("double"); //switchState to double
enableDisableArray(unselectedPigs, true); //disable all unselected
enableDisableByID("confirmRoll", false); //enable confirm
enableDisableByID("confirmRoll", false);
sel.push(card.value * 3);
addPigs(sel);
document.getElementById(
"confirmRoll"
).innerHTML = `Confirm: ${rollScore}pts`;
break;
case "double":
card.setState("unselected"); //switchState to unselected
shiftArray(card.name, selectedPigs, unselectedPigs); //shift from selected array to unselected array
enableDisableArray(unselectedPigs, false); //disable all unselected
enableDisableByID("oinker", false); //enable oinker
enableDisableByID("confirmRoll", true); //disable confirm
sel.push(card.value * -4);
addPigs(sel);
document.getElementById("confirmRoll").innerHTML = "Select pigs";
break;
default:
break;
}
break;
case 2: //same card already in double; otherwise it would be disabled
card.setState("unselected"); //switchState to unselected
shiftArray(card.name, selectedPigs, unselectedPigs); //shift from selected array to unselected array
enableDisableArray(unselectedPigs, false); //disable all unselected
enableDisableByID("confirmRoll", true); //disable confirm
sel.push(card.value * -1); // need to update confirm roll button to have new rollscore
document.getElementById("confirmRoll").innerHTML = "Select pigs";
break;
default:
break;
}
}
function pigOutSelected(card) {
switch (card.state) {
case "unselected":
card.setState("single"); //switchState to single
shiftArray(card.name, unselectedPigs, selectedPigs); //shift to selected
enableDisableArray(unselectedPigs, true); //disable all unselected
enableDisableByID("oinker", true); //disable oinker
enableDisableByID("confirmRoll", false); //enable confirm
document.getElementById("confirmRoll").innerHTML = "Confirm: Pig Out?";
tempRoundScore = 0; //set tempRoundScore to 0
break;
case "single":
// console.log(`Card: ${card.name}, Selected: ${selectedPigs}, Unsel: ${unselectedPigs}`);
card.setState("unselected"); //switchState to unselected
shiftArray(card.name, selectedPigs, unselectedPigs); //remove from selected
// console.log(`Card: ${card.name}, Selected: ${selectedPigs}, Unsel: ${unselectedPigs}`);
enableDisableArray(unselectedPigs, false); //enable all unselected
enableDisableByID("oinker", false); //disable oinker
enableDisableByID("confirmRoll", true); //enable confirm
document.getElementById("confirmRoll").innerHTML = "Select pigs";
tempRoundScore = 0; //set tempRoundScore to roundScore
break;
case "double":
//do nothing
break;
default:
break;
}
}
function oinkerSelected(card) {
switch (card.state) {
case "unselected":
card.setState("single"); //switchState to single
shiftArray(card.name, unselectedPigs, selectedPigs); //shift to selected
enableDisableArray(unselectedPigs, true); //disable all unselected
enableDisableByID("confirmRoll", false); //enable confirm
document.getElementById("confirmRoll").innerHTML = "Confirm: Oinker?";
tempPlayerScore = 0; //set tempPlayerScore to 0
break;
case "single":
card.setState("unselected"); //switchState to unselected
shiftArray(card.name, selectedPigs, unselectedPigs); //remove from selected
enableDisableArray(unselectedPigs, false); //enable all unselected
enableDisableByID("confirmRoll", true); //disable confirm
document.getElementById("confirmRoll").innerHTML = "Done";
tempPlayerScore = tempRoundScore; //set tempPlayerScore to PlayerScore
break;
case "double":
//do nothing
break;
default:
break;
}
}
/**
* Enable or disable an array
* param array unselected or selected
* param bool true=disabled, false=enabled
*/
function enableDisableArray(array, bool) {
for (let i = 0; i < array.length; i++) {
document.getElementById(array[i]).disabled = bool;
}
}
/**
* Enable or disable an element by ID
* param id the elements ID
* param bool true=disabled, false=enabled
*/
function enableDisableByID(id, bool) {
document.getElementById(id).disabled = bool;
}
function confirmRoll() {
switch (selectedPigs[0]) {
case "pigout":
roundScore = 0;
enableDisableByID("bank", true);
moveToNextPlayer();
newRoll();
break;
case "oinker":
currentPlayer.totalPts = 0;
roundScore = 0;
enableDisableByID("bank", true);
moveToNextPlayer();
newRoll();
break;
default:
document.getElementById("confirmRoll").innerHTML = "Select pigs";
roundScore = roundScore + rollScore;
enableDisableByID("bank", false);
newRoll();
break;
}
displayStats();
}
function bank() {
if (roundScore > 0) {
currentPlayer.totalPts = currentPlayer.totalPts + roundScore;
enableDisableByID("bank", true);
moveToNextPlayer();
newRoll();
displayStats();
} else {
console.log("no points to bank");
}
}
function shiftArray(pig, fromArray, toArray) {
let index = fromArray.indexOf(pig); // find pig name in array
if (index > -1) {
// if found run code
fromArray.splice(index, 1); // remove pig from index
toArray.push(pig); // add pig to array
} else {
// if not found, don't do anything
}
}
function closeRules() {
let modal = document.getElementById("modal");
let overlay = document.getElementById("overlay");
modal.classList.remove("isVisible");
overlay.classList.remove("isVisible");
}
function openRules() {
let modal = document.getElementById("modal");
let overlay = document.getElementById("overlay");
modal.classList.add("isVisible");
overlay.classList.add("isVisible");
}
newGame();
closeRules();
/*
RULES
Pig out: lose all turn points; next turn
Sider: 1 point
Trotter: 5 points
Double Trotter: 20 points (or 4x)
Razorback: 5 points
Double Razorback: 20 points (or 4x)
Snouter: 10pts
Double snouter: 40pts (or 4x)
leaning jowler: 15pts
double leaning jowler: 60 points (or 4x)
mixed combo: add single points (unless mixed with sider; then sider is 0)
Oinker (touching pigs): lose all game points; next turn
*/