AreWe

Wizard Poker

Jul 1st, 2020
1,171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. wizardPoker = (input) => {
  2.     let allCards = input.shift().split(':'),
  3.         deck = [];
  4.     while((cmd = input.shift().split(' '))[0] !== 'Ready') {
  5.         if(cmd[0] === 'Add') {
  6.             if(allCards.indexOf(cmd[1]) < 0) {
  7.                 console.log('Card not found.');
  8.             } else {
  9.                 deck.push(cmd[1]);
  10.             }
  11.         } else if(cmd[0] === 'Insert') {
  12.             let index = Number(cmd[2]);
  13.             if(allCards.indexOf(cmd[1]) < 0 || index < 0 || index > deck.length) {
  14.                 console.log('Error!');
  15.             } else {
  16.                 deck.splice(index, 0, cmd[1]);
  17.             }
  18.         } else if(cmd[0] === 'Remove') {
  19.             let index = deck.indexOf(cmd[1]);
  20.             if(index < 0) {
  21.                 console.log('Card not found.');
  22.             } else {
  23.                 deck.splice(index, 1);
  24.             }
  25.         } else if(cmd[0] === 'Swap') {
  26.             let [indexCard1, indexCard2] = [deck.indexOf(cmd[1]), deck.indexOf(cmd[2])];
  27.             [deck[indexCard1], deck[indexCard2]] = [cmd[2], cmd[1]];
  28.         } else { // 'Shuffle'
  29.             deck.reverse();
  30.         }
  31.     }
  32.     console.log(deck.join(' '));
  33. }
Advertisement
Add Comment
Please, Sign In to add comment