-
Notifications
You must be signed in to change notification settings - Fork 0
/
js-party-browser.js
614 lines (602 loc) · 20.3 KB
/
js-party-browser.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
//Define Player class
class Player {
constructor (name){
this.name = name;
this.playerNumber;
this.boardPosition = 0;
this.coinBalance = startingCoinBalance;
this.inventory = {};
this.starBalance = 0;
this.spaceColor = blue;
}
//define static methods
static rollDie = function (){
return Math.ceil(Math.random() * dieSize);
}
static subtractFromAll(coins){
let subtracted = 0;
for(let i = 1; i <= playerCount; i++){
let initialBalance = player[i].coinBalance;
player[i].decreaseBalance(coins);
player[i].updateSpace();
subtracted += (initialBalance - player[i].coinBalance);
}
return subtracted;
}
//define instance methods
announce (){
if (board.space[this.boardPosition][0] === blue){
confirm (`${coin} ${this.name} gains ${blueReward} coins! ${coin}`)
}
else if (board.space[this.boardPosition][0] === red){
if(this.coinBalance < redPenalty){
confirm (`${coin} ${this.name} loses ${this.coinBalance} coins! ${coin}`);
}
else confirm (`${coin} ${this.name} loses ${redPenalty} coins! ${coin}`);
}
}
increaseBalance (coins){
this.coinBalance += coins;
}
decreaseBalance (coins){
if (this.coinBalance >= coins) {
this.coinBalance -= coins;
}
else {
this.coinBalance = 0;
}
}
clearSpace(){
board.space[this.boardPosition].forEach((ele, index) => {
if (ele.includes(this.name)) board.space[this.boardPosition].splice(index, 1, '');
});
}
fillSpace(){
if(this.spacesRemaining){
board.space[this.boardPosition][this.playerNumber] = `${this.name} ${coin}: ${this.coinBalance} ${star}: ${this.starBalance} ${item} ${Object.keys(this.inventory).length} ${die}: ${this.spacesRemaining}`;
}
else {
board.space[this.boardPosition][this.playerNumber] = `${this.name} ${coin}: ${this.coinBalance} ${star}: ${this.starBalance} ${item} ${Object.keys(this.inventory).length}`;
}
}
updateSpace(){
this.clearSpace();
this.fillSpace();
}
checkPosition(i, newRoll){
if (i < newRoll){
if (board.space[this.boardPosition][0] === star){
refreshConsole();
this.offerStar();
this.updateSpace();
refreshConsole();
}
else if (board.space[this.boardPosition][0] === item) {
refreshConsole();
this.offerItem();
this.updateSpace();
refreshConsole();
}
}
else {
refreshConsole();
if (board.space[this.boardPosition][0] === star){
this.offerStar();
}
else if (board.space[this.boardPosition][0] === item) {
this.offerItem();
}
else if (board.space[this.boardPosition][0] === event) {
GameBoard.chooseRandomEvent();
}
else if (board.space[this.boardPosition][0] === blue) {
this.increaseBalance(blueReward);
}
else if (board.space[this.boardPosition][0] === red) {
this.decreaseBalance(redPenalty);
}
this.updateSpace();
this.announce();
refreshConsole();
}
}
advancePlayer(i, newRoll){
this.clearSpace();
let newBoardPosition = (this.boardPosition + 1) % boardSize;
this.boardPosition = newBoardPosition;
this.spacesRemaining --;
this.fillSpace();
this.checkPosition(i, newRoll);
}
move(){
confirm(`${die} ${this.name}'s turn ${die}`);
if (Object.keys(this.inventory).length > 0) this.useItem();
let newRoll = Player.rollDie();
confirm(`${die} ${this.name} rolled a ${newRoll} ${die}`);
this.spacesRemaining = newRoll;
for (let i = 1; i <= newRoll; i ++){
this.advancePlayer(i, newRoll);
}
}
offerStar(){
if (this.coinBalance >= starCost){
if (confirm(`${star} Congratulations, ${this.name}! You have reached the Star! Would you like to purchase it for ${starCost} coins? ${star}`)){
this.coinBalance -= starCost;
this.starBalance += 1;
this.updateSpace();
refreshConsole();
confirm (`${star} ${this.name} has purchased a star! The board will be reset! ${star} `);
board.randomize();
refreshConsole();
confirm (`The star is now at space ${board.starPosition}`)
}
else confirm ("Best of luck then!")
}
else confirm (`${star} You've reached the star, but you don't have enough coins to buy it yet! Keep playing! ${star}`)
}
offerItem(){
if (this.coinBalance >= board.getMinimumItemCost() && Object.keys(this.inventory).length < 3){
let playerWantsItem = confirm (`${item} You've found an item vendor. Would you like to purchase an Item? ${item}`);
if (playerWantsItem){
let validInputs = ["a", "b", "c"];
let itemsForSale = Object.values(board.itemSet);
let itemMenu ={}
let playerInput;
for (let i = 0; i < itemsForSale.length; i++){
itemMenu[validInputs[i]] = itemsForSale[i];
}
function offerChoices(player){
refreshConsole();
playerInput = prompt(`Enter the letter of the Item you would like. Your choices are \n ${item} a: ${itemsForSale[0].name} ${coin} ${itemsForSale[0].cost} \n ${item} b: ${itemsForSale[1].name} ${coin} ${itemsForSale[1].cost} \n ${item} c: ${itemsForSale[2].name} ${coin} ${itemsForSale[2].cost}`);
if (!validInputs.includes(playerInput)){
refreshConsole();
confirm ("Please input a valid letter");
offerChoices(player);
}
else if(player.inventory[`${itemMenu[playerInput].name}`]){
refreshConsole();
confirm ("You already have one of those. Please choose something else!")
offerChoices(player);
}
}
offerChoices(this);
if (validInputs.includes(playerInput) && !this.inventory[itemMenu[playerInput]] && this.coinBalance >= itemMenu[playerInput].cost){
this.inventory[itemMenu[playerInput].name] = itemMenu[playerInput];
this.coinBalance -= itemMenu[playerInput].cost;
this.updateSpace();
refreshConsole();
confirm(`${item} ${this.name} bought: ${itemMenu[playerInput].name} ${item}`)
}
else if (this.coinBalance < itemMenu[playerInput].cost){
confirm ("You do not have enough coins for that Item. Please choose another.")
this.offerItem();
}
}
else confirm ("See you another time!");
}
else if (Object.keys(this.inventory).length ===3 ) confirm ("You've reached an item vendor, but you can't carry any more items!")
else confirm("You've reached an item vendor, but you don't have enough coins to make a purchase! Come back later!");
}
useItem () {
let wantsToUseItem = confirm ("Would you like to use an Item?");
if (wantsToUseItem){
let itemMenu = {};
let playerInventory = Object.values(this.inventory);
let playerInput;
let validInputs = [];
for (let i = 0; i < playerInventory.length; i ++){
validInputs.push(numberToLetters(i));
itemMenu[validInputs[i]] = playerInventory[i];
}
function offerChoices(){
refreshConsole();
if (playerInventory.length === 3){
playerInput = prompt(`Enter the letter of the Item you would like to use. \n ${item} a: ${playerInventory[0].name} \n ${item} b: ${playerInventory[1].name}, \n ${item} c: ${playerInventory[2].name}`);
}
else if(playerInventory.length === 2){
playerInput = prompt(`Enter the letter of the Item you would like to use. \n ${item} a: ${playerInventory[0].name} \n ${item} b: ${playerInventory[1].name}`);
}
else if(playerInventory.length === 1){
playerInput = prompt(`Enter the letter of the Item you would like to use. \n ${item} a: ${playerInventory[0].name}`);
}
if(!validInputs.includes(playerInput)) {
refreshConsole();
confirm("Please enter a valid input!");
offerChoices ();
}
}
offerChoices();
this.inventory[itemMenu[playerInput].name].use(player[`${this.playerNumber}`]);
}
else confirm("Very well!");
}
}
//define Item Classes and Subclasses
class Item {
constructor(){
this.name = undefined;
this.cost = undefined;
this.effect = undefined;
this.inventorySize = 1;
}
}
class ExtraDie extends Item {
constructor (){
super();
this.name = "Extra Die";
this.cost = 10;
this.effect = "Grants the player an additional Role";
this.use = function (player){
delete player.inventory["Extra Die"];
refreshConsole();
confirm(`${player.name} uses an extra die. They get to roll twice!`)
player.updateSpace();
refreshConsole();
let extraRoll = Player.rollDie();
confirm(`${die} ${player.name} rolled a ${extraRoll} ${die}`);
player.spacesRemaining = extraRoll;
for (let i = 1; i <= extraRoll; i ++){
player.advancePlayer(i, extraRoll);
}
confirm("Roll again!");
}
}
}
class PositionSwapper extends Item{
constructor(){
super();
this.name = "Position Swapper";
this.cost = 10;
this.effect = "Shuffles the positions of all players";
this.use = function (user){
delete user.inventory["Position Swapper"];
let choiceArray = [];
let validInputs = [];
let playerInput;
for (let i = 1; i <= playerCount; i++){
if(user.playerNumber !== i){
choiceArray.push(`${player[i].playerNumber}: ${player[i].name}`);
validInputs.push(`${player[i].playerNumber}`);
}
}
user.updateSpace();
refreshConsole();
function askChoice(){
refreshConsole();
playerInput = prompt(`Enter the number of the player would you like to swap with? \n ${choiceArray}`);
if(!validInputs.includes(playerInput)) {
refreshConsole();
confirm ("Please input a valid player number");
askChoice();
}
}
askChoice();
let target = player[playerInput]
let targetPosition = target.boardPosition;
let userPosition = user.boardPosition;
target.clearSpace();
target.boardPosition = userPosition;
target.fillSpace();
user.clearSpace();
user.boardPosition = targetPosition;
user.fillSpace();
refreshConsole();
confirm(`${user.name} and ${target.name} have switched spaces!`);
}
}
}
class CoinSnatcher extends Item{
constructor(){
super();
this.name = "Coin Snatcher";
this.cost = 10;
this.effect = "Steals 10 coins from each other player";
this.use = function (user){
let initialCoinBalance = user.coinBalance
delete user.inventory["Coin Snatcher"];
let loot = Player.subtractFromAll(10);
user.coinBalance += loot;
user.updateSpace();
refreshConsole();
confirm(`${user.name} steals ${user.coinBalance - initialCoinBalance} coins!`);
}
}
}
//Define Board class
class GameBoard{
constructor (numStars, numEvents, numVendors){
this.starCount = numStars;
this.eventCount = numEvents;
this.vendorCount = numVendors;
this.space = {};
//define items for the board
this.itemSet = {
"Extra Die" : new ExtraDie(),
"Position Swapper" : new PositionSwapper(),
"Coin Snatcher" : new CoinSnatcher(),
}
}
//define static methods
static initializePlayers(){
let playerCount;
function askPlayerCount(){
playerCount = prompt("Enter the number of players (2 minimum)")
if (playerCount < 2 || playerCount % 1 !== 0){
confirm ("Please enter a valid number of players")
askPlayerCount();
}
}
askPlayerCount();
let player = {};
let playerInputs = [];
for(let i = 1; i <= playerCount; i++){
let newPlayerName = prompt("Enter the name of Player " + i);
playerInputs.forEach((ele, index) => {
if (ele.includes(newPlayerName) || newPlayerName.includes(ele)){
confirm("Please make sure that no player's name includes anothers.");
GameBoard.initializePlayers();
}
})
playerInputs.push(newPlayerName);
player[i] = new Player(newPlayerName);
player[i].playerNumber = i;
}
return player;
}
static setGameLength(){
let gameLength;
function askGameLength(){
let playerInput = prompt("Enter the number of rounds would you like to play (1 minimum)");
if (playerInput < 1 || playerInput % 1 !== 0){
confirm ("Please enter a valid number of rounds");
askGameLength();
}
else gameLength = playerInput;
}
askGameLength();
return gameLength;
}
static createBoard(numStars, numEvents, numItems){
let newBoard = new GameBoard (numStars,numEvents,numItems);
newBoard.randomize();
return newBoard;
}
static initializeBoardState(){
Object.keys(player).forEach(playerNumber =>{
board.space[0].push(`${player[playerNumber].name} ${coin}: ${player[playerNumber].coinBalance} ${star}: ${player[playerNumber].starBalance} ${item} ${Object.keys(player[playerNumber].inventory).length}`);
});
}
static closestGuessContest = function (){
console.table(board.space);
confirm (`${miniGame} All players deposit 5 coins and guess a number from 1 to 100. Winner takes all! ${miniGame} `)
let prizePool = Player.subtractFromAll(5);
for (let i = 1; i <= playerCount; i++){
player[i].updateSpace();
}
refreshConsole("event");
let target = Math.ceil(Math.random() * 100);
let guessArray = [];
let guessArrayValidity = true;
let playerInput;
function takeGuesses(){
for(let i = 1; i <= playerCount; i++){
playerInput = prompt(`${player[i].name}, enter your guess:`);
guessArray.push(playerInput);
for (let j = 0; j < guessArray.length; j++){
if (guessArray[j] < 1 || guessArray[j] > 100 || playerInput % 1 !==0) guessArrayValidity = false;
}
if(!guessArrayValidity){
guessArray =[];
guessArrayValidity = true;
confirm("Please enter a valid number");
takeGuesses();
break;
}
}
}
takeGuesses();
let smallestDifference = Infinity;
let winningIndex;
guessArray.forEach((ele, index) => {
if (Math.abs(target - ele) < smallestDifference){
smallestDifference = Math.abs(target - ele);
winningIndex = index +1 ;
}
});
confirm (`${miniGame} The number was ${target}. ${player[winningIndex].name} wins ${prizePool} coins! ${miniGame}`);
player[`${winningIndex}`].increaseBalance(prizePool);
for (let i = 1; i <= playerCount; i++){
player[i].updateSpace();
}
refreshConsole("event");
}
static randomizePositions(){
confirm (`Players positions will be shuffled!`)
let positionArray = [];
for (let i = 1; i <= playerCount; i++){
positionArray.push(player[i].boardPosition);
}
let shuffledPositions = shuffleArray(positionArray);
for (let i = 1; i <= playerCount; i++){
player[i].clearSpace();
player[i].boardPosition = shuffledPositions[i - 1];
player[i].fillSpace();
}
refreshConsole();
}
static coinGiveaway (){
confirm(`Everyone gets 10 coins!`);
for (let i = 1; i <= playerCount; i++){
player[i].increaseBalance(10);
player[i].updateSpace();
}
refreshConsole();
}
static coinTakeaway (){
confirm(`Everyone loses 10 coins!`);
for (let i = 1; i <= playerCount; i++){
player[i].decreaseBalance(10);
player[i].updateSpace();
refreshConsole();
}
}
static chooseRandomEvent(){
confirm (`${event}${event}${event} Event Time! ${event}${event}${event}`)
let randInt = Math.ceil(Math.random()*100);
if (randInt <= 50) GameBoard.randomizePositions();
else if (randInt <= 80) GameBoard.coinGiveaway();
else GameBoard.coinTakeaway();
}
static declareWinner(){
let winner;
let winnerStarCount = -Infinity;
let winnerCoinBalance = -Infinity;
for (let i = 1; i <= playerCount; i++){
if(player[i].starBalance > winnerStarCount){
winnerStarCount = player[i].starBalance
winnerCoinBalance = player[i].coinBalance;
winner = player[i];
}
else if(player[i].starBalance === winnerStarCount && player[i].coinBalance > winnerCoinBalance){
winnerCoinBalance = player[i].coinBalance;
winner = player[i];
}
}
separator(trophy, 12,`${winner.name} has won the game! Congratulations!`);
}
//define instance methods
randomize (){
//calculate number of each kind of space
let numStars = this.starCount;
let numEvents = this.eventCount;
let numVendors = this.vendorCount;
let numBlue = Math.floor((boardSize - numStars - numEvents - numVendors) * (3 / 4));
let numRed = boardSize - numStars - numEvents - numVendors -numBlue;
//create input arrays for populateArray
let spaceDistribution = [numStars, numEvents,numVendors, numBlue, numRed];
let spaceLabels = [star, event, item, blue, red]
//create and shuffle spaceArr
let spaceArr = shuffleArray(populateArray(spaceDistribution,spaceLabels));
//make space object from spaceArr
spaceArr.forEach((element, index) => {
if (this.space[index]) this.space[index][0] = element;
else this.space[index] = [element];
});
this.getStarLocation();
}
getStarLocation(){
for (let key in this.space){
if (this.space[key][0].includes(`${star}`)){
this.starPosition = key;
}
}
}
getMinimumItemCost(){
let minimumItemCost = Infinity;
for (let item in this.itemSet){
if (this.itemSet[item].cost < minimumItemCost) {
minimumItemCost = this.itemSet[item].cost;
}
}
this.minimumItemCost = minimumItemCost;
return minimumItemCost;
}
}
//useful helper functions
function shuffleArray (array){
let output = [];
function shuffler (array){
if (array.length === 0) return output;
let randInt = Math.floor(array.length * Math.random());
output.push(array[randInt]);
array.splice(randInt, 1);
return shuffler(array);
}
return shuffler(array);
}
function populateArray (countArr, labelArr){
let output = [];
for (let i = 0; i < countArr.length; i++){
for (let j = 0; j < countArr[i]; j++){
output.push(labelArr[i])
}
}
return output;
}
function numberToLetters(num) {
let letters = ''
while (num >= 0) {
letters = 'abcdefghijklmnopqrstuvwxyz'[num % 26] + letters
num = Math.floor(num / 26) - 1
}
return letters
}
function separator (character, count, message){
output = character.repeat(count) + " " + message + " " + character.repeat(count);
console.log(output);
}
function refreshConsole(phase){
if (phase === "event"){
console.clear();
let message = "Event " + currentRound + `/${rounds}`;
separator(miniGame, 20, message);
}
else{
console.clear();
let message = "Round " + currentRound + `/${rounds}`;
separator(sparkle, 20, message);
}
console.table(board.space);
}
//set global variables
const red = `\u{1f534}`;
const blue = `\u{1f535}`;
const star = `\u{2b50}`;
const item = `\u{1f36c}`;
const event = `\u{2757}`;
const coin = `\u{1f4b0}`;
const die = ` \u{1f3b2}`;
const miniGame = `\u{26A1}`;
const boom = `\u{1f4a5}`;
const sparkle = `\u{2728}`;
const party = `\u{1f38a}`;
const check = `\u{2705}`;
const trophy = `\u{1F3c6}`
const boardSize = 30; //spaces are numbered from 0 up to boardSize - 1
const dieSize = 10; //maximum possible roll
const delay = 300;
const starCost = 30;
const blueReward = 5;
const redPenalty = 3;
const startingCoinBalance = 10;
const numStars = 1;
const numEvents = 3;
const numVendors = 2;
separator(party, 20, "Welcome to JS Party");
const board = GameBoard.createBoard(numStars, numEvents, numVendors);
const player = GameBoard.initializePlayers();
const rounds = GameBoard.setGameLength();
const playerCount = Object.keys(player).length;
const consent = confirm ("Ready? " +check);
//main game function
function beginGame (){
GameBoard.initializeBoardState();
for (let i = 1; i <= rounds; i++){
currentRound = i;
for (let i =1; i <= playerCount; i ++){
refreshConsole();
player[i].move();
}
confirm(`${miniGame} Mini Game Time! ${miniGame}`);
console.clear();
separator(`${miniGame}`, 20, "Event " + i + `/${rounds}`);
GameBoard.closestGuessContest();
}
refreshConsole();
GameBoard.declareWinner();
}
//start game
if (consent){
beginGame();
}
else alert("OK...See you later!")