Advertisement
EntropyStarRover

card game

Jul 4th, 2019
158
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function cardGame(input) {
  2.     let playersAndTheirCards = {};
  3.  
  4.     for (let i = 0; i < input.length; i++) {
  5.         let line = input[i].split(": ");
  6.         let player = line[0];
  7.         let cards = line[1].split(", ");
  8.  
  9.         if (player in playersAndTheirCards) {
  10.             let currentCards = playersAndTheirCards[player];
  11.             let allCards=currentCards.concat(cards);
  12.             let distinctCards=[...new Set(allCards)];
  13.          
  14.             playersAndTheirCards[player]=distinctCards;
  15.  
  16.         } else {
  17.             playersAndTheirCards[player] = cards;
  18.         }
  19.     }
  20.  
  21.     for(let player in playersAndTheirCards) {
  22.  
  23.         let personalScore=0;
  24.         let cardsArr=playersAndTheirCards[player];
  25.    
  26.         cardsArr.forEach(card => {
  27.             let kvp=card.split("");
  28.                    
  29.             let color=kvp.pop();
  30.             let power=kvp.join("");
  31.  
  32.             let value=0;
  33.             let multiplier=0;
  34.  
  35.             switch(power){
  36.                 case "2": value=2; break;
  37.                 case "3": value=3; break;
  38.                 case "4": value=4; break;
  39.                 case "5": value=5; break;
  40.                 case "6": value=6; break;
  41.                 case "7": value=7; break;
  42.                 case "8": value=8; break;
  43.                 case "9": value=9; break;
  44.                 case "10": value=10; break;
  45.                 case "J": value=11; break;
  46.                 case "Q": value=12; break;
  47.                 case "K": value=13; break;
  48.                 case "A": value=14; break;
  49.             }
  50.  
  51.             switch(color){
  52.                 case "S":multiplier=4;break;
  53.                 case "H":multiplier=3;break;
  54.                 case "D":multiplier=2;break;
  55.                 case "C":multiplier=1;break;
  56.             }
  57.      
  58.             personalScore+=value*multiplier;          
  59.         });
  60.         console.log(`${player}: ${personalScore}`);
  61.      }    
  62. }
  63. cardGame([
  64.     'Peter: 2C, 4H, 9H, AS, QS',
  65.     'Tomas: 3H, 10S, JC, KD, 5S, 10S',
  66.     'Andrea: QH, QC, QS, QD',
  67.     'Tomas: 6H, 7S, KC, KD, 5S, 10C',
  68.     'Andrea: QH, QC, JS, JD, JC',
  69.     'Peter: JD, JD, JD, JD, JD, JD'
  70. ]
  71. )
Advertisement
RAW Paste Data Copied
Advertisement