Advertisement
Braber01

card_js es6_ified

Sep 25th, 2017
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if(typeof require != "undefined"){
  2.     _ = require("loadash");
  3. }
  4.  
  5. var Card = class Card {
  6.     constructor(number,suit){
  7.         this.number = number;
  8.         this.suit = suit;
  9.         this.movementTween = {
  10.             position: new THREE.Vector3(0,0,0),
  11.             rotation: new THREE.Vector3(0,0,0)
  12.         };
  13.     };
  14.     orderedDeck() {
  15.         var cards = [];
  16.         for(let i = 0; i < Card.numArray.length; i++) {
  17.             for(let j = 0; j < Card.suitArray.length; j++){
  18.                 cards.push(new Card(i,Card.suitArray[j]));
  19.             }//next j
  20.         }//next i
  21.         return cards;
  22.     }//orderedDeck()
  23.  
  24.     fromName(name) {
  25.         var match = name.match(/(\w+) of (\w+)/);
  26.         if (match) {
  27.             var number = _.findIndex(Card.numArray, (x) => {
  28.                 return x.toString() === match[1];
  29.             });
  30.             var suit = _.findIndex(Card.suitArray,(x) => {
  31.                 return x.toString() === match[2];
  32.             });
  33.             if(number != null && suit != null){
  34.                 return new Card(number,Card.suitArray[suit]);
  35.             }
  36.         }
  37.         throw new Error(`${name} is not the name of a card.`);
  38.     };//fromName(name)
  39.  
  40.     toName() {
  41.         return `${Card.numArray[this.number].toString()} of ${this.suit}`;
  42.     };
  43.     //fill in the following function later
  44.     friendlyRepesantation() { };
  45.    
  46.     friendlyNumber() {
  47.         return Card.numArray[this.number];
  48.     };
  49.  
  50.     fileName() {
  51.         return this.texturePrefix + Card.numArray[this.number] + "_of_" + this.suit + ".png";
  52.     }
  53.  
  54.     hasMultiples(cards,numberofMultiples){
  55.         var sortedCards = [];
  56.         for(let i = 0; i < cards.length; i++){
  57.             if(typeof (sortedCards[cards[i].number]) == "undefined"){
  58.                 sortedCards[cards[i].number] = {
  59.                     cards: [cards[i]],
  60.                     num: 0
  61.                 };
  62.             } else {
  63.                 sortedCards[cards[i].number].cards.push(cards[i]);
  64.             }
  65.             sortedCards[cards[i].number].num++;
  66.         }
  67.         var findThem = false;
  68.  
  69.         sortedCards.forEach((obj) => {
  70.             if(parseInt(obj.num) === parseInt(numberofMultiples)){
  71.                 findThem = obj.cards;
  72.             }
  73.         });
  74.         return findThem;
  75.     };
  76.     isFlush(cards) {
  77.         if(cards.length < 5){
  78.             return false;
  79.         }
  80.         var suits = {};
  81.         for (let i = 0; i < cards.length; i++) {
  82.             if(typeof suits[cards[i].suit] === "undefined") {
  83.                 suits[cards[i].suit] = {
  84.                     cards: [cards[i]],
  85.                     num: 0
  86.                 };
  87.             }else {
  88.                 suits[cards[i].suit].cards.push(cards[i]);
  89.             }
  90.             suits[cards[i].suit].num++;
  91.         }
  92.         var isFlush = false;
  93.  
  94.         for(let propertyName in suits){
  95.             if(suits.hasOwnProperty(propertyName) && suits[propertyName].num >= 5){
  96.                 isFlush = suits[propertyName].cards;
  97.             }
  98.         }
  99.         return isFlush;
  100.     };
  101.     comparingAceLow(card){
  102.         return card.number == 12 ? -1 : card.number;
  103.     };
  104.     comparingAceHigh(card){
  105.         return card.number;
  106.     };
  107.     isStraight(cards){
  108.         var possibilities = [
  109.             _.sortBy(cards,Card.comparingAceLow),
  110.             _.sortBy(cards,Card.comparingAceHigh)
  111.         ];
  112.  
  113.         return _.some(possibilities, (cards) => {
  114.             for(let i =0; i < cards.length - 1; i++){
  115.                 var first = cards[i].number, second = cards[1+1].number;
  116.                 if(first == 12 && second == 0){
  117.                     continue;
  118.                 }
  119.                 if(first + 1 !== second){
  120.                     return false;
  121.                 }
  122.             }
  123.             return true;
  124.         });
  125.     };
  126.  
  127.     sameCards(setOne,setTwo){
  128.         //assuming cards are in the correct order
  129.         if(setOne.length !== setTwo.length || setOne.length === 0 || !setOne.length){
  130.             return false;
  131.         }
  132.         for(let i = 0; i < setOne.length; i++){
  133.             if(setOne[i].number !== setTwo[i].number || setOne[i].suit !== setTwo[i].suit){
  134.                 return false;
  135.             }
  136.         };
  137.         return true;
  138.     };
  139.     max(cards){
  140.         return Math.max.apply(null,cards.map((val) => {
  141.             return val.number;
  142.         }));
  143.     };
  144. }
  145. Card.numArray = [2,3,4,5,6,7,8,9,10,"jack","queen","king","ace"];
  146. Card.suitArray = ["clubs","diamonds","hearts","spades"];
  147. Card.texturePrefix = "assets/Cards/";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement