Advertisement
Pijomir

Card Game

Aug 27th, 2022
1,277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function countCards(input) {
  2.     let players = new Map();
  3.     for (let string of input) {
  4.         let parts = string.split(': ');
  5.         let playerName = parts.shift();
  6.         let cards = parts[0].split(', ');
  7.         if (players.has(playerName)) {
  8.             let ownedCards = players.get(playerName);
  9.             cards.push(...ownedCards);
  10.         }
  11.  
  12.         players.set(playerName, cards);
  13.     }
  14.  
  15.     for (let playerName of players.keys()) {
  16.         let cardsToBeFiltered = players.get(playerName);
  17.         let filteredCards = cardsToBeFiltered.filter((el, i) => {
  18.             return cardsToBeFiltered.indexOf(el) === i
  19.         });
  20.         players.set(playerName, filteredCards);
  21.     }
  22.  
  23.     let power = new Map([['J', 11], ['Q', 12], ['K', 13], ['A', 14]]);
  24.     let type = new Map([['S', 4], ['H', 3], ['D', 2], ['C', 1]]);
  25.  
  26.     for (let playerName of players.keys()) {
  27.         let allCards = players.get(playerName);
  28.         let countOfCards = allCards
  29.             .map(a => {
  30.                 if (isNaN(+a[0])) {
  31.                     return power.get(a[0]) * type.get(a[1]);
  32.                 } else {
  33.                     if (a.length === 2) {
  34.                         return +a[0] * type.get(a[1]);
  35.                     } else {
  36.                         return 10 * type.get(a[2]);
  37.                     }
  38.                 }
  39.             })
  40.             .reduce((a, b) => a + b);
  41.  
  42.         players.set(playerName, countOfCards);
  43.     }
  44.  
  45.     for (let [playerName, cardsCount] of players.entries()) {
  46.         console.log(`${playerName}: ${cardsCount}`);
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement