Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if(typeof require != "undefined"){
- _ = require("loadash");
- }
- var Card = class Card {
- constructor(number,suit){
- this.number = number;
- this.suit = suit;
- this.movementTween = {
- position: new THREE.Vector3(0,0,0),
- rotation: new THREE.Vector3(0,0,0)
- };
- };
- orderedDeck() {
- var cards = [];
- for(let i = 0; i < Card.numArray.length; i++) {
- for(let j = 0; j < Card.suitArray.length; j++){
- cards.push(new Card(i,Card.suitArray[j]));
- }//next j
- }//next i
- return cards;
- }//orderedDeck()
- fromName(name) {
- var match = name.match(/(\w+) of (\w+)/);
- if (match) {
- var number = _.findIndex(Card.numArray, (x) => {
- return x.toString() === match[1];
- });
- var suit = _.findIndex(Card.suitArray,(x) => {
- return x.toString() === match[2];
- });
- if(number != null && suit != null){
- return new Card(number,Card.suitArray[suit]);
- }
- }
- throw new Error(`${name} is not the name of a card.`);
- };//fromName(name)
- toName() {
- return `${Card.numArray[this.number].toString()} of ${this.suit}`;
- };
- //fill in the following function later
- friendlyRepesantation() { };
- friendlyNumber() {
- return Card.numArray[this.number];
- };
- fileName() {
- return this.texturePrefix + Card.numArray[this.number] + "_of_" + this.suit + ".png";
- }
- hasMultiples(cards,numberofMultiples){
- var sortedCards = [];
- for(let i = 0; i < cards.length; i++){
- if(typeof (sortedCards[cards[i].number]) == "undefined"){
- sortedCards[cards[i].number] = {
- cards: [cards[i]],
- num: 0
- };
- } else {
- sortedCards[cards[i].number].cards.push(cards[i]);
- }
- sortedCards[cards[i].number].num++;
- }
- var findThem = false;
- sortedCards.forEach((obj) => {
- if(parseInt(obj.num) === parseInt(numberofMultiples)){
- findThem = obj.cards;
- }
- });
- return findThem;
- };
- isFlush(cards) {
- if(cards.length < 5){
- return false;
- }
- var suits = {};
- for (let i = 0; i < cards.length; i++) {
- if(typeof suits[cards[i].suit] === "undefined") {
- suits[cards[i].suit] = {
- cards: [cards[i]],
- num: 0
- };
- }else {
- suits[cards[i].suit].cards.push(cards[i]);
- }
- suits[cards[i].suit].num++;
- }
- var isFlush = false;
- for(let propertyName in suits){
- if(suits.hasOwnProperty(propertyName) && suits[propertyName].num >= 5){
- isFlush = suits[propertyName].cards;
- }
- }
- return isFlush;
- };
- comparingAceLow(card){
- return card.number == 12 ? -1 : card.number;
- };
- comparingAceHigh(card){
- return card.number;
- };
- isStraight(cards){
- var possibilities = [
- _.sortBy(cards,Card.comparingAceLow),
- _.sortBy(cards,Card.comparingAceHigh)
- ];
- return _.some(possibilities, (cards) => {
- for(let i =0; i < cards.length - 1; i++){
- var first = cards[i].number, second = cards[1+1].number;
- if(first == 12 && second == 0){
- continue;
- }
- if(first + 1 !== second){
- return false;
- }
- }
- return true;
- });
- };
- sameCards(setOne,setTwo){
- //assuming cards are in the correct order
- if(setOne.length !== setTwo.length || setOne.length === 0 || !setOne.length){
- return false;
- }
- for(let i = 0; i < setOne.length; i++){
- if(setOne[i].number !== setTwo[i].number || setOne[i].suit !== setTwo[i].suit){
- return false;
- }
- };
- return true;
- };
- max(cards){
- return Math.max.apply(null,cards.map((val) => {
- return val.number;
- }));
- };
- }
- Card.numArray = [2,3,4,5,6,7,8,9,10,"jack","queen","king","ace"];
- Card.suitArray = ["clubs","diamonds","hearts","spades"];
- Card.texturePrefix = "assets/Cards/";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement